Angle difference between two bearings: Difference between revisions

Added Algol 68
(Angle difference between two bearings in Run BASIC)
(Added Algol 68)
 
(22 intermediate revisions by 12 users not shown)
Line 34:
 
=={{header|11l}}==
<langsyntaxhighlight lang="11l">F get_difference(b1, b2)
R wrap(b2 - b1, -180.0, 180.0)
 
Line 51:
print(get_difference(-165313.6666297357, 33693.9894517456))
print(get_difference(1174.8380510598456, -154146.66490124757))
print(get_difference(60175.77306795546, 42213.07192354373))</langsyntaxhighlight>
 
{{out}}
Line 74:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<langsyntaxhighlight lang="360asm">* Angle difference between two bearings - 06/06/2018
ANGLEDBB CSECT
USING ANGLEDBB,R13 base register
Line 120:
XDEC DS CL12 temp
YREGS
END ANGLEDBB</langsyntaxhighlight>
{{out}}
<pre>
Line 132:
-78.3251 -159.0360 -80.7109
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program diffAngle64.s */
 
/************************************/
/* Constantes */
/************************************/
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szCarriageReturn: .asciz "\n"
szMessResult: .asciz "Difference between @ and @ = @ \n"
 
.align 8
fB1: .double 0F20.0
fB2: .double 0F45.0
fB3: .double 0F-45.0
fB4: .double 0F-85.0
fB5: .double 90.0
fB6: .double -95.0
fB7: .double 125.0
fB8: .double 145.0
fB9: .double 0F29.4803
fB10: .double 0F-88.6381
fB11: .double 0F-78.3251
fB12: .double 0F-159.036
fB13: .double 0F-70099.74233810938
fB14: .double 0F29840.67437876723
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr x0,qAdrfB1
ldr x1,qAdrfB2
bl testComputeAngle
//b 100f
ldr x0,qAdrfB3
ldr x1,qAdrfB2
bl testComputeAngle
ldr x0,qAdrfB4
ldr x1,qAdrfB5
bl testComputeAngle
ldr x0,qAdrfB6
ldr x1,qAdrfB5
bl testComputeAngle
 
ldr x0,qAdrfB3
ldr x1,qAdrfB7
bl testComputeAngle
ldr x0,qAdrfB3
ldr x1,qAdrfB8
bl testComputeAngle
ldr x0,qAdrfB9
ldr x1,qAdrfB10
bl testComputeAngle
 
ldr x0,qAdrfB11
ldr x1,qAdrfB12
bl testComputeAngle
 
ldr x0,qAdrfB13
ldr x1,qAdrfB14
bl testComputeAngle
 
100: // standard end of the program
mov x0, #0 // return code
mov x8,EXIT
svc #0 // perform the system call
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsZoneConv: .quad sZoneConv
qAdrfB1: .quad fB1
qAdrfB2: .quad fB2
qAdrfB3: .quad fB3
qAdrfB4: .quad fB4
qAdrfB5: .quad fB5
qAdrfB6: .quad fB6
qAdrfB7: .quad fB7
qAdrfB8: .quad fB8
qAdrfB9: .quad fB9
qAdrfB10: .quad fB10
qAdrfB11: .quad fB11
qAdrfB12: .quad fB12
qAdrfB13: .quad fB13
qAdrfB14: .quad fB14
/******************************************************************/
/* compute difference and display result */
/******************************************************************/
/* s0 contains bearing 1 */
/* s1 contains bearing 2 */
testComputeAngle:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
ldr d0,[x0]
fmov d2,d0
ldr d1,[x1]
bl computeDiffAngle
fmov d3,d0
fmov d0,d2
ldr x0,qAdrsZoneConv
bl convertirFloat
ldr x0,qAdrszMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc
mov x3,x0
fmov d0,d1
ldr x0,qAdrsZoneConv
bl convertirFloat
mov x0,x3
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc
mov x3,x0
fmov d0,d3
ldr x0,qAdrsZoneConv
bl convertirFloat
mov x0,x3
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
qAdrszMessResult: .quad szMessResult
/******************************************************************/
/* compute difference of two bearing */
/******************************************************************/
/* d0 contains bearing 1 */
/* d1 contains bearing 2 */
computeDiffAngle:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp d1,d2,[sp,-16]! // save registres
stp d3,d4,[sp,-16]! // save registres
mov x1,#360
mov x4,#0 // top positive/negative
fcvtzs d4,d0 // conversion.integer
scvtf d2,d4 // conversion float
fsub d2,d0,d2 // partie décimale
fmov x0,d4 // partie entière
cmp x0,#0 // negative ?
bge 1f
neg x0,x0 // yes -> inversion
mov x4,#1
1:
udiv x2,x0,x1 // divide by 360
msub x3,x2,x1,x0
cmp x4,#0 // value negative ?
neg x5,x3
csel x3,x5,x3,ne // inversion remainder
fmov d3,x3
scvtf d3,d3 // and conversion float
fadd d0,d3,d2 // add decimal part
mov x4,#0 // bearing 2
fcvtzs d4,d1 // conversion integer
scvtf d2,d4 // conversion float
fsub d2,d1,d2 // partie décimale
fmov x0,d4
cmp x0,#0
bge 2f
neg x0,x0
mov x4,#1
2:
udiv x2,x0,x1 // divide by 360
msub x3,x2,x1,x0
cmp x4,#0
neg x5,x3
csel x3,x5,x3,ne // inversion remainder
fmov d3,x3
scvtf d3,d3 // conversion float
fadd d1,d3,d2
fsub d0,d1,d0 // calculate the difference between the 2 values
mov x0,180
fmov d3,x0
scvtf d3,d3 // conversion float 180
fmov d4,x1 // 360
scvtf d4,d4 // conversion float 360
fcmp d0,#0.0 // difference is negative ?
blt 2f
// difference is positive
fcmp d0,d4 // difference > 360
ble 3f
fsub d0,d0,d4 // yes -> difference - 360
3:
fcmp d0,d3 // compare difference and 180
ble 100f
fsub d0,d4,d0 // > 180 calculate 360 - difference
fneg d0,d0 // and negate
b 100f
 
2: // différence is négative
fneg d2,d4 // -360
fcmp d0,d2 // compare différence et - 360
ble 3f
fsub d0,d0,d4 // sub 360 to différence
3:
fneg d3,d3 // -180
fcmp d0,d3 // compare difference and -180
bge 100f
fadd d0,d4,d0 // calculate 360 + différence
 
100:
ldp d3,d4,[sp],16 // restaur registers
ldp d1,d2,[sp],16 // restaur registers
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
 
/******************************************************************/
/* Conversion Float */
/******************************************************************/
/* d0 contains Float */
/* x0 contains address conversion area mini 20 charactèrs */
/* x0 return result length */
/* see https://blog.benoitblanchon.fr/lightweight-float-to-string/ */
convertirFloat:
stp x1,lr,[sp,-16]! // save registres
stp x2,x3,[sp,-16]! // save registres
stp x4,x5,[sp,-16]! // save registres
stp x6,x7,[sp,-16]! // save registres
stp x8,x9,[sp,-16]! // save registres
stp d1,d2,[sp,-16]! // save registres
mov x6,x0 // save area address
fmov x0,d0
mov x8,#0 // result length
mov x3,#'+'
strb w3,[x6] // signe + forcing
mov x2,x0
tbz x2,63,1f
mov x2,1
lsl x2,x2,63
bic x0,x0,x2
mov x3,#'-' // sign -
strb w3,[x6]
1:
adds x8,x8,#1 // next position
cmp x0,#0 // case 0 positive or negative
bne 2f
mov x3,#'0'
strb w3,[x6,x8] // store character 0
adds x8,x8,#1
strb wzr,[x6,x8] // store 0 final
mov x0,x8 // return length
b 100f
2:
ldr x2,iMaskExposant
mov x1,x0
and x1,x1,x2 // exposant
cmp x1,x2
bne 4f
tbz x0,51,3f // test bit 51 to zéro
mov x2,#'N' // case Nan. store byte no possible store integer
strb w2,[x6] // area no aligned
mov x2,#'a'
strb w2,[x6,#1]
mov x2,#'n'
strb w2,[x6,#2]
mov x2,#0 // 0 final
strb w2,[x6,#3]
mov x0,#3
b 100f
3: // case infini positive or négative
mov x2,#'I'
strb w2,[x6,x8]
adds x8,x8,#1
mov x2,#'n'
strb w2,[x6,x8]
adds x8,x8,#1
mov x2,#'f'
strb w2,[x6,x8]
adds x8,x8,#1
mov x2,#0
strb w2,[x6,x8]
mov x0,x8
b 100f
4:
bl normaliserFloat
mov x5,x0 // save exposant
fcvtzu d2,d0
fmov x0,d2 // part integer
scvtf d1,d2 // conversion float
fsub d1,d0,d1 // extraction part fractional
ldr d2,dConst1
fmul d1,d2,d1 // to crop it in full
fcvtzu d1,d1 // convertion integer
fmov x4,d1 // fract value
// conversion part integer to x0
mov x2,x6 // save address begin area
adds x6,x6,x8
mov x1,x6
bl conversion10
add x6,x6,x0
mov x3,#','
strb w3,[x6]
adds x6,x6,#1
mov x0,x4 // conversion part fractionnaire
mov x1,x6
bl conversion10SP
add x6,x6,x0
sub x6,x6,#1
// remove trailing zeros
5:
ldrb w0,[x6]
cmp w0,#'0'
bne 6f
sub x6,x6,#1
b 5b
6:
cmp w0,#','
bne 7f
sub x6,x6,#1
7:
cmp x5,#0 // if exposant = 0 no display
bne 8f
add x6,x6,#1
b 10f
8:
add x6,x6,#1
mov x3,#'E'
strb w3,[x6]
add x6,x6,#1
mov x0,x5 // conversion exposant
mov x3,x0
tbz x3,63,9f // exposant negative ?
neg x0,x0
mov x3,#'-'
strb w3,[x6]
adds x6,x6,#1
9:
mov x1,x6
bl conversion10
add x6,x6,x0
10:
strb wzr,[x6] // store 0 final
adds x6,x6,#1
mov x0,x6
subs x0,x0,x2 // retour de la longueur de la zone
subs x0,x0,#1 // sans le 0 final
 
100:
ldp d1,d2,[sp],16 // restaur registres
ldp x8,x9,[sp],16 // restaur registres
ldp x6,x7,[sp],16 // restaur registres
ldp x4,x5,[sp],16 // restaur registres
ldp x2,x3,[sp],16 // restaur registres
ldp x1,lr,[sp],16 // restaur registres
ret
iMaskExposant: .quad 0x7FF<<52
dConst1: .double 0f1E17
 
/***************************************************/
/* normaliser float */
/***************************************************/
/* x0 contain float value (always positive value and <> Nan) */
/* d0 return new value */
/* x0 return exposant */
normaliserFloat:
stp x1,lr,[sp,-16]! // save registers
fmov d0,x0 // value float
mov x0,#0 // exposant
ldr d1,dConstE7 // no normalisation for value < 1E7
fcmp d0,d1
blo 10f // if d0 < dConstE7
ldr d1,dConstE256
fcmp d0,d1
blo 1f
fdiv d0,d0,d1
adds x0,x0,#256
1:
ldr d1,dConstE128
fcmp d0,d1
blo 1f
fdiv d0,d0,d1
adds x0,x0,#128
1:
ldr d1,dConstE64
fcmp d0,d1
blo 1f
fdiv d0,d0,d1
adds x0,x0,#64
1:
ldr d1,dConstE32
fcmp d0,d1
blo 1f
fdiv d0,d0,d1
adds x0,x0,#32
1:
ldr d1,dConstE16
fcmp d0,d1
blo 2f
fdiv d0,d0,d1
adds x0,x0,#16
2:
ldr d1,dConstE8
fcmp d0,d1
blo 3f
fdiv d0,d0,d1
adds x0,x0,#8
3:
ldr d1,dConstE4
fcmp d0,d1
blo 4f
fdiv d0,d0,d1
adds x0,x0,#4
4:
ldr d1,dConstE2
fcmp d0,d1
blo 5f
fdiv d0,d0,d1
adds x0,x0,#2
5:
ldr d1,dConstE1
fcmp d0,d1
blo 10f
fdiv d0,d0,d1
adds x0,x0,#1
 
10:
ldr d1,dConstME5 // pas de normalisation pour les valeurs > 1E-5
fcmp d0,d1
bhi 100f // fin
ldr d1,dConstME255
fcmp d0,d1
bhi 11f
ldr d1,dConstE256
 
fmul d0,d0,d1
subs x0,x0,#256
11:
ldr d1,dConstME127
fcmp d0,d1
bhi 11f
ldr d1,dConstE128
 
fmul d0,d0,d1
subs x0,x0,#128
11:
ldr d1,dConstME63
fcmp d0,d1
bhi 11f
ldr d1,dConstE64
 
fmul d0,d0,d1
subs x0,x0,#64
11:
ldr d1,dConstME31
fcmp d0,d1
bhi 11f
ldr d1,dConstE32
 
fmul d0,d0,d1
subs x0,x0,#32
11:
ldr d1,dConstME15
fcmp d0,d1
bhi 12f
ldr d1,dConstE16
fmul d0,d0,d1
subs x0,x0,#16
12:
ldr d1,dConstME7
fcmp d0,d1
bhi 13f
ldr d1,dConstE8
fmul d0,d0,d1
subs x0,x0,#8
13:
ldr d1,dConstME3
fcmp d0,d1
bhi 14f
ldr d1,dConstE4
fmul d0,d0,d1
subs x0,x0,#4
14:
ldr d1,dConstME1
fcmp d0,d1
bhi 15f
ldr d1,dConstE2
fmul d0,d0,d1
subs x0,x0,#2
15:
ldr d1,dConstE0
fcmp d0,d1
bhi 100f
ldr d1,dConstE1
fmul d0,d0,d1
subs x0,x0,#1
 
100: // fin standard de la fonction
ldp x1,lr,[sp],16 // restaur registres
ret
.align 2
dConstE7: .double 0f1E7
dConstE256: .double 0f1E256
dConstE128: .double 0f1E128
dConstE64: .double 0f1E64
dConstE32: .double 0f1E32
dConstE16: .double 0f1E16
dConstE8: .double 0f1E8
dConstE4: .double 0f1E4
dConstE2: .double 0f1E2
dConstE1: .double 0f1E1
dConstME5: .double 0f1E-5
dConstME255: .double 0f1E-255
dConstME127: .double 0f1E-127
dConstME63: .double 0f1E-63
dConstME31: .double 0f1E-31
dConstME15: .double 0f1E-15
dConstME7: .double 0f1E-7
dConstME3: .double 0f1E-3
dConstME1: .double 0f1E-1
dConstE0: .double 0f1E0
 
/******************************************************************/
/* Décimal Conversion */
/******************************************************************/
/* x0 contain value et x1 address conversion area */
conversion10SP:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
mov x5,x1
mov x4,#16
mov x2,x0
mov x1,#10 // décimal conversion
1: // conversion loop
mov x0,x2 // copy begin number or quotient
udiv x2,x0,x1 // division by 10
msub x3,x1,x2,x0 // compute remainder
add x3,x3,#48 // compute digit
strb w3,[x5,x4] // store byte address area (x5) + offset (x4)
subs x4,x4,#1 // position precedente
bge 1b
strb wzr,[x5,16] // 0 final
100:
ldp x4,x5,[sp],16 // restaur registers
ldp x2,x3,[sp],16 // restaur registers
ldp x1,lr,[sp],16 // restaur registers
ret
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../includeARM64.inc"
</syntaxhighlight>
<pre>
Difference between +20 and +45 = +25
Difference between -45 and +45 = +90
Difference between -85 and +90 = +175
Difference between -95 and +90 = -175
Difference between -45 and +125 = +170
Difference between -45 and +145 = -170
Difference between +29,4802 and -88,638099 = -118,1
Difference between -78,325 and -159 = -80,7108999
Difference between -70099,7423381 and +29840,674378 = -139,58328
</pre>
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:REALMATH.ACT"
 
INT FUNC AngleI(INT a1,a2)
Line 194 ⟶ 778:
TestR("1174.8380510598456","-154146.66490124757")
TestR("60175.77306795546","42213.07192354373")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Angle_difference_between_two_bearings.png Screenshot from Atari 8-bit computer]
Line 214 ⟶ 798:
=={{header|Ada}}==
Ada does not provide a built-in mod function for floating point types. This program supplies one.
<syntaxhighlight lang="ada">
<lang Ada>
-----------------------------------------------------------------------
-- Angle difference between two bearings
Line 266 ⟶ 850:
end Bearing_Angles;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 280 ⟶ 864:
Difference between -154146.6649 and 1174.8381 is -161.5030
Difference between 42213.0719 and 60175.7731 is 37.2989
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|11l}}
<syntaxhighlight lang="algol68">
BEGIN # angle difference between 2 bearings - translated from the 11l sample #
 
PROC wrap = (REAL v, l1, l2 )REAL:
BEGIN
REAL result := v;
WHILE result < l1 DO result +:= 2 * l2 OD;
WHILE result > l2 DO result +:= 2 * l1 OD;
result
END # wrap # ;
PROC get_difference = ( REAL b1, b2 )REAL: wrap( b2 - b1, -180.0, 180.0 );
 
OP FMT = ( REAL v )STRING:
BEGIN
STRING result := fixed( ABS v, 0, 3 );
IF result[ LWB result ] = "." THEN "0" +=: result FI;
WHILE result[ UPB result ] = "0" DO result := result[ : UPB result - 1 ] OD;
IF result[ UPB result ] = "." THEN result := result[ : UPB result - 1 ] FI;
IF v < 0 THEN "-" ELSE " " FI + result
END # FMT # ;
 
print( ( FMT get_difference( 20.0, 45.0 ), newline ) );
print( ( FMT get_difference( -45.0, 45.0 ), newline ) );
print( ( FMT get_difference( -85.0, 90.0 ), newline ) );
print( ( FMT get_difference( -95.0, 90.0 ), newline ) );
print( ( FMT get_difference( -45.0, 125.0 ), newline ) );
print( ( FMT get_difference( -45.0, 145.0 ), newline ) );
print( ( FMT get_difference( -45.0, 125.0 ), newline ) );
print( ( FMT get_difference( -45.0, 145.0 ), newline ) );
print( ( FMT get_difference( 29.4803, -88.6381 ), newline ) );
print( ( FMT get_difference( -78.3251, -159.036 ), newline ) );
print( ( newline ) );
print( ( FMT get_difference( -70099.74233810938, 29840.67437876723 ), newline ) );
print( ( FMT get_difference( -165313.6666297357, 33693.9894517456 ), newline ) );
print( ( FMT get_difference( 1174.8380510598456, -154146.66490124757 ), newline ) );
print( ( FMT get_difference( 60175.77306795546, 42213.07192354373 ), newline ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
25
90
175
-175
170
-170
170
-170
-118.118
-80.711
 
-139.583
-72.344
-161.503
37.299
</pre>
 
=={{header|APL}}==
Returns an angle in (-180,180]; so two opposite bearings have a difference of 180 degrees, which is more natural than -180 degrees.
<langsyntaxhighlight APLlang="apl">[0] D←B1 DIFF B2
[1] D←180+¯360|180+B2-B1
</syntaxhighlight>
</lang>
{{out}}
<pre> 'B1' 'B2' 'DIFFERENCE'⍪(⊂'¯¯¯¯¯¯¯¯¯¯')⍪(⊃B),DIFF/¨B
Line 310 ⟶ 955:
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI or android with termux */
/* program diffAngle.s */
 
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szCarriageReturn: .asciz "\n"
szMessResult: .asciz "Difference between @ and @ = @ \n"
 
.align 4
fB1: .float 20.0
fB2: .float 45.0
fB3: .float -45.0
fB4: .float -85.0
fB5: .float 90.0
fB6: .float -95.0
fB7: .float 125.0
fB8: .float 145.0
fB9: .float 29.4803
fB10: .float -88.6381
fB11: .float -78.3251
fB12: .float -159.036
fB13: .float -70099.74233810938
fB14: .float 29840.67437876723
 
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main:
ldr r0,iAdrfB1
ldr r1,iAdrfB2
bl testComputeAngle
ldr r0,iAdrfB3
ldr r1,iAdrfB2
bl testComputeAngle
ldr r0,iAdrfB4
ldr r1,iAdrfB5
bl testComputeAngle
ldr r0,iAdrfB6
ldr r1,iAdrfB5
bl testComputeAngle
 
ldr r0,iAdrfB3
ldr r1,iAdrfB7
bl testComputeAngle
ldr r0,iAdrfB3
ldr r1,iAdrfB8
bl testComputeAngle
ldr r0,iAdrfB9
ldr r1,iAdrfB10
bl testComputeAngle
 
ldr r0,iAdrfB11
ldr r1,iAdrfB12
bl testComputeAngle
 
ldr r0,iAdrfB13
ldr r1,iAdrfB14
bl testComputeAngle
 
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsZoneConv: .int sZoneConv
iAdrfB1: .int fB1
iAdrfB2: .int fB2
iAdrfB3: .int fB3
iAdrfB4: .int fB4
iAdrfB5: .int fB5
iAdrfB6: .int fB6
iAdrfB7: .int fB7
iAdrfB8: .int fB8
iAdrfB9: .int fB9
iAdrfB10: .int fB10
iAdrfB11: .int fB11
iAdrfB12: .int fB12
iAdrfB13: .int fB13
iAdrfB14: .int fB14
/******************************************************************/
/* compute difference and display result */
/******************************************************************/
/* s0 contains bearing 1 */
/* s1 contains bearing 2 */
testComputeAngle:
push {r1-r3,lr} @ save registers
vldr.f32 s0,[r0]
vmov s2,s0
vldr.f32 s1,[r1]
bl computeDiffAngle
vmov s3,s0
vmov s0,s2
ldr r0,iAdrsZoneConv
bl convertirFloat
ldr r0,iAdrszMessResult
ldr r1,iAdrsZoneConv
bl strInsertAtCharInc
mov r3,r0
vmov s0,s1
ldr r0,iAdrsZoneConv
bl convertirFloat
mov r0,r3
ldr r1,iAdrsZoneConv
bl strInsertAtCharInc
mov r3,r0
vmov s0,s3
ldr r0,iAdrsZoneConv
bl convertirFloat
mov r0,r3
ldr r1,iAdrsZoneConv
bl strInsertAtCharInc
bl affichageMess
100:
pop {r1-r3,pc} @ restaur registers
iAdrszMessResult: .int szMessResult
/******************************************************************/
/* compute difference of two bearing */
/******************************************************************/
/* s0 contains bearing 1 */
/* s1 contains bearing 2 */
computeDiffAngle:
push {r1-r4,lr} @ save registers
vpush {s1-s4}
mov r1,#360
mov r4,#0 @ top positive/negative
vcvt.s32.f32 s4,s0 @ conversion integer
vcvt.f32.s32 s2,s4 @ conversion float
vsub.f32 s2,s0,s2 @ partie décimale
vmov r0,s4 @ partie entière
cmp r0,#0 @ negative ?
neglt r0,r0 @ yes -> inversion
movlt r4,#1
bl division @ divide by 360 (r0 dividende r1 divisor r2 quotient r3 remainder)
cmp r4,#0 @ value negative ?
negne r3,r3 @ inversion remainder
vmov s3,r3
vcvt.f32.s32 s3,s3 @ and conversion float
vadd.f32 s0,s3,s2 @ add decimal part
mov r4,#0 @ bearing 2
vcvt.s32.f32 s4,s1 @ conversion integer
vcvt.f32.s32 s2,s4 @ conversion float
vsub.f32 s2,s1,s2 @ partie décimale
vmov r0,s4
cmp r0,#0
neglt r0,r0
movlt r4,#1
bl division @ divide by 360
cmp r4,#0
negne r3,r3 @ negate remainder
vmov s3,r3
vcvt.f32.s32 s3,s3 @ conversion float
vadd.f32 s1,s3,s2
vsub.f32 s0,s1,s0 @ calculate the difference between the 2 values
mov r0,#180
vmov s3,r0
vcvt.f32.s32 s3,s3 @ conversion float 180
vmov s4,r1 @ 360
vcvt.f32.s32 s4,s4 @ conversion float 360
vcmp.f32 s0,#0.0 @ difference is negative ?
vmrs APSR_nzcv, FPSCR @ flags transfert (do not forget this instruction !!!)
blt 2f
@ difference is positive
vcmp.f32 s0,s4 @ difference > 360
vmrs APSR_nzcv, FPSCR @ flags transfert (do not forget this instruction !!!)
vsubgt.f32 s0,s4 @ yes -> difference - 360
vcmp.f32 s0,s3 @ compare difference and 180
vmrs APSR_nzcv, FPSCR @ flags transfert (do not forget this instruction !!!)
vsubgt.f32 s0,s4,s0 @ > 180 calculate 360 - difference
vneggt.f32 s0,s0 @ and negate
b 100f
 
2: @ différence is négative
vneg.f32 s2,s4 @ -360
vcmp.f32 s0,s2 @ compare différence et - 360
vmrs APSR_nzcv, FPSCR @ flags transfert (do not forget this instruction !!!)
vsubgt.f32 s0,s4 @ sub 360 to différence
vneg.f32 s3,s3 @ -180
vcmp.f32 s0,s3 @ compare difference and -180
vmrs APSR_nzcv, FPSCR @ flags transfert (do not forget this instruction !!!)
vaddlt.f32 s0,s4,s0 @ calculate 360 + différence
 
100:
vpop {s1-s4}
pop {r1-r4,pc} @ restaur registers
 
/******************************************************************/
/* Conversion Float */
/******************************************************************/
/* s0 contains Float */
/* r0 contains address conversion area mini 20 charactèrs*/
/* r0 return result length */
convertirFloat:
push {r1-r7,lr}
vpush {s0-s2}
mov r6,r0 @ save area address
vmov r0,s0
movs r7,#0 @ result length
movs r3,#'+'
strb r3,[r6] @ sign + forcing
mov r2,r0
lsls r2,#1 @ extraction bit 31
bcc 1f @ positive ?
lsrs r0,r2,#1 @ raz sign if negative
movs r3,#'-' @ sign -
strb r3,[r6]
1:
adds r7,#1 @ next position
cmp r0,#0 @ case of positive or negative 0
bne 2f
movs r3,#'0'
strb r3,[r6,r7] @ store character 0
adds r7,#1 @ next position
movs r3,#0
strb r3,[r6,r7] @ store 0 final
mov r0,r7 @ return length
b 100f @ and end
2:
ldr r2,iMaskExposant
mov r1,r0
ands r1,r2 @ exposant = 255 ?
cmp r1,r2
bne 4f
lsls r0,#10 @ bit 22 à 0 ?
bcc 3f @ yes
movs r2,#'N' @ case of Nan. store byte, if not possible store int
strb r2,[r6] @ area no aligned
movs r2,#'a'
strb r2,[r6,#1]
movs r2,#'n'
strb r2,[r6,#2]
movs r2,#0 @ 0 final
strb r2,[r6,#3]
movs r0,#3 @ return length 3
b 100f
3: @ case infini positive or négative
movs r2,#'I'
strb r2,[r6,r7]
adds r7,#1
movs r2,#'n'
strb r2,[r6,r7]
adds r7,#1
movs r2,#'f'
strb r2,[r6,r7]
adds r7,#1
movs r2,#0
strb r2,[r6,r7]
mov r0,r7
b 100f
4:
bl normaliserFloat
mov r5,r0 @ save exposant
VCVT.U32.f32 s2,s0 @ integer value of integer part
vmov r0,s2 @ integer part
VCVT.F32.U32 s1,s2 @ conversion float
vsub.f32 s1,s0,s1 @ extraction fract part
vldr s2,iConst1
vmul.f32 s1,s2,s1 @ to crop it in full
 
VCVT.U32.f32 s1,s1 @ integer conversion
vmov r4,s1 @ fract value
@ integer conversion in r0
mov r2,r6 @ save address area begin
adds r6,r7
mov r1,r6
bl conversion10
add r6,r0
movs r3,#','
strb r3,[r6]
adds r6,#1
mov r0,r4 @ conversion fractional part
mov r1,r6
bl conversion10SP @ spécial routine with conservation begin 0
add r6,r0
subs r6,#1
@ remove trailing zeros
5:
ldrb r0,[r6]
cmp r0,#'0'
bne 6f
subs r6,#1
b 5b
6:
cmp r0,#','
bne 7f
subs r6,#1
7:
adds r6,#1
movs r3,#'E'
strb r3,[r6]
adds r6,#1
mov r0,r5 @ conversion exposant
mov r3,r0
lsls r3,#1
bcc 4f
rsbs r0,r0,#0
movs r3,#'-'
strb r3,[r6]
adds r6,#1
4:
mov r1,r6
bl conversion10
add r6,r0
movs r3,#0
strb r3,[r6]
adds r6,#1
mov r0,r6
subs r0,r2 @ return length result
subs r0,#1 @ - 0 final
 
100:
vpop {s0-s2}
pop {r1-r7,pc}
iMaskExposant: .int 0xFF<<23
iConst1: .float 0f1E9
 
/***************************************************/
/* normaliser float */
/***************************************************/
/* r0 contain float value (always positive value and <> Nan) */
/* s0 return new value */
/* r0 return exposant */
normaliserFloat:
push {lr} @ save registre
vmov s0,r0 @ value float
movs r0,#0 @ exposant
vldr s1,iConstE7 @ no normalisation for value < 1E7
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 10f @ if s0 < iConstE7
vldr s1,iConstE32
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 1f
vldr s1,iConstE32
vdiv.f32 s0,s0,s1
adds r0,#32
1:
vldr s1,iConstE16
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 2f
vldr s1,iConstE16
vdiv.f32 s0,s0,s1
adds r0,#16
2:
vldr s1,iConstE8
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 3f
vldr s1,iConstE8
vdiv.f32 s0,s0,s1
adds r0,#8
3:
vldr s1,iConstE4
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 4f
vldr s1,iConstE4
vdiv.f32 s0,s0,s1
adds r0,#4
4:
vldr s1,iConstE2
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 5f
vldr s1,iConstE2
vdiv.f32 s0,s0,s1
adds r0,#2
5:
vldr s1,iConstE1
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
blo 10f
vldr s1,iConstE1
vdiv.f32 s0,s0,s1
adds r0,#1
 
10:
vldr s1,iConstME5 @ pas de normalisation pour les valeurs > 1E-5
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 100f
vldr s1,iConstME31
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 11f
vldr s1,iConstE32
 
vmul.f32 s0,s0,s1
subs r0,#32
11:
vldr s1,iConstME15
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 12f
vldr s1,iConstE16
vmul.f32 s0,s0,s1
subs r0,#16
12:
vldr s1,iConstME7
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 13f
vldr s1,iConstE8
vmul.f32 s0,s0,s1
subs r0,#8
13:
vldr s1,iConstME3
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 14f
vldr s1,iConstE4
vmul.f32 s0,s0,s1
subs r0,#4
14:
vldr s1,iConstME1
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 15f
vldr s1,iConstE2
vmul.f32 s0,s0,s1
subs r0,#2
15:
vldr s1,iConstE0
vcmp.f32 s0,s1
vmrs APSR_nzcv,FPSCR
bhi 100f
vldr s1,iConstE1
vmul.f32 s0,s0,s1
subs r0,#1
 
100: @ fin standard de la fonction
pop {pc} @ restaur des registres
.align 2
iConstE7: .float 0f1E7
iConstE32: .float 0f1E32
iConstE16: .float 0f1E16
iConstE8: .float 0f1E8
iConstE4: .float 0f1E4
iConstE2: .float 0f1E2
iConstE1: .float 0f1E1
iConstME5: .float 0f1E-5
iConstME31: .float 0f1E-31
iConstME15: .float 0f1E-15
iConstME7: .float 0f1E-7
iConstME3: .float 0f1E-3
iConstME1: .float 0f1E-1
iConstE0: .float 0f1E0
/******************************************************************/
/* Décimal Conversion */
/******************************************************************/
/* r0 contain value et r1 address conversion area */
conversion10SP:
push {r1-r6,lr} @ save registers
mov r5,r1
mov r4,#8
mov r2,r0
mov r1,#10 @ conversion decimale
1: @ begin loop
mov r0,r2 @ copy number or quotients
bl division @ r0 dividende r1 divisor r2 quotient r3 remainder
add r3,#48 @ compute digit
strb r3,[r5,r4] @ store byte area address (r5) + offset (r4)
subs r4,r4,#1 @ position précedente
bge 1b @ and loop if not < zero
mov r0,#8
mov r3,#0
strb r3,[r5,r0] @ store 0 final
100:
pop {r1-r6,pc} @ restaur registers
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Difference between +20E0 and +45E0 = +25E0
Difference between -45E0 and +45E0 = +90E0
Difference between -85E0 and +90E0 = +175E0
Difference between -95E0 and +90E0 = -175E0
Difference between -45E0 and +125E0 = +170E0
Difference between -45E0 and +145E0 = -170E0
Difference between +29,48030089E0 and -88,63809964E0 = -118,1184082E0
Difference between -78,32510374E0 and -159,03599548E0 = -80,71087648E0
Difference between -70099,74218752E0 and +29840,67382809E0 = -139,58398438E0
 
</pre>
=={{header|Arturo}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="rebol">getDifference: function [b1, b2][
r: (b2 - b1) % 360.0
 
Line 338 ⟶ 1,506:
print getDifference neg 165313.6666297357 33693.9894517456
print getDifference 1174.8380510598456 neg 154146.66490124757
print getDifference 60175.77306795546 42213.07192354373</langsyntaxhighlight>
 
{{out}}
Line 361 ⟶ 1,529:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Angles:= [[20, 45]
,[-45, 45]
,[-85, 90]
Line 381 ⟶ 1,549:
Angle_difference_between_two_bearings(set){
return (diff := Mod(set.2, 360) - Mod(set.1, 360)) >180 ? diff-360 : diff
}</langsyntaxhighlight>
{{out}}
<pre>45 to 20 = 25
Line 397 ⟶ 1,565:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK
BEGIN {
Line 431 ⟶ 1,599:
return(r)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 451 ⟶ 1,619:
 
=={{header|BASIC}}==
==={{header|Chipmunk Basic}}===
{{trans|Phyton}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 cls
110 sub getdifference(b1,b2)
120 r = (b2-b1) mod 360
130 if r >= 180 then r = r-360
140 print using "#######.######";b1;
150 print using " #######.######";b2;
160 print using " #######.######";r
170 end sub
180 print "Input in -180 to +180 range:"
190 print " b1 b2 difference"
200 print " -------------------------------------------------"
210 getdifference(20,45)
220 getdifference(-45,45)
230 getdifference(-85,90)
240 getdifference(-95,90)
250 getdifference(-45,125)
260 getdifference(-45,145)
270 getdifference(-45,125)
280 getdifference(-45,145)
290 getdifference(29.4803,-88.6381)
300 getdifference(-78.3251,-159.036)
310 getdifference(-70099.742338,29840.674379)
320 getdifference(-165313.66663,33693.989452)
330 getdifference(1174.838051,-154146.664901)
340 print
350 print "Input in wider range:"
360 print " b1 b2 difference"
370 print " -------------------------------------------------"
380 getdifference(-70099.742338,29840.674379)
390 getdifference(-165313.66663,33693.989452)
400 getdifference(1174.838051,-154146.664901)
410 getdifference(60175.773068,42213.071924)</syntaxhighlight>
{{out}}
<pre>Input in -180 to +180 range:
b1 b2 difference
-------------------------------------------------
20.000000 45.000000 25.000000
-45.000000 45.000000 90.000000
-85.000000 90.000000 175.000000
-95.000000 90.000000 -175.000000
-45.000000 125.000000 170.000000
-45.000000 145.000000 -170.000000
-45.000000 125.000000 170.000000
-45.000000 145.000000 -170.000000
29.480300 -88.638100 -118.000000
-78.325100 -159.036000 -80.000000
-70099.742338 29840.674379 -140.000000
-165313.666630 33693.989452 -73.000000
1174.838051 -154146.664901 -161.000000
 
Input in wider range:
b1 b2 difference
-------------------------------------------------
-70099.742338 29840.674379 -140.000000
-165313.666630 33693.989452 -73.000000
1174.838051 -154146.664901 -161.000000
60175.773068 42213.071924 -322.000000</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<langsyntaxhighlight lang="qbasic">SUB getDifference (b1!, b2!)
r! = (b2 - b1) MOD 360!
IF r >= 180! THEN r = r - 360!
Line 473 ⟶ 1,703:
CALL getDifference(-70099.74233810938#, 29840.67437876723#)
CALL getDifference(-165313.6666297357#, 33693.9894517456#)
CALL getDifference(1174.838051059846#, -154146.6649012476#)</langsyntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">SUB getdifference (b1,b2)
LET r = REMAINDER(b2-b1, 360.0)
IF r >= 180.0 THEN LET r = r-360.0
Line 496 ⟶ 1,726:
CALL getdifference(-165313.6666297357, 33693.9894517456)
CALL getdifference(1174.8380510598456, -154146.66490124757)
END</langsyntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|Python}}
<langsyntaxhighlight BASIC256lang="basic256"># Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
# by Jjuanhdez, 06/2022
 
Line 525 ⟶ 1,755:
if r >= 180.0 then r -= 360.0
print ljust(b1,16); ljust(b2,16); ljust(r,12)
end subroutine</langsyntaxhighlight>
 
=={{header|Befunge}}==
<langsyntaxhighlight lang="befunge">012pv1 2 3 4 5 6 7 8
>&:v >859**%:459**1-`#v_ >12g!:12p#v_\-:459**1-`#v_ >.>
>0`#^_8v >859**-^ >859**-^
^:+**95< > ^
</syntaxhighlight>
</lang>
The labelled points are:
1. Initialise write/not write and read input,
Line 565 ⟶ 1,795:
25 90 175 -175 170 -170 -117 -81 -141 -74 -160 38
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Adiff ← 180 - 360 | 180 + -
 
tests ← [20‿45, ¯45‿45, ¯85‿90, ¯95‿90, ¯45‿125, ¯45‿145
99‿279, 29.4803‿¯88.6381, ¯78.3251‿¯159.036
¯70099.74233810938‿29840.67437876723
¯165313.6666297357‿33693.9894517456
1174.8380510598456‿¯154146.66490124757
60175.77306795546‿42213.07192354373]
 
Round ← ⌊∘+⟜0.5⌾(1e3⊸×)
 
Round∘∾⟜(Adiff´)˘ tests</syntaxhighlight>
{{out}}
<pre>┌─
╵ 20 45 25
¯45 45 90
¯85 90 175
¯95 90 ¯175
¯45 125 170
¯45 145 ¯170
99 279 180
29.48 ¯88.638 ¯118.118
¯78.325 ¯159.036 ¯80.711
¯70099.742 29840.674 ¯139.583
¯165313.667 33693.989 ¯72.344
1174.838 ¯154146.665 ¯161.503
60175.773 42213.072 37.299
┘</pre>
 
=={{header|C}}==
This implementation either reads two bearings from the console or a file containing a list of bearings. Usage printed on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 606 ⟶ 1,866:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output for two bearings :
<pre>
Line 652 ⟶ 1,912:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
 
namespace Angle_difference_between_two_bearings
Line 733 ⟶ 1,993:
}
}
}</langsyntaxhighlight>
{{out| Output}}
<pre>Hello World!
Line 753 ⟶ 2,013:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
using namespace std;
Line 787 ⟶ 2,047:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 808 ⟶ 2,068:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn angle-difference [a b]
(let [r (mod (- b a) 360)]
(if (>= r 180)
Line 819 ⟶ 2,079:
(angle-difference -95 90) ; -175
(angle-difference -70099.74 29840.67) ; -139.59
</syntaxhighlight>
</lang>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol">
******************************************************************
* COBOL solution to Angle difference challange
Line 972 ⟶ 2,232:
* 012 37.2989
******************************************************************
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="common lisp">
(defun angle-difference (b1 b2)
(let ((diff (mod (- b2 b1) 360)))
Line 983 ⟶ 2,243:
(decf diff 360)
diff))))
</syntaxhighlight>
</lang>
{{out| Output}}
<pre>
Line 999 ⟶ 2,259:
 
</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">precision 4
 
define s1 = 0, s2 = 0
 
dim b1[20, -45, -85, -95, -45, -45, 29.4803, -78.3251]
dim b2[45, 45, 90, 90, 125, 145, -88.6381, -159.036]
 
arraysize s1, b1
arraysize s2, b2
 
if s1 = s2 then
 
for i = 0 to s1 - 1
 
let r = (b2[i] - b1[i]) % 360
 
if r >= 180 then
 
let r = r - 360
 
endif
 
print "bearing 1: ", b1[i], " bearing 2: ", b2[i], " difference: ", r
 
next i
 
endif</syntaxhighlight>
{{out| Output}}
<pre>bearing 1: 20 bearing 2: 45 difference: 25
bearing 1: -45 bearing 2: 45 difference: 90
bearing 1: -85 bearing 2: 90 difference: 175
bearing 1: -95 bearing 2: 90 difference: -175
bearing 1: -45 bearing 2: 125 difference: 170
bearing 1: -45 bearing 2: 145 difference: -170
bearing 1: 29.4803 bearing 2: -88.6381 difference: -118
bearing 1: -78.3251 bearing 2: -159.0360 difference: -80</pre>
 
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
double getDifference(double b1, double b2) {
Line 1,033 ⟶ 2,331:
writeln(getDifference(1174.8380510598456, -154146.66490124757));
writeln(getDifference(60175.77306795546, 42213.07192354373));
}</langsyntaxhighlight>
 
{{out}}
Line 1,054 ⟶ 2,352:
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
func angdiff a b .
r = (b - a) mod 360
if r < -180
r += 360
elif r >= 180
r -= 360
.
return r
.
proc pd a b . .
print b & " " & a & " -> " & angdiff a b
.
pd 20 45
pd -45 45
pd -85 90
pd -95 90
pd -45 125
pd -45 145
pd 29.4803 -88.6381
pd -78.3251 -159.036
pd -70099.74233810938 29840.67437876723
pd -165313.6666297357 33693.9894517456
pd 1174.8380510598456 -154146.66490124757
pd 60175.77306795546 42213.07192354373
</syntaxhighlight>
 
=={{header|Erlang}}==
The real number calculations are done using integer arithmetic to better handle
Line 1,059 ⟶ 2,386:
The module is tested by running the test function, which in turn matches expected results
with the result of function call.
<syntaxhighlight lang="erlang">
<lang Erlang>
-module(bearings).
 
Line 1,130 ⟶ 2,457:
 
passed.
</syntaxhighlight>
</lang>
 
=={{header|Excel}}==
Line 1,140 ⟶ 2,467:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">ANGLEBETWEENBEARINGS
=LAMBDA(ab,
DEGREES(
Line 1,167 ⟶ 2,494:
) * ACOS((ax * bx) + (ay * by))
)
)</langsyntaxhighlight>
 
Using an Excel custom number format to display the degree symbol°
Line 1,283 ⟶ 2,610:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let deltaBearing (b1:double) (b2:double) =
let r = (b2 - b1) % 360.0;
if r > 180.0 then
Line 1,306 ⟶ 2,633:
printfn "%A" (deltaBearing 1174.8380510598456 -154146.66490124757)
printfn "%A" (deltaBearing 60175.77306795546 42213.07192354373)
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>25.0
Line 1,324 ⟶ 2,651:
{{trans|F#}}
{{works with|Factor|0.99 development release 2019-03-17}}
<langsyntaxhighlight lang="factor">USING: combinators generalizations kernel math prettyprint ;
IN: rosetta-code.bearings
 
Line 1,349 ⟶ 2,676:
[ delta-bearing . ] 2 12 mnapply ;
 
MAIN: bearings-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 1,368 ⟶ 2,695:
=={{header|Forth}}==
Developed with Gforth 0.7.9_20211014 using floating point math.
<langsyntaxhighlight lang="forth">
: Angle-Difference-stack ( b1 b2 - a +/-180)
\ Algorithm with stack manipulation without branches ( s. Frotran Groovy)
Line 1,392 ⟶ 2,719:
Angle-Difference-const f.
;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,418 ⟶ 2,745:
As it happens, the test data did not provoke any objections from the ACOSD function, but even so, a conversion to using ''arctan'' instead of ''arccos'' to recover angles would be safer. By using the four-quadrant ''arctan(x,y)'' function, the sign of the angle difference is also delivered and although that result could be in 0°-360° it turns out to be in ±180° as desired. On the other hand, the library of available functions did not include an arctan for complex parameters, so the complex number Z had to be split into its real and imaginary parts, thus requiring two appearances and to avoid repeated calculation, a temporary variable Z is needed. Otherwise, the statement could have been just <code>T = ATAN2D(Z1*CONJG(Z2))</code> and the whole calculation could be effected in one statement, <code>T = ATAN2D(CIS(90 - B1)*CONJG(CIS(90 - B2)))</code> And, since ''cis(t) = exp(i.t)'', <code>T = ATAN2D(EXP(CMPLX(0,90 - B1))*CONJG(EXP(CMPLX(0,90 - B2))))</code> - although using the arithmetic statement function does seem less intimidating.
 
The source style is F77 (even using the old-style arithmetic statement function) except for the convenience of generic functions taking the type of their parameters to save on the bother of DCMPLX instead of just CMPLX, etc. Floating-point constants in the test data are specified with ~D0, the exponential form that signifies double precision otherwise they would be taken as single precision values. Some compilers offer an option stating that all floating-point constants are to be taken as double precision. REAL*8 precision amounts to about sixteen decimal digits, so some of the supplied values will not be accurately represented, unless something beyond REAL*8 is available. <langsyntaxhighlight Fortranlang="fortran"> SUBROUTINE BDIFF (B1,B2) !Difference B2 - B1, as bearings. All in degrees, not radians.
REAL*8 B1,B2 !Maximum precision, for large-angle folding.
COMPLEX*16 CIS,Z1,Z2,Z !Scratchpads.
Line 1,448 ⟶ 2,775:
END DO
 
END</langsyntaxhighlight>
The output shows the stages:
<pre>
Line 1,469 ⟶ 2,796:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 28-01-2019
' compile with: fbc -s console
 
Line 1,517 ⟶ 2,844:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> b1 b2 difference
Line 1,533 ⟶ 2,860:
1174.8380510598461 -154146.6649012475973 -161.5029523074336
60175.7730679554588 42213.0719235437282 37.2988555882694</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn GetDifference( b1 as float, b2 as float )
float r = ( b2 - b1 ) mod 360.0
if r >= 180.0 then r = r - 360.0
printf @"%9.1f\u00B0 %10.1f\u00B0 = %7.1f\u00B0", b1, b2, r
end fn
 
printf @"Input in -180 to +180 range:"
printf @"-----------------------------------"
printf @"%9s %12s %15s", "b1", "b2", "distance"
printf @"-----------------------------------"
fn GetDifference( 20.0, 45.0 )
fn GetDifference( -45.0, 45.0 )
fn GetDifference( -85.0, 90.0 )
fn GetDifference( -95.0, 90.0 )
fn GetDifference( -45.0, 125.0 )
fn GetDifference( -45.0, 145.0 )
fn GetDifference( -45.0, 125.0 )
fn GetDifference( -45.0, 145.0 )
fn GetDifference( 29.4803, -88.6381 )
fn GetDifference( -78.3251, -159.036 )
fn GetDifference( -70099.74233810938, 29840.67437876723 )
fn GetDifference( -165313.6666297357, 33693.9894517456 )
fn GetDifference( 1174.8380510598456, -154146.66490124757 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
-----------------------------------
b1 b2 distance
-----------------------------------
20.0° 45.0° = 25.0°
-45.0° 45.0° = 90.0°
-85.0° 90.0° = 175.0°
-95.0° 90.0° = -175.0°
-45.0° 125.0° = 170.0°
-45.0° 145.0° = -170.0°
-45.0° 125.0° = 170.0°
-45.0° 145.0° = -170.0°
29.5° -88.6° = -118.1°
-78.3° -159.0° = -80.7°
-70099.7° 29840.7° = -139.6°
-165313.7° 33694.0° = -72.3°
1174.8° -154146.7° = -161.5°
</pre>
 
=={{header|Go}}==
Line 1,538 ⟶ 2,913:
 
One feature of this solution is that if you can rely on the input bearings being in the range -180 to 180, you don't have to use math.Mod. Another feature is the bearing type and method syntax.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,570 ⟶ 2,945:
return d
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,585 ⟶ 2,960:
 
A feature here is that the function body is a one-liner sufficient for the task test cases.
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,615 ⟶ 2,990:
func angleDifference(b2, b1 float64) float64 {
return math.Mod(math.Mod(b2-b1, 360)+360+180, 360) - 180
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,635 ⟶ 3,010:
'''Solution A:'''
{{trans|C++}}
<langsyntaxhighlight lang="groovy">def angleDifferenceA(double b1, double b2) {
r = (b2 - b1) % 360.0
(r > 180.0 ? r - 360.0
: r <= -180.0 ? r + 360.0
: r)
}</langsyntaxhighlight>
 
'''Solution B:'''<br>
In the spirit of the [[Angle_difference_between_two_bearings#Fortran|Fortran]] "Why branch when you can math?" solution, but without all the messy trigonometry.
<langsyntaxhighlight lang="groovy">def angleDifferenceB(double b1, double b2) {
((b2 - b1) % 360.0 - 540.0) % 360.0 + 180.0
}</langsyntaxhighlight>
NOTE: We could ADD 540 and SUBTRACT 180 instead (as did many others, notably [[Angle_difference_between_two_bearings#360_Assembly|360_Assembly]], [[Angle_difference_between_two_bearings#NewLISP|NewLISP]], [[Angle_difference_between_two_bearings#Racket|Racket]], and [[Angle_difference_between_two_bearings#REXX|REXX]]). The difference is that my choice normalizes "about face" to +180° while the other (At least in [https://rosettacode.org/wiki/Category:Groovy Groovy] and other [https://rosettacode.org/wiki/Category:C C]-derived languages) normalizes "about face" to -180°.
 
'''Test:'''
<langsyntaxhighlight lang="groovy">println " b1 b2 diff A diff B"
[
[b1: 20, b2: 45 ],
Line 1,667 ⟶ 3,042:
def (b1,b2) = bearings.values().collect{ it as double }
printf ("%22.13f %22.13f %22.13f %22.13f\n", b1, b2, angleDifferenceA(b1, b2), angleDifferenceB(b1, b2))
}</langsyntaxhighlight>
 
'''Output:'''
Line 1,685 ⟶ 3,060:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
Line 1,694 ⟶ 3,069:
 
---------- ANGLE DIFFERENCE BETWEEN TWO BEARINGS ---------
 
angleBetweenDegrees :: (Degrees, Degrees) -> Degrees
angleBetweenDegrees = degrees . bearingDelta . join bimap radians
 
bearingDelta :: (Radians, Radians) -> Radians
Line 1,709 ⟶ 3,081:
| otherwise = -1
 
angleBetweenDegrees :: (Degrees, Degrees) -> Degrees
angleBetweenDegrees =
degrees
. bearingDelta
. join bimap radians
 
--------------------------- TEST -------------------------
Line 1,732 ⟶ 3,109:
 
radians :: Degrees -> Radians
radians = (/ 180) . (pi *)</langsyntaxhighlight>
{{Out}}
<pre> 20.00° - 45.00° -> 25.00°
Line 1,742 ⟶ 3,119:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic">100 INPUT PROMPT "1. angle: ":A1
110 INPUT PROMPT "2. angle: ":A2
120 LET B=MOD(A2-A1,360)
130 IF B>180 THEN LET B=B-360
140 IF B<-180 THEN LET B=B+360
150 PRINT "Difference: ";B</langsyntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j">relativeBearing=: 180 - 360 | 180 + -
 
tests=: _99&".;._2 noun define
<lang j>relativeBearing=: (180 -~ 360 | 180 + -~)/"1</lang>
<lang j>tests=: _99&".;._2 noun define
20 45
-45 45
Line 1,765 ⟶ 3,142:
1174.8380510598456 -154146.66490124757
60175.77306795546 42213.07192354373
)</syntaxhighlight>
)
 
tests ,. relativeBearing tests
<pre> tests ,. relativeBearing/"1 tests
20 45 25
_45 45 90
Line 1,778 ⟶ 3,156:
_165314 33694 _72.3439
1174.84 _154147 _161.503
60175.8 42213.1 37.2989</langpre>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">public class AngleDifference {
 
public static double getDifference(double b1, double b2) {
Line 1,812 ⟶ 3,190:
System.out.println(getDifference(60175.77306795546, 42213.07192354373));
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,836 ⟶ 3,214:
This approach should be reliable but it is also very inefficient.
 
<langsyntaxhighlight lang="javascript">function relativeBearing(b1Rad, b2Rad)
{
b1y = Math.cos(b1Rad);
Line 1,870 ⟶ 3,248:
+relativeBearing(60175.77306795546*deg2rad, 42213.07192354373*deg2rad)*rad2deg+"\n";
 
}</langsyntaxhighlight>
 
{{out}}
Line 1,890 ⟶ 3,268:
===ES6===
 
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
"use strict";
 
Line 1,964 ⟶ 3,342:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,975 ⟶ 3,353:
 
=={{header|Jsish}}==
<langsyntaxhighlight lang="javascript">/* Angle difference between bearings, in Jsish */
function angleDifference(bearing1:number, bearing2:number):number {
var angle = (bearing2 - bearing1) % 360;
Line 2,014 ⟶ 3,392:
60175.7730679555° 42213.0719235437° 37.2988555882694°
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 2,025 ⟶ 3,403:
 
Note that the `%` operator in jq produces integral results.
<langsyntaxhighlight lang="jq"># Angles are in degrees; the result is rounded to 4 decimal places:
def subtract($b1; $b2):
10000 as $scale
Line 2,052 ⟶ 3,430:
| subtract($p0; $p1) as $diff
| (if $p0 < 0 then " " else " " end) as $offset
| "\($offset)\($p0) and \($p1) -> \($diff)" )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,090 ⟶ 3,468:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">using Printf
 
function angdiff(a, b)
Line 2,111 ⟶ 3,489:
(1174.8380510598456, -154146.66490124757), (60175.77306795546, 42213.07192354373)]
@printf("% 9.1f - % 9.1f = % 6.1f\n", a, b, angdiff(a, b))
end</langsyntaxhighlight>
 
{{out}}
Line 2,133 ⟶ 3,511:
 
=={{header|K}}==
<syntaxhighlight lang="k">
<lang K>
/ Angle difference between two angles
/ angledif.k
 
angdif: {[b1;b2]; :[(r:(b2-b1)!360.0)<-180.0;r+:360.0;r>180.0;r-:360.0];:r}
</syntaxhighlight>
</lang>
 
The output of a session is given below:
Line 2,168 ⟶ 3,546:
=={{header|Klingphix}}==
{{trans|NewLISP}}
<langsyntaxhighlight Klingphixlang="klingphix">include ..\Utilitys.tlhy
 
:bearing sub 360 mod 540 add 360 mod 180 sub ;
Line 2,187 ⟶ 3,565:
pstack
 
" " input</langsyntaxhighlight>
{{out}}
<pre>(-25, -90, -175, 175, -170, 170, 118.118, 80.7109, 139.583, 72.3439, 161.503, -37.2989)</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Angle(d: Double) {
Line 2,225 ⟶ 3,603:
println(f.format(ap.first, ap.second, diff.value))
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,248 ⟶ 3,626:
Each bearing will be stored in an object that inherits methods to accomplish all parts of the task: accept a new number of degrees, automatically adjusting to the range [-180, 180]; construct new bearing objects; subtract another bearing from itself and return the difference; construct a list of new bearing objects given a list of arbitrary degree sizes; and format the number of degrees into a modest human-readable format. Bearings will be zero-initialized by default if no degree size is provided.
 
<langsyntaxhighlight lang="lua">bearing = {degrees = 0} -- prototype object
 
function bearing:assign(angle)
Line 2,307 ⟶ 3,685:
end
 
main()</langsyntaxhighlight>
 
{{out}}
Line 2,325 ⟶ 3,703:
=={{header|Maple}}==
{{trans|C++}}
<langsyntaxhighlight Maplelang="maple">getDiff := proc(b1,b2)
local r:
r := frem(b2 - b1, 360):
Line 2,342 ⟶ 3,720:
getDiff(-165313.6666297357,33693.9894517456);
getDiff(1174.8380510598456,-154146.66490124757);
getDiff(60175.77306795546,42213.07192354373)</langsyntaxhighlight>
{{Out}}
<pre>25
Line 2,360 ⟶ 3,738:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[AngleDifference]
AngleDifference[b1_, b2_] := Mod[b2 - b1, 360, -180]
AngleDifference[20, 45]
Line 2,373 ⟶ 3,751:
AngleDifference[-165313.6666297357, 33693.9894517456]
AngleDifference[1174.8380510598456, -154146.66490124757]
AngleDifference[60175.77306795546, 42213.07192354373]</langsyntaxhighlight>
{{out}}
<pre>25
Line 2,390 ⟶ 3,768:
=={{header|Modula-2}}==
{{trans|Java}}
<langsyntaxhighlight lang="modula2">FROM Terminal IMPORT *;
 
PROCEDURE WriteRealLn(value : REAL);
Line 2,433 ⟶ 3,811:
 
ReadChar;
END Bearings.</langsyntaxhighlight>
 
=={{header|NewLISP}}==
Taken from Racket solution
 
<langsyntaxhighlight lang="lisp">
#!/usr/bin/env newlisp
(define (bearing- bearing heading) (sub (mod (add (mod (sub bearing heading) 360.0) 540.0) 360.0) 180.0))
Line 2,454 ⟶ 3,832:
(bearing- 1174.8380510598456 -154146.66490124757)
(bearing- 60175.77306795546 42213.07192354373))
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,473 ⟶ 3,851:
=={{header|Nim}}==
 
<langsyntaxhighlight lang="nim">import math
import strutils
 
Line 2,504 ⟶ 3,882:
vector.b2.formatFloat(ffDecimal, 2).align(13) &
delta(vector.b1, vector.b2).formatFloat(ffDecimal, 2).align(13)
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,520 ⟶ 3,898:
1174.84 -154146.66 -161.50
60175.77 42213.07 37.30
</pre>
 
=={{header|Nutt}}==
{{trans|Java}}
<syntaxhighlight lang="Nutt">
module main
imports native.io.output.say
 
funct get_difference(b1:Float,b2:Float):Float=
var r:Float=(b2-b1)%360.0
doif r< -180.0 r=r+360.0
doif r>=180.0 r=r-360.0
yield r
return
 
 
say("Input in -180 to +180 range")
say(get_difference(20.0,45.0))
say(get_difference(-45.0,45.0))
say(get_difference(-85.0,90.0))
say(get_difference(-95.0,90.0))
say(get_difference(-45.0,125.0))
say(get_difference(-45.0,145.0))
say(get_difference(-45.0,125.0))
say(get_difference(-45.0,145.0))
say(get_difference(29.4803,-88.6381))
say(get_difference(-78.3251,-159.036))
 
say("Input in wider range")
say(get_difference(-70099.74233810938,29840.67437876723))
say(get_difference(-165313.6666297357,33693.9894517456))
say(get_difference(1174.8380510598456,-154146.66490124757))
say(get_difference(60175.77306795546,42213.07192354373))
 
end
</syntaxhighlight>
 
{{output}}
<pre>
Input in -180 to +180 range
25.0
90.0
175.0
-175.0
170.0
-170.0
170.0
-170.0
-118.1184
-80.7109
Input in wider range
-139.58328312339
-72.3439185187
-161.5029523074156
37.29885558827
</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang="objeck">class AngleBearings {
function : Main(args : String[]) ~ Nil {
"Input in -180 to +180 range"->PrintLine();
Line 2,557 ⟶ 3,990:
return r;
}
}</langsyntaxhighlight>
 
{{output}}
Line 2,581 ⟶ 4,014:
=={{header|OCaml}}==
{{trans|D}}
<langsyntaxhighlight lang="ocaml">let get_diff b1 b2 =
let r = mod_float (b2 -. b1) 360.0 in
if r < -180.0
Line 2,607 ⟶ 4,040:
Printf.printf " %g\n" (get_diff 1174.8380510598456 (-154146.66490124757));
Printf.printf " %g\n" (get_diff 60175.77306795546 42213.07192354373);
;;</langsyntaxhighlight>
 
{{out}}
Line 2,628 ⟶ 4,061:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">centerliftmod1(x)=frac(x+1/2)-1/2;
anglediff(x,y)=centerliftmod1((y-x)/360)*360;
vecFunc(f)=v->call(f,v);
apply(vecFunc(anglediff), [[20,45], [-45,45], [-85,90], [-95,90], [-45,125], [-45,145], [29.4803,-88.6381], [-78.3251,-159.036], [-70099.74233810938,29840.67437876723], [-165313.6666297357,33693.9894517456], [1174.8380510598456,-154146.66490124757], [60175.77306795546,42213.07192354373]])</langsyntaxhighlight>
{{out}}
<pre>%1 = [25, 90, 175, -175, 170, -170, -118.11840000000000000000000000000000000, -80.710900000000000000000000000000000000, -139.58328312339000000000000000000000023, -72.343918518700000000000000000000000733, -161.50295230741560000000000000000000000, 37.298855588269999999999999999999999909]</pre>
Line 2,639 ⟶ 4,072:
This program is meant to be saved in the same folder as a file <code>angles.txt</code> containing the input. Each pair of angles to subtract appears on its own line in the input file.
 
<syntaxhighlight lang="pascal">
<lang Pascal>
Program Bearings;
{ Reads pairs of angles from a file and subtracts them }
Line 2,688 ⟶ 4,121:
close(angleFile)
End.
</syntaxhighlight>
</lang>
 
{{in}}
Line 2,721 ⟶ 4,154:
=={{header|Perl}}==
Perl's built-in modulo is integer-only, so import a suitable one from the <code>POSIX</code> core module
<langsyntaxhighlight lang="perl">use POSIX 'fmod';
 
sub angle {
Line 2,749 ⟶ 4,182:
printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, angle($b1,$b2);
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 20.00 45.00 = 25.00
Line 2,765 ⟶ 4,198:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">tz</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- trim trailing zeroes and decimal point</span>
Line 2,799 ⟶ 4,232:
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1174.8380510598456</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">154146.66490124757</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">test</span><span style="color: #0000FF;">(</span><span style="color: #000000;">60175.77306795546</span><span style="color: #0000FF;">,</span><span style="color: #000000;">42213.07192354373</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,819 ⟶ 4,252:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
( "16" 1 "16" 1 "16" ) var al
Line 2,846 ⟶ 4,279:
-165313.6666297357 33693.9894517456 test
1174.8380510598456 -154146.66490124757 test
60175.77306795546 42213.07192354373 test</langsyntaxhighlight>
{{out}}
<pre> b1 b2 diff
Line 2,869 ⟶ 4,302:
examples with ''Get-Examples''. There is also a full help file accessed by typing
''Get-Help Get-AngleDiff -full''
<syntaxhighlight lang="powershell">
<lang PowerShell>
<#
.Synopsis
Line 3,003 ⟶ 4,436:
 
 
}</langsyntaxhighlight>
 
'''Results of user inputs'''<br>
using -45 and 98 with ''Get-AngleDiff'':
<langsyntaxhighlight PowerShelllang="powershell">PS C:\WINDOWS\system32> Get-AngleDiff -45 98
The difference between -45 and 98 is 143</langsyntaxhighlight>
 
'''Results of ''Get-Examples'''''
Line 3,030 ⟶ 4,463:
The difference between 60175.7730679555 and 42213.0719235437 is 37.2988555882694
</pre>
 
=={{header|PureBasic}}==
{{trans|Python}}
<syntaxhighlight lang="purebasic">Procedure.f getDifference (b1.f, b2.f)
r.f = Mod((b2 - b1), 360)
If r >= 180: r - 360
EndIf
PrintN(StrF(b1) + #TAB$ + StrF(b2) + #TAB$ + StrF(r));
EndProcedure
 
If OpenConsole()
PrintN("Input in -180 to +180 range:")
getDifference(20.0, 45.0)
getDifference(-45.0, 45.0)
getDifference(-85.0, 90.0)
getDifference(-95.0, 90.0)
getDifference(-45.0, 125.0)
getDifference(-45.0, 145.0)
getDifference(-45.0, 125.0)
getDifference(-45.0, 145.0)
getDifference(29.4803, -88.6381)
getDifference(-78.3251, -159.036)
PrintN(#CRLF$ + "Input in wider range:")
getDifference(-70099.74233810938, 29840.67437876723)
getDifference(-165313.6666297357, 33693.9894517456)
getDifference(1174.8380510598456, -154146.66490124757)
getDifference(60175.77306795546, 42213.07192354373)
Repeat: Until Inkey() <> ""
EndIf</syntaxhighlight>
 
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang="python">from __future__ import print_function
def getDifference(b1, b2):
Line 3,060 ⟶ 4,522:
print (getDifference(-165313.6666297357, 33693.9894517456))
print (getDifference(1174.8380510598456, -154146.66490124757))
print (getDifference(60175.77306795546, 42213.07192354373))</langsyntaxhighlight>
 
{{out}}
Line 3,083 ⟶ 4,545:
Or, generalising a little by deriving the degrees from a (Radians -> Radians) function, and formatting the output in columns:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Difference between two bearings'''
 
from math import (acos, cos, pi, sin)
Line 3,171 ⟶ 4,633:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Difference between two bearings:
Line 3,189 ⟶ 4,651:
The table output use print formatting only available from python 3. Tested with 3.6.
{{Works with|Python|3.6}}
<langsyntaxhighlight lang="python">"""
Difference between two bearings
"""
Line 3,205 ⟶ 4,667:
print('.{:-^19}.{:-^19}.{:-^9}.' .format(" b1 ", " b2 ", " Δ b " ))
for Δ in dataSet:
print('|{: > 19}|{: > 19}|{: > 9.4f}|' .format(Δ[0], Δ[1],delta_bearing(Δ[0],Δ[1])))</langsyntaxhighlight>
{{Out}}
<pre>.------- b1 --------.------- b2 --------.-- Δ b --.
Line 3,225 ⟶ 4,687:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ v- -v
Line 3,254 ⟶ 4,716:
$->v drop
angledelta
20 point$ echo$ cr ]</langsyntaxhighlight>
 
{{out}}
Line 3,275 ⟶ 4,737:
''see my comments in discussion regards bearing-heading or vice versa''
 
<langsyntaxhighlight lang="racket">#lang racket
(define (% a b) (- a (* b (truncate (/ a b)))))
 
Line 3,302 ⟶ 4,764:
(check-equal? (% 17.5 10) 7.5)
(check-equal? (% -7.5 10) -7.5)
(check-equal? (% -17.5 10) -7.5))</langsyntaxhighlight>
 
{{out}}
Line 3,322 ⟶ 4,784:
{{works with|Rakudo|2016.11}}
 
<syntaxhighlight lang="raku" perl6line>sub infix:<∠> (Real $b1, Real $b2) {
(my $b = ($b2 - $b1 + 720) % 360) > 180 ?? $b - 360 !! $b;
}
Line 3,339 ⟶ 4,801:
60175.77306795546, 42213.07192354373
 
-> $b1, $b2 { printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, $b1 ∠ $b2 }</langsyntaxhighlight>
{{out}}
<pre> 20.00 45.00 = 25.00
Line 3,356 ⟶ 4,818:
=={{header|REXX}}==
Some extra coding was added for a better visual presentation; &nbsp; the angles were centered, &nbsp; the answers were aligned.
<langsyntaxhighlight lang="rexx">/*REXX pgm calculates difference between two angles (in degrees), normalizes the result.*/
numeric digits 25 /*use enough dec. digits for angles*/
call show 20, 45 /*display angular difference (deg).*/
Line 3,376 ⟶ 4,838:
if pos(., x)\==0 then x=strip( strip(x, 'T', 0), "T", .) /*strip trailing chaff.*/
say center(a || $, d) '─' center(b || $, d) " ────► " x || $
return /* [↑] display the angular difference.*/</langsyntaxhighlight>
{{out|output}}
<pre>
Line 3,394 ⟶ 4,856:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Angle difference between two bearings
 
Line 3,416 ⟶ 4,878:
end
return r
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,430 ⟶ 4,892:
-118.1184
-80.7109
</pre>
 
=={{header|RPL}}==
≪ SWAP - 360 MOD
'''IF''' DUP 180 > '''THEN''' 360 - '''END''' ≫ 'BDIFF' STO
{{in}}
<pre>
20 45 BDIFF
-45 45 BDIFF
-85 90 BDIFF
-95 90 BDIFF
-45 125 BDIFF
-45 145 BDIFF
29.4803 -88.6381 BDIFF
-78.3251 -159.036 BDIFF
</pre>
{{out}}
<pre>
8: 25
7: 90
6 175
5: -175
4: 170
3: -170
2: -118.1184
1: -80.7109
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<langsyntaxhighlight lang="ruby">def getDifference(b1, b2)
r = (b2 - b1) % 360.0
# Ruby modulus has same sign as divisor, which is positive here,
Line 3,462 ⟶ 4,950:
puts getDifference(1174.8380510598456, -154146.66490124757)
puts getDifference(60175.77306795546, 42213.07192354373)
end</langsyntaxhighlight>
 
{{out}}
Line 3,483 ⟶ 4,971:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">sub getDifference b1, b2
r = (b2 - b1) mod 360
if r >= 180 then r = r - 360
Line 3,503 ⟶ 4,991:
call getDifference -70099.74233810938, 29840.67437876723
call getDifference -165313.6666297357, 33693.9894517456
call getDifference 1174.8380510598456, -154146.66490124757</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
/// Calculate difference between two bearings, in -180 to 180 degrees range
pub fn angle_difference(bearing1: f64, bearing2: f64) -> f64 {
Line 3,546 ⟶ 5,034:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/lH5eUix/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/IgQSmzcjSpSMxvWWoZUc9w Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object AngleDifference extends App {
private def getDifference(b1: Double, b2: Double) = {
val r = (b2 - b1) % 360.0
Line 3,574 ⟶ 5,062:
println(getDifference(60175.77306795546, 42213.07192354373))
 
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
R6RS standard.
<langsyntaxhighlight lang="scheme">#!r6rs
 
(import (rnrs base (6))
Line 3,608 ⟶ 5,096:
(display (apply bearing-difference argument-list))
(newline))
test-cases))</langsyntaxhighlight>
 
{{out}}
Line 3,631 ⟶ 5,119:
which can be used to solve this task easily.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 3,662 ⟶ 5,150:
writeln(getDifference(1174.8380510598456, -154146.66490124757));
writeln(getDifference(60175.77306795546, 42213.07192354373));
end func;</langsyntaxhighlight>
 
{{out}}
Line 3,685 ⟶ 5,173:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func bearingAngleDiff(b1, b2) {
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b
}
Line 3,709 ⟶ 5,197:
) {
printf("%25s %25s %25s\n", b1, b2, bearingAngleDiff(b1, b2))
}</langsyntaxhighlight>
{{out}}
<pre> B1 B2 Difference
Line 3,728 ⟶ 5,216:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">func angleDifference(a1: Double, a2: Double) -> Double {
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360)
 
Line 3,755 ⟶ 5,243:
]
 
print(testCases.map(angleDifference))</langsyntaxhighlight>
 
{{out}}
Line 3,762 ⟶ 5,250:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">
proc angleDiff {b1 b2} {
set angle [::tcl::mathfunc::fmod [expr ($b2 - $b1)] 360]
Line 3,791 ⟶ 5,279:
puts [angleDiff 1174.8380510598456 -154146.66490124757]
puts [angleDiff 60175.77306795546 42213.07192354373]
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,813 ⟶ 5,301:
 
=={{header|VBA}}==
{{trans|Phix}}<langsyntaxhighlight lang="vb">Private Function tx(a As Variant) As String
Dim s As String
s = CStr(Format(a, "0.######"))
Line 3,846 ⟶ 5,334:
test 1174.83805105985, -154146.664901248
test 60175.7730679555, 42213.0719235437
End Sub</langsyntaxhighlight>{{out}}
<pre> b1 b2 diff
---------------- ---------------- ----------------
Line 3,864 ⟶ 5,352:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
 
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal
Line 3,914 ⟶ 5,402:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>25
Line 3,929 ⟶ 5,417:
37.2988555882</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math
type Bearing = f64
Line 3,968 ⟶ 5,456:
fn angle_difference(b2 Bearing, b1 Bearing) Bearing {
return math.mod(math.mod(b2-b1, 360)+360+180, 360) - 180
}</langsyntaxhighlight>
 
{{out}}
Line 3,991 ⟶ 5,479:
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">var subtract = Fn.new { |b1, b2|
var d = (b2 - b1) % 360
if (d < -180) d = d + 360
Line 4,020 ⟶ 5,508:
var offset = (p0 < 0) ? " " : " "
System.print("%(offset)%(p0) and %(p1) -> %(diff)")
}</langsyntaxhighlight>
 
{{out}}
Line 4,040 ⟶ 5,528:
 
=={{header|XBS}}==
<langsyntaxhighlight lang="xbs">settype Bearing = {Angle:number}
class Bearing {
private method construct(Angle:number=0)
Line 4,070 ⟶ 5,558:
set b2:Bearing=new Bearing(v[1]);
log(b2::ToString()+" - "+b1::ToString()+" = "+(b2-b1)::ToString());
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,090 ⟶ 5,578:
Pairs of bearing angles are input from the console or from a file
(terminated with Ctrl+C) redirected on the command line.
<langsyntaxhighlight XPL0lang="xpl0">real B1, B2, Ang;
[Text(0, " Bearing 1 Bearing 2 Difference");
loop [B1:= RlIn(1);
Line 4,102 ⟶ 5,590:
RlOut(0, Ang);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,123 ⟶ 5,611:
=={{header|Yabasic}}==
{{trans|Python}}
<langsyntaxhighlight lang="freebasic">// Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
// by Jjuanhdez, 06/2022
 
Line 4,147 ⟶ 5,635:
if r >= 180.0 r = r - 360.0
print r
end sub</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="zkl">fcn bearingAngleDiff(b1,b2){ // -->Float, b1,b2 can be int or float
( (b:=(0.0 + b2 - b1 + 720)%360) > 180 ) and b - 360 or b;
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">T( 20,45, -45,45, -85,90, -95,90, -45,125, -45,145 )
.pump(Console.println,Void.Read,
fcn(b1,b2){ "%.1f\UB0; + %.1f\UB0; = %.1f\UB0;"
.fmt(b1,b2,bearingAngleDiff(b1,b2)) });</langsyntaxhighlight>
{{out}}
<pre>
3,021

edits