Angle difference between two bearings: Difference between revisions
Content added Content deleted
(add task to aarch64 assembly raspberry pi) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 34: | Line 34: | ||
=={{header|11l}}== |
=={{header|11l}}== |
||
< |
<syntaxhighlight lang=11l>F get_difference(b1, b2) |
||
R wrap(b2 - b1, -180.0, 180.0) |
R wrap(b2 - b1, -180.0, 180.0) |
||
Line 51: | Line 51: | ||
print(get_difference(-165313.6666297357, 33693.9894517456)) |
print(get_difference(-165313.6666297357, 33693.9894517456)) |
||
print(get_difference(1174.8380510598456, -154146.66490124757)) |
print(get_difference(1174.8380510598456, -154146.66490124757)) |
||
print(get_difference(60175.77306795546, 42213.07192354373))</ |
print(get_difference(60175.77306795546, 42213.07192354373))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 74: | Line 74: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
{{trans|Rexx}} |
{{trans|Rexx}} |
||
< |
<syntaxhighlight lang=360asm>* Angle difference between two bearings - 06/06/2018 |
||
ANGLEDBB CSECT |
ANGLEDBB CSECT |
||
USING ANGLEDBB,R13 base register |
USING ANGLEDBB,R13 base register |
||
Line 120: | Line 120: | ||
XDEC DS CL12 temp |
XDEC DS CL12 temp |
||
YREGS |
YREGS |
||
END ANGLEDBB</ |
END ANGLEDBB</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 134: | Line 134: | ||
=={{header|AArch64 Assembly}}== |
=={{header|AArch64 Assembly}}== |
||
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }} |
{{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 */ |
/* ARM assembly AARCH64 Raspberry PI 3B */ |
||
/* program diffAngle64.s */ |
/* program diffAngle64.s */ |
||
Line 705: | Line 705: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../includeARM64.inc" |
.include "../includeARM64.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Difference between +20 and +45 = +25 |
Difference between +20 and +45 = +25 |
||
Line 720: | Line 720: | ||
{{libheader|Action! Tool Kit}} |
{{libheader|Action! Tool Kit}} |
||
{{libheader|Action! Real Math}} |
{{libheader|Action! Real Math}} |
||
< |
<syntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT" |
||
INT FUNC AngleI(INT a1,a2) |
INT FUNC AngleI(INT a1,a2) |
||
Line 778: | Line 778: | ||
TestR("1174.8380510598456","-154146.66490124757") |
TestR("1174.8380510598456","-154146.66490124757") |
||
TestR("60175.77306795546","42213.07192354373") |
TestR("60175.77306795546","42213.07192354373") |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Angle_difference_between_two_bearings.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Angle_difference_between_two_bearings.png Screenshot from Atari 8-bit computer] |
||
Line 798: | Line 798: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
Ada does not provide a built-in mod function for floating point types. This program supplies one. |
Ada does not provide a built-in mod function for floating point types. This program supplies one. |
||
< |
<syntaxhighlight lang=Ada> |
||
----------------------------------------------------------------------- |
----------------------------------------------------------------------- |
||
-- Angle difference between two bearings |
-- Angle difference between two bearings |
||
Line 850: | Line 850: | ||
end Bearing_Angles; |
end Bearing_Angles; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 868: | Line 868: | ||
=={{header|APL}}== |
=={{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. |
Returns an angle in (-180,180]; so two opposite bearings have a difference of 180 degrees, which is more natural than -180 degrees. |
||
< |
<syntaxhighlight lang=APL>[0] D←B1 DIFF B2 |
||
[1] D←180+¯360|180+B2-B1 |
[1] D←180+¯360|180+B2-B1 |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 'B1' 'B2' 'DIFFERENCE'⍪(⊂'¯¯¯¯¯¯¯¯¯¯')⍪(⊃B),DIFF/¨B |
<pre> 'B1' 'B2' 'DIFFERENCE'⍪(⊂'¯¯¯¯¯¯¯¯¯¯')⍪(⊃B),DIFF/¨B |
||
Line 895: | Line 895: | ||
=={{header|ARM Assembly}}== |
=={{header|ARM Assembly}}== |
||
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}} |
{{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 */ |
/* ARM assembly Raspberry PI or android with termux */ |
||
/* program diffAngle.s */ |
/* program diffAngle.s */ |
||
Line 1,403: | Line 1,403: | ||
/***************************************************/ |
/***************************************************/ |
||
.include "../affichage.inc" |
.include "../affichage.inc" |
||
</syntaxhighlight> |
|||
</lang> |
|||
<pre> |
<pre> |
||
Difference between +20E0 and +45E0 = +25E0 |
Difference between +20E0 and +45E0 = +25E0 |
||
Line 1,418: | Line 1,418: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang=rebol>getDifference: function [b1, b2][ |
||
r: (b2 - b1) % 360.0 |
r: (b2 - b1) % 360.0 |
||
Line 1,444: | Line 1,444: | ||
print getDifference neg 165313.6666297357 33693.9894517456 |
print getDifference neg 165313.6666297357 33693.9894517456 |
||
print getDifference 1174.8380510598456 neg 154146.66490124757 |
print getDifference 1174.8380510598456 neg 154146.66490124757 |
||
print getDifference 60175.77306795546 42213.07192354373</ |
print getDifference 60175.77306795546 42213.07192354373</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,467: | Line 1,467: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
< |
<syntaxhighlight lang=AutoHotkey>Angles:= [[20, 45] |
||
,[-45, 45] |
,[-45, 45] |
||
,[-85, 90] |
,[-85, 90] |
||
Line 1,487: | Line 1,487: | ||
Angle_difference_between_two_bearings(set){ |
Angle_difference_between_two_bearings(set){ |
||
return (diff := Mod(set.2, 360) - Mod(set.1, 360)) >180 ? diff-360 : diff |
return (diff := Mod(set.2, 360) - Mod(set.1, 360)) >180 ? diff-360 : diff |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>45 to 20 = 25 |
<pre>45 to 20 = 25 |
||
Line 1,503: | Line 1,503: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
< |
<syntaxhighlight lang=AWK> |
||
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK |
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 1,537: | Line 1,537: | ||
return(r) |
return(r) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,560: | Line 1,560: | ||
{{works with|QBasic|1.1}} |
{{works with|QBasic|1.1}} |
||
{{works with|QuickBasic|4.5}} |
{{works with|QuickBasic|4.5}} |
||
< |
<syntaxhighlight lang=qbasic>SUB getDifference (b1!, b2!) |
||
r! = (b2 - b1) MOD 360! |
r! = (b2 - b1) MOD 360! |
||
IF r >= 180! THEN r = r - 360! |
IF r >= 180! THEN r = r - 360! |
||
Line 1,579: | Line 1,579: | ||
CALL getDifference(-70099.74233810938#, 29840.67437876723#) |
CALL getDifference(-70099.74233810938#, 29840.67437876723#) |
||
CALL getDifference(-165313.6666297357#, 33693.9894517456#) |
CALL getDifference(-165313.6666297357#, 33693.9894517456#) |
||
CALL getDifference(1174.838051059846#, -154146.6649012476#)</ |
CALL getDifference(1174.838051059846#, -154146.6649012476#)</syntaxhighlight> |
||
==={{header|True BASIC}}=== |
==={{header|True BASIC}}=== |
||
< |
<syntaxhighlight lang=qbasic>SUB getdifference (b1,b2) |
||
LET r = REMAINDER(b2-b1, 360.0) |
LET r = REMAINDER(b2-b1, 360.0) |
||
IF r >= 180.0 THEN LET r = r-360.0 |
IF r >= 180.0 THEN LET r = r-360.0 |
||
Line 1,602: | Line 1,602: | ||
CALL getdifference(-165313.6666297357, 33693.9894517456) |
CALL getdifference(-165313.6666297357, 33693.9894517456) |
||
CALL getdifference(1174.8380510598456, -154146.66490124757) |
CALL getdifference(1174.8380510598456, -154146.66490124757) |
||
END</ |
END</syntaxhighlight> |
||
=={{header|BASIC256}}== |
=={{header|BASIC256}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=BASIC256># Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings |
||
# by Jjuanhdez, 06/2022 |
# by Jjuanhdez, 06/2022 |
||
Line 1,631: | Line 1,631: | ||
if r >= 180.0 then r -= 360.0 |
if r >= 180.0 then r -= 360.0 |
||
print ljust(b1,16); ljust(b2,16); ljust(r,12) |
print ljust(b1,16); ljust(b2,16); ljust(r,12) |
||
end subroutine</ |
end subroutine</syntaxhighlight> |
||
=={{header|Befunge}}== |
=={{header|Befunge}}== |
||
< |
<syntaxhighlight lang=befunge>012pv1 2 3 4 5 6 7 8 |
||
>&:v >859**%:459**1-`#v_ >12g!:12p#v_\-:459**1-`#v_ >.> |
>&:v >859**%:459**1-`#v_ >12g!:12p#v_\-:459**1-`#v_ >.> |
||
>0`#^_8v >859**-^ >859**-^ |
>0`#^_8v >859**-^ >859**-^ |
||
^:+**95< > ^ |
^:+**95< > ^ |
||
</syntaxhighlight> |
|||
</lang> |
|||
The labelled points are: |
The labelled points are: |
||
1. Initialise write/not write and read input, |
1. Initialise write/not write and read input, |
||
Line 1,674: | Line 1,674: | ||
=={{header|C}}== |
=={{header|C}}== |
||
This implementation either reads two bearings from the console or a file containing a list of bearings. Usage printed on incorrect invocation. |
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<stdlib.h> |
||
#include<stdio.h> |
#include<stdio.h> |
||
Line 1,712: | Line 1,712: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Invocation and output for two bearings : |
Invocation and output for two bearings : |
||
<pre> |
<pre> |
||
Line 1,758: | Line 1,758: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang=csharp>using System; |
||
namespace Angle_difference_between_two_bearings |
namespace Angle_difference_between_two_bearings |
||
Line 1,839: | Line 1,839: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out| Output}} |
{{out| Output}} |
||
<pre>Hello World! |
<pre>Hello World! |
||
Line 1,859: | Line 1,859: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang=cpp>#include <cmath> |
||
#include <iostream> |
#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 1,893: | Line 1,893: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,914: | Line 1,914: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang=clojure>(defn angle-difference [a b] |
||
(let [r (mod (- b a) 360)] |
(let [r (mod (- b a) 360)] |
||
(if (>= r 180) |
(if (>= r 180) |
||
Line 1,925: | Line 1,925: | ||
(angle-difference -95 90) ; -175 |
(angle-difference -95 90) ; -175 |
||
(angle-difference -70099.74 29840.67) ; -139.59 |
(angle-difference -70099.74 29840.67) ; -139.59 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang=cobol> |
||
****************************************************************** |
****************************************************************** |
||
* COBOL solution to Angle difference challange |
* COBOL solution to Angle difference challange |
||
Line 2,078: | Line 2,078: | ||
* 012 37.2989 |
* 012 37.2989 |
||
****************************************************************** |
****************************************************************** |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang=common lisp> |
||
(defun angle-difference (b1 b2) |
(defun angle-difference (b1 b2) |
||
(let ((diff (mod (- b2 b1) 360))) |
(let ((diff (mod (- b2 b1) 360))) |
||
Line 2,089: | Line 2,089: | ||
(decf diff 360) |
(decf diff 360) |
||
diff)))) |
diff)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out| Output}} |
{{out| Output}} |
||
<pre> |
<pre> |
||
Line 2,108: | Line 2,108: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=D>import std.stdio; |
||
double getDifference(double b1, double b2) { |
double getDifference(double b1, double b2) { |
||
Line 2,139: | Line 2,139: | ||
writeln(getDifference(1174.8380510598456, -154146.66490124757)); |
writeln(getDifference(1174.8380510598456, -154146.66490124757)); |
||
writeln(getDifference(60175.77306795546, 42213.07192354373)); |
writeln(getDifference(60175.77306795546, 42213.07192354373)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,165: | Line 2,165: | ||
The module is tested by running the test function, which in turn matches expected results |
The module is tested by running the test function, which in turn matches expected results |
||
with the result of function call. |
with the result of function call. |
||
< |
<syntaxhighlight lang=Erlang> |
||
-module(bearings). |
-module(bearings). |
||
Line 2,236: | Line 2,236: | ||
passed. |
passed. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Excel}}== |
=={{header|Excel}}== |
||
Line 2,246: | Line 2,246: | ||
{{Works with|Office 365 betas 2021}} |
{{Works with|Office 365 betas 2021}} |
||
< |
<syntaxhighlight lang=lisp>ANGLEBETWEENBEARINGS |
||
=LAMBDA(ab, |
=LAMBDA(ab, |
||
DEGREES( |
DEGREES( |
||
Line 2,273: | Line 2,273: | ||
) * ACOS((ax * bx) + (ay * by)) |
) * ACOS((ax * bx) + (ay * by)) |
||
) |
) |
||
)</ |
)</syntaxhighlight> |
||
Using an Excel custom number format to display the degree symbol° |
Using an Excel custom number format to display the degree symbol° |
||
Line 2,389: | Line 2,389: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang=fsharp>let deltaBearing (b1:double) (b2:double) = |
||
let r = (b2 - b1) % 360.0; |
let r = (b2 - b1) % 360.0; |
||
if r > 180.0 then |
if r > 180.0 then |
||
Line 2,412: | Line 2,412: | ||
printfn "%A" (deltaBearing 1174.8380510598456 -154146.66490124757) |
printfn "%A" (deltaBearing 1174.8380510598456 -154146.66490124757) |
||
printfn "%A" (deltaBearing 60175.77306795546 42213.07192354373) |
printfn "%A" (deltaBearing 60175.77306795546 42213.07192354373) |
||
0 // return an integer exit code</ |
0 // return an integer exit code</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>25.0 |
<pre>25.0 |
||
Line 2,430: | Line 2,430: | ||
{{trans|F#}} |
{{trans|F#}} |
||
{{works with|Factor|0.99 development release 2019-03-17}} |
{{works with|Factor|0.99 development release 2019-03-17}} |
||
< |
<syntaxhighlight lang=factor>USING: combinators generalizations kernel math prettyprint ; |
||
IN: rosetta-code.bearings |
IN: rosetta-code.bearings |
||
Line 2,455: | Line 2,455: | ||
[ delta-bearing . ] 2 12 mnapply ; |
[ delta-bearing . ] 2 12 mnapply ; |
||
MAIN: bearings-demo</ |
MAIN: bearings-demo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,474: | Line 2,474: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Developed with Gforth 0.7.9_20211014 using floating point math. |
Developed with Gforth 0.7.9_20211014 using floating point math. |
||
< |
<syntaxhighlight lang=forth> |
||
: Angle-Difference-stack ( b1 b2 - a +/-180) |
: Angle-Difference-stack ( b1 b2 - a +/-180) |
||
\ Algorithm with stack manipulation without branches ( s. Frotran Groovy) |
\ Algorithm with stack manipulation without branches ( s. Frotran Groovy) |
||
Line 2,498: | Line 2,498: | ||
Angle-Difference-const f. |
Angle-Difference-const f. |
||
; |
; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,524: | Line 2,524: | ||
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. |
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. < |
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. <syntaxhighlight lang=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. |
REAL*8 B1,B2 !Maximum precision, for large-angle folding. |
||
COMPLEX*16 CIS,Z1,Z2,Z !Scratchpads. |
COMPLEX*16 CIS,Z1,Z2,Z !Scratchpads. |
||
Line 2,554: | Line 2,554: | ||
END DO |
END DO |
||
END</ |
END</syntaxhighlight> |
||
The output shows the stages: |
The output shows the stages: |
||
<pre> |
<pre> |
||
Line 2,575: | Line 2,575: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang=freebasic>' version 28-01-2019 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 2,623: | Line 2,623: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> b1 b2 difference |
<pre> b1 b2 difference |
||
Line 2,644: | Line 2,644: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=go>package main |
||
import "fmt" |
import "fmt" |
||
Line 2,676: | Line 2,676: | ||
return d |
return d |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,691: | Line 2,691: | ||
A feature here is that the function body is a one-liner sufficient for the task test cases. |
A feature here is that the function body is a one-liner sufficient for the task test cases. |
||
< |
<syntaxhighlight lang=go>package main |
||
import ( |
import ( |
||
Line 2,721: | Line 2,721: | ||
func angleDifference(b2, b1 float64) float64 { |
func angleDifference(b2, b1 float64) float64 { |
||
return math.Mod(math.Mod(b2-b1, 360)+360+180, 360) - 180 |
return math.Mod(math.Mod(b2-b1, 360)+360+180, 360) - 180 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,741: | Line 2,741: | ||
'''Solution A:''' |
'''Solution A:''' |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=groovy>def angleDifferenceA(double b1, double b2) { |
||
r = (b2 - b1) % 360.0 |
r = (b2 - b1) % 360.0 |
||
(r > 180.0 ? r - 360.0 |
(r > 180.0 ? r - 360.0 |
||
: r <= -180.0 ? r + 360.0 |
: r <= -180.0 ? r + 360.0 |
||
: r) |
: r) |
||
}</ |
}</syntaxhighlight> |
||
'''Solution B:'''<br> |
'''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. |
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. |
||
< |
<syntaxhighlight lang=groovy>def angleDifferenceB(double b1, double b2) { |
||
((b2 - b1) % 360.0 - 540.0) % 360.0 + 180.0 |
((b2 - b1) % 360.0 - 540.0) % 360.0 + 180.0 |
||
}</ |
}</syntaxhighlight> |
||
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°. |
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:''' |
'''Test:''' |
||
< |
<syntaxhighlight lang=groovy>println " b1 b2 diff A diff B" |
||
[ |
[ |
||
[b1: 20, b2: 45 ], |
[b1: 20, b2: 45 ], |
||
Line 2,773: | Line 2,773: | ||
def (b1,b2) = bearings.values().collect{ it as double } |
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)) |
printf ("%22.13f %22.13f %22.13f %22.13f\n", b1, b2, angleDifferenceA(b1, b2), angleDifferenceB(b1, b2)) |
||
}</ |
}</syntaxhighlight> |
||
'''Output:''' |
'''Output:''' |
||
Line 2,791: | Line 2,791: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang=Haskell>import Control.Monad (join) |
||
import Data.Bifunctor (bimap) |
import Data.Bifunctor (bimap) |
||
import Text.Printf (printf) |
import Text.Printf (printf) |
||
Line 2,840: | Line 2,840: | ||
radians :: Degrees -> Radians |
radians :: Degrees -> Radians |
||
radians = (/ 180) . (pi *)</ |
radians = (/ 180) . (pi *)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> 20.00° - 45.00° -> 25.00° |
<pre> 20.00° - 45.00° -> 25.00° |
||
Line 2,850: | Line 2,850: | ||
=={{header|IS-BASIC}}== |
=={{header|IS-BASIC}}== |
||
< |
<syntaxhighlight lang=IS-BASIC>100 INPUT PROMPT "1. angle: ":A1 |
||
110 INPUT PROMPT "2. angle: ":A2 |
110 INPUT PROMPT "2. angle: ":A2 |
||
120 LET B=MOD(A2-A1,360) |
120 LET B=MOD(A2-A1,360) |
||
130 IF B>180 THEN LET B=B-360 |
130 IF B>180 THEN LET B=B-360 |
||
140 IF B<-180 THEN LET B=B+360 |
140 IF B<-180 THEN LET B=B+360 |
||
150 PRINT "Difference: ";B</ |
150 PRINT "Difference: ";B</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang=j>relativeBearing=: (180 -~ 360 | 180 + -~)/"1</syntaxhighlight> |
||
< |
<syntaxhighlight lang=j>tests=: _99&".;._2 noun define |
||
20 45 |
20 45 |
||
-45 45 |
-45 45 |
||
Line 2,886: | Line 2,886: | ||
_165314 33694 _72.3439 |
_165314 33694 _72.3439 |
||
1174.84 _154147 _161.503 |
1174.84 _154147 _161.503 |
||
60175.8 42213.1 37.2989</ |
60175.8 42213.1 37.2989</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=java>public class AngleDifference { |
||
public static double getDifference(double b1, double b2) { |
public static double getDifference(double b1, double b2) { |
||
Line 2,920: | Line 2,920: | ||
System.out.println(getDifference(60175.77306795546, 42213.07192354373)); |
System.out.println(getDifference(60175.77306795546, 42213.07192354373)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,944: | Line 2,944: | ||
This approach should be reliable but it is also very inefficient. |
This approach should be reliable but it is also very inefficient. |
||
< |
<syntaxhighlight lang=javascript>function relativeBearing(b1Rad, b2Rad) |
||
{ |
{ |
||
b1y = Math.cos(b1Rad); |
b1y = Math.cos(b1Rad); |
||
Line 2,978: | Line 2,978: | ||
+relativeBearing(60175.77306795546*deg2rad, 42213.07192354373*deg2rad)*rad2deg+"\n"; |
+relativeBearing(60175.77306795546*deg2rad, 42213.07192354373*deg2rad)*rad2deg+"\n"; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,998: | Line 2,998: | ||
===ES6=== |
===ES6=== |
||
< |
<syntaxhighlight lang=JavaScript>(() => { |
||
"use strict"; |
"use strict"; |
||
Line 3,072: | Line 3,072: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,083: | Line 3,083: | ||
=={{header|Jsish}}== |
=={{header|Jsish}}== |
||
< |
<syntaxhighlight lang=javascript>/* Angle difference between bearings, in Jsish */ |
||
function angleDifference(bearing1:number, bearing2:number):number { |
function angleDifference(bearing1:number, bearing2:number):number { |
||
var angle = (bearing2 - bearing1) % 360; |
var angle = (bearing2 - bearing1) % 360; |
||
Line 3,122: | Line 3,122: | ||
60175.7730679555° 42213.0719235437° 37.2988555882694° |
60175.7730679555° 42213.0719235437° 37.2988555882694° |
||
=!EXPECTEND!= |
=!EXPECTEND!= |
||
*/</ |
*/</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,133: | Line 3,133: | ||
Note that the `%` operator in jq produces integral results. |
Note that the `%` operator in jq produces integral results. |
||
< |
<syntaxhighlight lang=jq># Angles are in degrees; the result is rounded to 4 decimal places: |
||
def subtract($b1; $b2): |
def subtract($b1; $b2): |
||
10000 as $scale |
10000 as $scale |
||
Line 3,160: | Line 3,160: | ||
| subtract($p0; $p1) as $diff |
| subtract($p0; $p1) as $diff |
||
| (if $p0 < 0 then " " else " " end) as $offset |
| (if $p0 < 0 then " " else " " end) as $offset |
||
| "\($offset)\($p0) and \($p1) -> \($diff)" )</ |
| "\($offset)\($p0) and \($p1) -> \($diff)" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,198: | Line 3,198: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=julia>using Printf |
||
function angdiff(a, b) |
function angdiff(a, b) |
||
Line 3,219: | Line 3,219: | ||
(1174.8380510598456, -154146.66490124757), (60175.77306795546, 42213.07192354373)] |
(1174.8380510598456, -154146.66490124757), (60175.77306795546, 42213.07192354373)] |
||
@printf("% 9.1f - % 9.1f = % 6.1f\n", a, b, angdiff(a, b)) |
@printf("% 9.1f - % 9.1f = % 6.1f\n", a, b, angdiff(a, b)) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,241: | Line 3,241: | ||
=={{header|K}}== |
=={{header|K}}== |
||
<syntaxhighlight lang=K> |
|||
<lang K> |
|||
/ Angle difference between two angles |
/ Angle difference between two angles |
||
/ angledif.k |
/ angledif.k |
||
angdif: {[b1;b2]; :[(r:(b2-b1)!360.0)<-180.0;r+:360.0;r>180.0;r-:360.0];:r} |
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: |
The output of a session is given below: |
||
Line 3,276: | Line 3,276: | ||
=={{header|Klingphix}}== |
=={{header|Klingphix}}== |
||
{{trans|NewLISP}} |
{{trans|NewLISP}} |
||
< |
<syntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy |
||
:bearing sub 360 mod 540 add 360 mod 180 sub ; |
:bearing sub 360 mod 540 add 360 mod 180 sub ; |
||
Line 3,295: | Line 3,295: | ||
pstack |
pstack |
||
" " input</ |
" " input</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>(-25, -90, -175, 175, -170, 170, 118.118, 80.7109, 139.583, 72.3439, 161.503, -37.2989)</pre> |
<pre>(-25, -90, -175, 175, -170, 170, 118.118, 80.7109, 139.583, 72.3439, 161.503, -37.2989)</pre> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang=scala>// version 1.1.2 |
||
class Angle(d: Double) { |
class Angle(d: Double) { |
||
Line 3,333: | Line 3,333: | ||
println(f.format(ap.first, ap.second, diff.value)) |
println(f.format(ap.first, ap.second, diff.value)) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,356: | Line 3,356: | ||
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. |
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. |
||
< |
<syntaxhighlight lang=lua>bearing = {degrees = 0} -- prototype object |
||
function bearing:assign(angle) |
function bearing:assign(angle) |
||
Line 3,415: | Line 3,415: | ||
end |
end |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,433: | Line 3,433: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=Maple>getDiff := proc(b1,b2) |
||
local r: |
local r: |
||
r := frem(b2 - b1, 360): |
r := frem(b2 - b1, 360): |
||
Line 3,450: | Line 3,450: | ||
getDiff(-165313.6666297357,33693.9894517456); |
getDiff(-165313.6666297357,33693.9894517456); |
||
getDiff(1174.8380510598456,-154146.66490124757); |
getDiff(1174.8380510598456,-154146.66490124757); |
||
getDiff(60175.77306795546,42213.07192354373)</ |
getDiff(60175.77306795546,42213.07192354373)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>25 |
<pre>25 |
||
Line 3,468: | Line 3,468: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang=Mathematica>ClearAll[AngleDifference] |
||
AngleDifference[b1_, b2_] := Mod[b2 - b1, 360, -180] |
AngleDifference[b1_, b2_] := Mod[b2 - b1, 360, -180] |
||
AngleDifference[20, 45] |
AngleDifference[20, 45] |
||
Line 3,481: | Line 3,481: | ||
AngleDifference[-165313.6666297357, 33693.9894517456] |
AngleDifference[-165313.6666297357, 33693.9894517456] |
||
AngleDifference[1174.8380510598456, -154146.66490124757] |
AngleDifference[1174.8380510598456, -154146.66490124757] |
||
AngleDifference[60175.77306795546, 42213.07192354373]</ |
AngleDifference[60175.77306795546, 42213.07192354373]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>25 |
<pre>25 |
||
Line 3,498: | Line 3,498: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=modula2>FROM Terminal IMPORT *; |
||
PROCEDURE WriteRealLn(value : REAL); |
PROCEDURE WriteRealLn(value : REAL); |
||
Line 3,541: | Line 3,541: | ||
ReadChar; |
ReadChar; |
||
END Bearings.</ |
END Bearings.</syntaxhighlight> |
||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
Taken from Racket solution |
Taken from Racket solution |
||
< |
<syntaxhighlight lang=lisp> |
||
#!/usr/bin/env newlisp |
#!/usr/bin/env newlisp |
||
(define (bearing- bearing heading) (sub (mod (add (mod (sub bearing heading) 360.0) 540.0) 360.0) 180.0)) |
(define (bearing- bearing heading) (sub (mod (add (mod (sub bearing heading) 360.0) 540.0) 360.0) 180.0)) |
||
Line 3,562: | Line 3,562: | ||
(bearing- 1174.8380510598456 -154146.66490124757) |
(bearing- 1174.8380510598456 -154146.66490124757) |
||
(bearing- 60175.77306795546 42213.07192354373)) |
(bearing- 60175.77306795546 42213.07192354373)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,581: | Line 3,581: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang=nim>import math |
||
import strutils |
import strutils |
||
Line 3,612: | Line 3,612: | ||
vector.b2.formatFloat(ffDecimal, 2).align(13) & |
vector.b2.formatFloat(ffDecimal, 2).align(13) & |
||
delta(vector.b1, vector.b2).formatFloat(ffDecimal, 2).align(13) |
delta(vector.b1, vector.b2).formatFloat(ffDecimal, 2).align(13) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,632: | Line 3,632: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang=objeck>class AngleBearings { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
"Input in -180 to +180 range"->PrintLine(); |
"Input in -180 to +180 range"->PrintLine(); |
||
Line 3,665: | Line 3,665: | ||
return r; |
return r; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 3,689: | Line 3,689: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang=ocaml>let get_diff b1 b2 = |
||
let r = mod_float (b2 -. b1) 360.0 in |
let r = mod_float (b2 -. b1) 360.0 in |
||
if r < -180.0 |
if r < -180.0 |
||
Line 3,715: | Line 3,715: | ||
Printf.printf " %g\n" (get_diff 1174.8380510598456 (-154146.66490124757)); |
Printf.printf " %g\n" (get_diff 1174.8380510598456 (-154146.66490124757)); |
||
Printf.printf " %g\n" (get_diff 60175.77306795546 42213.07192354373); |
Printf.printf " %g\n" (get_diff 60175.77306795546 42213.07192354373); |
||
;;</ |
;;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,736: | Line 3,736: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang=parigp>centerliftmod1(x)=frac(x+1/2)-1/2; |
||
anglediff(x,y)=centerliftmod1((y-x)/360)*360; |
anglediff(x,y)=centerliftmod1((y-x)/360)*360; |
||
vecFunc(f)=v->call(f,v); |
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]])</ |
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]])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>%1 = [25, 90, 175, -175, 170, -170, -118.11840000000000000000000000000000000, -80.710900000000000000000000000000000000, -139.58328312339000000000000000000000023, -72.343918518700000000000000000000000733, -161.50295230741560000000000000000000000, 37.298855588269999999999999999999999909]</pre> |
<pre>%1 = [25, 90, 175, -175, 170, -170, -118.11840000000000000000000000000000000, -80.710900000000000000000000000000000000, -139.58328312339000000000000000000000023, -72.343918518700000000000000000000000733, -161.50295230741560000000000000000000000, 37.298855588269999999999999999999999909]</pre> |
||
Line 3,747: | Line 3,747: | ||
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. |
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> |
||
Program Bearings; |
Program Bearings; |
||
{ Reads pairs of angles from a file and subtracts them } |
{ Reads pairs of angles from a file and subtracts them } |
||
Line 3,796: | Line 3,796: | ||
close(angleFile) |
close(angleFile) |
||
End. |
End. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{in}} |
{{in}} |
||
Line 3,829: | Line 3,829: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Perl's built-in modulo is integer-only, so import a suitable one from the <code>POSIX</code> core module |
Perl's built-in modulo is integer-only, so import a suitable one from the <code>POSIX</code> core module |
||
< |
<syntaxhighlight lang=perl>use POSIX 'fmod'; |
||
sub angle { |
sub angle { |
||
Line 3,857: | Line 3,857: | ||
printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, angle($b1,$b2); |
printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, angle($b1,$b2); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> 20.00 45.00 = 25.00 |
<pre> 20.00 45.00 = 25.00 |
||
Line 3,873: | Line 3,873: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang=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: #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> |
<span style="color: #000080;font-style:italic;">-- trim trailing zeroes and decimal point</span> |
||
Line 3,907: | Line 3,907: | ||
<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;">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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,927: | Line 3,927: | ||
=={{header|Phixmonti}}== |
=={{header|Phixmonti}}== |
||
< |
<syntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt |
||
( "16" 1 "16" 1 "16" ) var al |
( "16" 1 "16" 1 "16" ) var al |
||
Line 3,954: | Line 3,954: | ||
-165313.6666297357 33693.9894517456 test |
-165313.6666297357 33693.9894517456 test |
||
1174.8380510598456 -154146.66490124757 test |
1174.8380510598456 -154146.66490124757 test |
||
60175.77306795546 42213.07192354373 test</ |
60175.77306795546 42213.07192354373 test</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> b1 b2 diff |
<pre> b1 b2 diff |
||
Line 3,977: | Line 3,977: | ||
examples with ''Get-Examples''. There is also a full help file accessed by typing |
examples with ''Get-Examples''. There is also a full help file accessed by typing |
||
''Get-Help Get-AngleDiff -full'' |
''Get-Help Get-AngleDiff -full'' |
||
< |
<syntaxhighlight lang=PowerShell> |
||
<# |
<# |
||
.Synopsis |
.Synopsis |
||
Line 4,111: | Line 4,111: | ||
}</ |
}</syntaxhighlight> |
||
'''Results of user inputs'''<br> |
'''Results of user inputs'''<br> |
||
using -45 and 98 with ''Get-AngleDiff'': |
using -45 and 98 with ''Get-AngleDiff'': |
||
< |
<syntaxhighlight lang=PowerShell>PS C:\WINDOWS\system32> Get-AngleDiff -45 98 |
||
The difference between -45 and 98 is 143</ |
The difference between -45 and 98 is 143</syntaxhighlight> |
||
'''Results of ''Get-Examples''''' |
'''Results of ''Get-Examples''''' |
||
Line 4,141: | Line 4,141: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=PureBasic>Procedure.f getDifference (b1.f, b2.f) |
||
r.f = Mod((b2 - b1), 360) |
r.f = Mod((b2 - b1), 360) |
||
If r >= 180: r - 360 |
If r >= 180: r - 360 |
||
Line 4,166: | Line 4,166: | ||
getDifference(60175.77306795546, 42213.07192354373) |
getDifference(60175.77306795546, 42213.07192354373) |
||
Repeat: Until Inkey() <> "" |
Repeat: Until Inkey() <> "" |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=python>from __future__ import print_function |
||
def getDifference(b1, b2): |
def getDifference(b1, b2): |
||
Line 4,197: | Line 4,197: | ||
print (getDifference(-165313.6666297357, 33693.9894517456)) |
print (getDifference(-165313.6666297357, 33693.9894517456)) |
||
print (getDifference(1174.8380510598456, -154146.66490124757)) |
print (getDifference(1174.8380510598456, -154146.66490124757)) |
||
print (getDifference(60175.77306795546, 42213.07192354373))</ |
print (getDifference(60175.77306795546, 42213.07192354373))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,220: | Line 4,220: | ||
Or, generalising a little by deriving the degrees from a (Radians -> Radians) function, and formatting the output in columns: |
Or, generalising a little by deriving the degrees from a (Radians -> Radians) function, and formatting the output in columns: |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang=python>'''Difference between two bearings''' |
||
from math import (acos, cos, pi, sin) |
from math import (acos, cos, pi, sin) |
||
Line 4,308: | Line 4,308: | ||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Difference between two bearings: |
<pre>Difference between two bearings: |
||
Line 4,326: | Line 4,326: | ||
The table output use print formatting only available from python 3. Tested with 3.6. |
The table output use print formatting only available from python 3. Tested with 3.6. |
||
{{Works with|Python|3.6}} |
{{Works with|Python|3.6}} |
||
< |
<syntaxhighlight lang=python>""" |
||
Difference between two bearings |
Difference between two bearings |
||
""" |
""" |
||
Line 4,342: | Line 4,342: | ||
print('.{:-^19}.{:-^19}.{:-^9}.' .format(" b1 ", " b2 ", " Δ b " )) |
print('.{:-^19}.{:-^19}.{:-^9}.' .format(" b1 ", " b2 ", " Δ b " )) |
||
for Δ in dataSet: |
for Δ in dataSet: |
||
print('|{: > 19}|{: > 19}|{: > 9.4f}|' .format(Δ[0], Δ[1],delta_bearing(Δ[0],Δ[1])))</ |
print('|{: > 19}|{: > 19}|{: > 9.4f}|' .format(Δ[0], Δ[1],delta_bearing(Δ[0],Δ[1])))</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>.------- b1 --------.------- b2 --------.-- Δ b --. |
<pre>.------- b1 --------.------- b2 --------.-- Δ b --. |
||
Line 4,362: | Line 4,362: | ||
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>. |
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>. |
||
< |
<syntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now! |
||
[ v- -v |
[ v- -v |
||
Line 4,391: | Line 4,391: | ||
$->v drop |
$->v drop |
||
angledelta |
angledelta |
||
20 point$ echo$ cr ]</ |
20 point$ echo$ cr ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,412: | Line 4,412: | ||
''see my comments in discussion regards bearing-heading or vice versa'' |
''see my comments in discussion regards bearing-heading or vice versa'' |
||
< |
<syntaxhighlight lang=racket>#lang racket |
||
(define (% a b) (- a (* b (truncate (/ a b))))) |
(define (% a b) (- a (* b (truncate (/ a b))))) |
||
Line 4,439: | Line 4,439: | ||
(check-equal? (% 17.5 10) 7.5) |
(check-equal? (% 17.5 10) 7.5) |
||
(check-equal? (% -7.5 10) -7.5) |
(check-equal? (% -7.5 10) -7.5) |
||
(check-equal? (% -17.5 10) -7.5))</ |
(check-equal? (% -17.5 10) -7.5))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,459: | Line 4,459: | ||
{{works with|Rakudo|2016.11}} |
{{works with|Rakudo|2016.11}} |
||
< |
<syntaxhighlight lang=perl6>sub infix:<∠> (Real $b1, Real $b2) { |
||
(my $b = ($b2 - $b1 + 720) % 360) > 180 ?? $b - 360 !! $b; |
(my $b = ($b2 - $b1 + 720) % 360) > 180 ?? $b - 360 !! $b; |
||
} |
} |
||
Line 4,476: | Line 4,476: | ||
60175.77306795546, 42213.07192354373 |
60175.77306795546, 42213.07192354373 |
||
-> $b1, $b2 { printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, $b1 ∠ $b2 }</ |
-> $b1, $b2 { printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, $b1 ∠ $b2 }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 20.00 45.00 = 25.00 |
<pre> 20.00 45.00 = 25.00 |
||
Line 4,493: | Line 4,493: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Some extra coding was added for a better visual presentation; the angles were centered, the answers were aligned. |
Some extra coding was added for a better visual presentation; the angles were centered, the answers were aligned. |
||
< |
<syntaxhighlight lang=rexx>/*REXX pgm calculates difference between two angles (in degrees), normalizes the result.*/ |
||
numeric digits 25 /*use enough dec. digits for angles*/ |
numeric digits 25 /*use enough dec. digits for angles*/ |
||
call show 20, 45 /*display angular difference (deg).*/ |
call show 20, 45 /*display angular difference (deg).*/ |
||
Line 4,513: | Line 4,513: | ||
if pos(., x)\==0 then x=strip( strip(x, 'T', 0), "T", .) /*strip trailing chaff.*/ |
if pos(., x)\==0 then x=strip( strip(x, 'T', 0), "T", .) /*strip trailing chaff.*/ |
||
say center(a || $, d) '─' center(b || $, d) " ────► " x || $ |
say center(a || $, d) '─' center(b || $, d) " ────► " x || $ |
||
return /* [↑] display the angular difference.*/</ |
return /* [↑] display the angular difference.*/</syntaxhighlight> |
||
{{out|output}} |
{{out|output}} |
||
<pre> |
<pre> |
||
Line 4,531: | Line 4,531: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang=ring> |
||
# Project : Angle difference between two bearings |
# Project : Angle difference between two bearings |
||
Line 4,553: | Line 4,553: | ||
end |
end |
||
return r |
return r |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,571: | Line 4,571: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
< |
<syntaxhighlight lang=ruby>def getDifference(b1, b2) |
||
r = (b2 - b1) % 360.0 |
r = (b2 - b1) % 360.0 |
||
# Ruby modulus has same sign as divisor, which is positive here, |
# Ruby modulus has same sign as divisor, which is positive here, |
||
Line 4,599: | Line 4,599: | ||
puts getDifference(1174.8380510598456, -154146.66490124757) |
puts getDifference(1174.8380510598456, -154146.66490124757) |
||
puts getDifference(60175.77306795546, 42213.07192354373) |
puts getDifference(60175.77306795546, 42213.07192354373) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,620: | Line 4,620: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang=runbasic>sub getDifference b1, b2 |
||
r = (b2 - b1) mod 360 |
r = (b2 - b1) mod 360 |
||
if r >= 180 then r = r - 360 |
if r >= 180 then r = r - 360 |
||
Line 4,640: | Line 4,640: | ||
call getDifference -70099.74233810938, 29840.67437876723 |
call getDifference -70099.74233810938, 29840.67437876723 |
||
call getDifference -165313.6666297357, 33693.9894517456 |
call getDifference -165313.6666297357, 33693.9894517456 |
||
call getDifference 1174.8380510598456, -154146.66490124757</ |
call getDifference 1174.8380510598456, -154146.66490124757</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang=rust> |
||
/// Calculate difference between two bearings, in -180 to 180 degrees range |
/// Calculate difference between two bearings, in -180 to 180 degrees range |
||
pub fn angle_difference(bearing1: f64, bearing2: f64) -> f64 { |
pub fn angle_difference(bearing1: f64, bearing2: f64) -> f64 { |
||
Line 4,683: | Line 4,683: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Scala}}== |
=={{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)]. |
{{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)]. |
||
< |
<syntaxhighlight lang=Scala>object AngleDifference extends App { |
||
private def getDifference(b1: Double, b2: Double) = { |
private def getDifference(b1: Double, b2: Double) = { |
||
val r = (b2 - b1) % 360.0 |
val r = (b2 - b1) % 360.0 |
||
Line 4,711: | Line 4,711: | ||
println(getDifference(60175.77306795546, 42213.07192354373)) |
println(getDifference(60175.77306795546, 42213.07192354373)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
R6RS standard. |
R6RS standard. |
||
< |
<syntaxhighlight lang=scheme>#!r6rs |
||
(import (rnrs base (6)) |
(import (rnrs base (6)) |
||
Line 4,745: | Line 4,745: | ||
(display (apply bearing-difference argument-list)) |
(display (apply bearing-difference argument-list)) |
||
(newline)) |
(newline)) |
||
test-cases))</ |
test-cases))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,768: | Line 4,768: | ||
which can be used to solve this task easily. |
which can be used to solve this task easily. |
||
< |
<syntaxhighlight lang=seed7>$ include "seed7_05.s7i"; |
||
include "float.s7i"; |
include "float.s7i"; |
||
Line 4,799: | Line 4,799: | ||
writeln(getDifference(1174.8380510598456, -154146.66490124757)); |
writeln(getDifference(1174.8380510598456, -154146.66490124757)); |
||
writeln(getDifference(60175.77306795546, 42213.07192354373)); |
writeln(getDifference(60175.77306795546, 42213.07192354373)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,822: | Line 4,822: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang=ruby>func bearingAngleDiff(b1, b2) { |
||
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b |
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b |
||
} |
} |
||
Line 4,846: | Line 4,846: | ||
) { |
) { |
||
printf("%25s %25s %25s\n", b1, b2, bearingAngleDiff(b1, b2)) |
printf("%25s %25s %25s\n", b1, b2, bearingAngleDiff(b1, b2)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> B1 B2 Difference |
<pre> B1 B2 Difference |
||
Line 4,865: | Line 4,865: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang=swift>func angleDifference(a1: Double, a2: Double) -> Double { |
||
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360) |
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360) |
||
Line 4,892: | Line 4,892: | ||
] |
] |
||
print(testCases.map(angleDifference))</ |
print(testCases.map(angleDifference))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,899: | Line 4,899: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang=tcl> |
||
proc angleDiff {b1 b2} { |
proc angleDiff {b1 b2} { |
||
set angle [::tcl::mathfunc::fmod [expr ($b2 - $b1)] 360] |
set angle [::tcl::mathfunc::fmod [expr ($b2 - $b1)] 360] |
||
Line 4,928: | Line 4,928: | ||
puts [angleDiff 1174.8380510598456 -154146.66490124757] |
puts [angleDiff 1174.8380510598456 -154146.66490124757] |
||
puts [angleDiff 60175.77306795546 42213.07192354373] |
puts [angleDiff 60175.77306795546 42213.07192354373] |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,950: | Line 4,950: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang=vb>Private Function tx(a As Variant) As String |
||
Dim s As String |
Dim s As String |
||
s = CStr(Format(a, "0.######")) |
s = CStr(Format(a, "0.######")) |
||
Line 4,983: | Line 4,983: | ||
test 1174.83805105985, -154146.664901248 |
test 1174.83805105985, -154146.664901248 |
||
test 60175.7730679555, 42213.0719235437 |
test 60175.7730679555, 42213.0719235437 |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre> b1 b2 diff |
<pre> b1 b2 diff |
||
---------------- ---------------- ---------------- |
---------------- ---------------- ---------------- |
||
Line 5,001: | Line 5,001: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang=vbnet>Module Module1 |
||
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal |
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal |
||
Line 5,051: | Line 5,051: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>25 |
<pre>25 |
||
Line 5,068: | Line 5,068: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|go}} |
{{trans|go}} |
||
< |
<syntaxhighlight lang=vlang>import math |
||
type Bearing = f64 |
type Bearing = f64 |
||
Line 5,105: | Line 5,105: | ||
fn angle_difference(b2 Bearing, b1 Bearing) Bearing { |
fn angle_difference(b2 Bearing, b1 Bearing) Bearing { |
||
return math.mod(math.mod(b2-b1, 360)+360+180, 360) - 180 |
return math.mod(math.mod(b2-b1, 360)+360+180, 360) - 180 |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,128: | Line 5,128: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
< |
<syntaxhighlight lang=ecmascript>var subtract = Fn.new { |b1, b2| |
||
var d = (b2 - b1) % 360 |
var d = (b2 - b1) % 360 |
||
if (d < -180) d = d + 360 |
if (d < -180) d = d + 360 |
||
Line 5,157: | Line 5,157: | ||
var offset = (p0 < 0) ? " " : " " |
var offset = (p0 < 0) ? " " : " " |
||
System.print("%(offset)%(p0) and %(p1) -> %(diff)") |
System.print("%(offset)%(p0) and %(p1) -> %(diff)") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,177: | Line 5,177: | ||
=={{header|XBS}}== |
=={{header|XBS}}== |
||
< |
<syntaxhighlight lang=xbs>settype Bearing = {Angle:number} |
||
class Bearing { |
class Bearing { |
||
private method construct(Angle:number=0) |
private method construct(Angle:number=0) |
||
Line 5,207: | Line 5,207: | ||
set b2:Bearing=new Bearing(v[1]); |
set b2:Bearing=new Bearing(v[1]); |
||
log(b2::ToString()+" - "+b1::ToString()+" = "+(b2-b1)::ToString()); |
log(b2::ToString()+" - "+b1::ToString()+" = "+(b2-b1)::ToString()); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 5,227: | Line 5,227: | ||
Pairs of bearing angles are input from the console or from a file |
Pairs of bearing angles are input from the console or from a file |
||
(terminated with Ctrl+C) redirected on the command line. |
(terminated with Ctrl+C) redirected on the command line. |
||
< |
<syntaxhighlight lang=XPL0>real B1, B2, Ang; |
||
[Text(0, " Bearing 1 Bearing 2 Difference"); |
[Text(0, " Bearing 1 Bearing 2 Difference"); |
||
loop [B1:= RlIn(1); |
loop [B1:= RlIn(1); |
||
Line 5,239: | Line 5,239: | ||
RlOut(0, Ang); |
RlOut(0, Ang); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 5,260: | Line 5,260: | ||
=={{header|Yabasic}}== |
=={{header|Yabasic}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang=freebasic>// Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings |
||
// by Jjuanhdez, 06/2022 |
// by Jjuanhdez, 06/2022 |
||
Line 5,284: | Line 5,284: | ||
if r >= 180.0 r = r - 360.0 |
if r >= 180.0 r = r - 360.0 |
||
print r |
print r |
||
end sub</ |
end sub</syntaxhighlight> |
||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight 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; |
( (b:=(0.0 + b2 - b1 + 720)%360) > 180 ) and b - 360 or b; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang=zkl>T( 20,45, -45,45, -85,90, -95,90, -45,125, -45,145 ) |
||
.pump(Console.println,Void.Read, |
.pump(Console.println,Void.Read, |
||
fcn(b1,b2){ "%.1f\UB0; + %.1f\UB0; = %.1f\UB0;" |
fcn(b1,b2){ "%.1f\UB0; + %.1f\UB0; = %.1f\UB0;" |
||
.fmt(b1,b2,bearingAngleDiff(b1,b2)) });</ |
.fmt(b1,b2,bearingAngleDiff(b1,b2)) });</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |