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