Jump to content

Angle difference between two bearings: Difference between revisions

m
syntax highlighting fixup automation
(add task to aarch64 assembly raspberry pi)
m (syntax highlighting fixup automation)
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 134:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<langsyntaxhighlight lang=AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program diffAngle64.s */
Line 705:
/***************************************************/
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
<pre>
Difference between +20 and +45 = +25
Line 720:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:REALMATH.ACT"
 
INT FUNC AngleI(INT a1,a2)
Line 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 798:
=={{header|Ada}}==
Ada does not provide a built-in mod function for floating point types. This program supplies one.
<langsyntaxhighlight lang=Ada>
-----------------------------------------------------------------------
-- Angle difference between two bearings
Line 850:
end Bearing_Angles;
</syntaxhighlight>
</lang>
{{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.
<langsyntaxhighlight lang=APL>[0] D←B1 DIFF B2
[1] D←180+¯360|180+B2-B1
</syntaxhighlight>
</lang>
{{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}}
<langsyntaxhighlight lang=ARM Assembly>
/* ARM assembly Raspberry PI or android with termux */
/* program diffAngle.s */
Line 1,403:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Difference between +20E0 and +45E0 = +25E0
Line 1,418:
=={{header|Arturo}}==
{{trans|Ruby}}
<langsyntaxhighlight lang=rebol>getDifference: function [b1, b2][
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</langsyntaxhighlight>
 
{{out}}
Line 1,467:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>Angles:= [[20, 45]
,[-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
}</langsyntaxhighlight>
{{out}}
<pre>45 to 20 = 25
Line 1,503:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK
BEGIN {
Line 1,537:
return(r)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,560:
{{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 1,579:
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 1,602:
CALL getdifference(-165313.6666297357, 33693.9894517456)
CALL getdifference(1174.8380510598456, -154146.66490124757)
END</langsyntaxhighlight>
 
=={{header|BASIC256}}==
{{trans|Python}}
<langsyntaxhighlight lang=BASIC256># Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
# 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</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 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>
<lang C>
#include<stdlib.h>
#include<stdio.h>
Line 1,712:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and output for two bearings :
<pre>
Line 1,758:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang=csharp>using System;
 
namespace Angle_difference_between_two_bearings
Line 1,839:
}
}
}</langsyntaxhighlight>
{{out| Output}}
<pre>Hello World!
Line 1,859:
 
=={{header|C++}}==
<langsyntaxhighlight lang=cpp>#include <cmath>
#include <iostream>
using namespace std;
Line 1,893:
 
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 1,914:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang=clojure>(defn angle-difference [a b]
(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>
</lang>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang=cobol>
******************************************************************
* COBOL solution to Angle difference challange
Line 2,078:
* 012 37.2989
******************************************************************
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=common lisp>
(defun angle-difference (b1 b2)
(let ((diff (mod (- b2 b1) 360)))
Line 2,089:
(decf diff 360)
diff))))
</syntaxhighlight>
</lang>
{{out| Output}}
<pre>
Line 2,108:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight lang=D>import std.stdio;
 
double getDifference(double b1, double b2) {
Line 2,139:
writeln(getDifference(1174.8380510598456, -154146.66490124757));
writeln(getDifference(60175.77306795546, 42213.07192354373));
}</langsyntaxhighlight>
 
{{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.
<langsyntaxhighlight lang=Erlang>
-module(bearings).
 
Line 2,236:
 
passed.
</syntaxhighlight>
</lang>
 
=={{header|Excel}}==
Line 2,246:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang=lisp>ANGLEBETWEENBEARINGS
=LAMBDA(ab,
DEGREES(
Line 2,273:
) * ACOS((ax * bx) + (ay * by))
)
)</langsyntaxhighlight>
 
Using an Excel custom number format to display the degree symbol°
Line 2,389:
 
=={{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 2,412:
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 2,430:
{{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 2,455:
[ delta-bearing . ] 2 12 mnapply ;
 
MAIN: bearings-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 2,474:
=={{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 2,498:
Angle-Difference-const f.
;
</syntaxhighlight>
</lang>
{{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. <langsyntaxhighlight 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.
COMPLEX*16 CIS,Z1,Z2,Z !Scratchpads.
Line 2,554:
END DO
 
END</langsyntaxhighlight>
The output shows the stages:
<pre>
Line 2,575:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>' version 28-01-2019
' compile with: fbc -s console
 
Line 2,623:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 2,676:
return d
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,691:
 
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 2,721:
func angleDifference(b2, b1 float64) float64 {
return math.Mod(math.Mod(b2-b1, 360)+360+180, 360) - 180
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,741:
'''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 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))
}</langsyntaxhighlight>
 
'''Output:'''
Line 2,791:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=Haskell>import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
Line 2,840:
 
radians :: Degrees -> Radians
radians = (/ 180) . (pi *)</langsyntaxhighlight>
{{Out}}
<pre> 20.00° - 45.00° -> 25.00°
Line 2,850:
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight lang=IS-BASIC>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}}==
 
<langsyntaxhighlight lang=j>relativeBearing=: (180 -~ 360 | 180 + -~)/"1</langsyntaxhighlight>
<langsyntaxhighlight lang=j>tests=: _99&".;._2 noun define
20 45
-45 45
Line 2,886:
_165314 33694 _72.3439
1174.84 _154147 _161.503
60175.8 42213.1 37.2989</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang=java>public class AngleDifference {
 
public static double getDifference(double b1, double b2) {
Line 2,920:
System.out.println(getDifference(60175.77306795546, 42213.07192354373));
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,944:
This approach should be reliable but it is also very inefficient.
 
<langsyntaxhighlight lang=javascript>function relativeBearing(b1Rad, b2Rad)
{
b1y = Math.cos(b1Rad);
Line 2,978:
+relativeBearing(60175.77306795546*deg2rad, 42213.07192354373*deg2rad)*rad2deg+"\n";
 
}</langsyntaxhighlight>
 
{{out}}
Line 2,998:
===ES6===
 
<langsyntaxhighlight lang=JavaScript>(() => {
"use strict";
 
Line 3,072:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,083:
 
=={{header|Jsish}}==
<langsyntaxhighlight lang=javascript>/* Angle difference between bearings, in Jsish */
function angleDifference(bearing1:number, bearing2:number):number {
var angle = (bearing2 - bearing1) % 360;
Line 3,122:
60175.7730679555° 42213.0719235437° 37.2988555882694°
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 3,133:
 
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 3,160:
| subtract($p0; $p1) as $diff
| (if $p0 < 0 then " " else " " end) as $offset
| "\($offset)\($p0) and \($p1) -> \($diff)" )</langsyntaxhighlight>
{{out}}
<pre>
Line 3,198:
{{trans|Python}}
 
<langsyntaxhighlight lang=julia>using Printf
 
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</langsyntaxhighlight>
 
{{out}}
Line 3,241:
 
=={{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 3,276:
=={{header|Klingphix}}==
{{trans|NewLISP}}
<langsyntaxhighlight lang=Klingphix>include ..\Utilitys.tlhy
 
:bearing sub 360 mod 540 add 360 mod 180 sub ;
Line 3,295:
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 3,333:
println(f.format(ap.first, ap.second, diff.value))
}
}</langsyntaxhighlight>
 
{{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.
 
<langsyntaxhighlight lang=lua>bearing = {degrees = 0} -- prototype object
 
function bearing:assign(angle)
Line 3,415:
end
 
main()</langsyntaxhighlight>
 
{{out}}
Line 3,433:
=={{header|Maple}}==
{{trans|C++}}
<langsyntaxhighlight lang=Maple>getDiff := proc(b1,b2)
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)</langsyntaxhighlight>
{{Out}}
<pre>25
Line 3,468:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang=Mathematica>ClearAll[AngleDifference]
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]</langsyntaxhighlight>
{{out}}
<pre>25
Line 3,498:
=={{header|Modula-2}}==
{{trans|Java}}
<langsyntaxhighlight lang=modula2>FROM Terminal IMPORT *;
 
PROCEDURE WriteRealLn(value : REAL);
Line 3,541:
 
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 3,562:
(bearing- 1174.8380510598456 -154146.66490124757)
(bearing- 60175.77306795546 42213.07192354373))
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,581:
=={{header|Nim}}==
 
<langsyntaxhighlight lang=nim>import math
import strutils
 
Line 3,612:
vector.b2.formatFloat(ffDecimal, 2).align(13) &
delta(vector.b1, vector.b2).formatFloat(ffDecimal, 2).align(13)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,632:
=={{header|Objeck}}==
{{trans|Java}}
<langsyntaxhighlight lang=objeck>class AngleBearings {
function : Main(args : String[]) ~ Nil {
"Input in -180 to +180 range"->PrintLine();
Line 3,665:
return r;
}
}</langsyntaxhighlight>
 
{{output}}
Line 3,689:
=={{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 3,715:
Printf.printf " %g\n" (get_diff 1174.8380510598456 (-154146.66490124757));
Printf.printf " %g\n" (get_diff 60175.77306795546 42213.07192354373);
;;</langsyntaxhighlight>
 
{{out}}
Line 3,736:
 
=={{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 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.
 
<langsyntaxhighlight lang=Pascal>
Program Bearings;
{ Reads pairs of angles from a file and subtracts them }
Line 3,796:
close(angleFile)
End.
</syntaxhighlight>
</lang>
 
{{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
<langsyntaxhighlight lang=perl>use POSIX 'fmod';
 
sub angle {
Line 3,857:
printf "%10.2f %10.2f = %8.2f\n", $b1, $b2, angle($b1,$b2);
}
</syntaxhighlight>
</lang>
{{out}}
<pre> 20.00 45.00 = 25.00
Line 3,873:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight 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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 3,927:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight lang=Phixmonti>include ..\Utilitys.pmt
 
( "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</langsyntaxhighlight>
{{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''
<langsyntaxhighlight lang=PowerShell>
<#
.Synopsis
Line 4,111:
 
 
}</langsyntaxhighlight>
 
'''Results of user inputs'''<br>
using -45 and 98 with ''Get-AngleDiff'':
<langsyntaxhighlight lang=PowerShell>PS C:\WINDOWS\system32> Get-AngleDiff -45 98
The difference between -45 and 98 is 143</langsyntaxhighlight>
 
'''Results of ''Get-Examples'''''
Line 4,141:
=={{header|PureBasic}}==
{{trans|Python}}
<langsyntaxhighlight lang=PureBasic>Procedure.f getDifference (b1.f, b2.f)
r.f = Mod((b2 - b1), 360)
If r >= 180: r - 360
Line 4,166:
getDifference(60175.77306795546, 42213.07192354373)
Repeat: Until Inkey() <> ""
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
{{trans|C++}}
<langsyntaxhighlight lang=python>from __future__ import print_function
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))</langsyntaxhighlight>
 
{{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}}
<langsyntaxhighlight lang=python>'''Difference between two bearings'''
 
from math import (acos, cos, pi, sin)
Line 4,308:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{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}}
<langsyntaxhighlight lang=python>"""
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])))</langsyntaxhighlight>
{{Out}}
<pre>.------- b1 --------.------- b2 --------.-- Δ b --.
Line 4,362:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<langsyntaxhighlight lang=Quackery> [ $ "bigrat.qky" loadfile ] now!
 
[ v- -v
Line 4,391:
$->v drop
angledelta
20 point$ echo$ cr ]</langsyntaxhighlight>
 
{{out}}
Line 4,412:
''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 4,439:
(check-equal? (% 17.5 10) 7.5)
(check-equal? (% -7.5 10) -7.5)
(check-equal? (% -17.5 10) -7.5))</langsyntaxhighlight>
 
{{out}}
Line 4,459:
{{works with|Rakudo|2016.11}}
 
<langsyntaxhighlight lang=perl6>sub infix:<∠> (Real $b1, Real $b2) {
(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 }</langsyntaxhighlight>
{{out}}
<pre> 20.00 45.00 = 25.00
Line 4,493:
=={{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 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.*/</langsyntaxhighlight>
{{out|output}}
<pre>
Line 4,531:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : Angle difference between two bearings
 
Line 4,553:
end
return r
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,571:
=={{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 4,599:
puts getDifference(1174.8380510598456, -154146.66490124757)
puts getDifference(60175.77306795546, 42213.07192354373)
end</langsyntaxhighlight>
 
{{out}}
Line 4,620:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang=runbasic>sub getDifference b1, b2
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</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 4,683:
}
}
</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 lang=Scala>object AngleDifference extends App {
private def getDifference(b1: Double, b2: Double) = {
val r = (b2 - b1) % 360.0
Line 4,711:
println(getDifference(60175.77306795546, 42213.07192354373))
 
}</langsyntaxhighlight>
 
=={{header|Scheme}}==
R6RS standard.
<langsyntaxhighlight lang=scheme>#!r6rs
 
(import (rnrs base (6))
Line 4,745:
(display (apply bearing-difference argument-list))
(newline))
test-cases))</langsyntaxhighlight>
 
{{out}}
Line 4,768:
which can be used to solve this task easily.
 
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,799:
writeln(getDifference(1174.8380510598456, -154146.66490124757));
writeln(getDifference(60175.77306795546, 42213.07192354373));
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,822:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang=ruby>func bearingAngleDiff(b1, b2) {
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b
}
Line 4,846:
) {
printf("%25s %25s %25s\n", b1, b2, bearingAngleDiff(b1, b2))
}</langsyntaxhighlight>
{{out}}
<pre> B1 B2 Difference
Line 4,865:
=={{header|Swift}}==
 
<langsyntaxhighlight lang=swift>func angleDifference(a1: Double, a2: Double) -> Double {
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360)
 
Line 4,892:
]
 
print(testCases.map(angleDifference))</langsyntaxhighlight>
 
{{out}}
Line 4,899:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang=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>
</lang>
{{out}}
<pre>
Line 4,950:
 
=={{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 4,983:
test 1174.83805105985, -154146.664901248
test 60175.7730679555, 42213.0719235437
End Sub</langsyntaxhighlight>{{out}}
<pre> b1 b2 diff
---------------- ---------------- ----------------
Line 5,001:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang=vbnet>Module Module1
 
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal
Line 5,051:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>25
Line 5,068:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang=vlang>import math
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
}</langsyntaxhighlight>
 
{{out}}
Line 5,128:
 
=={{header|Wren}}==
<langsyntaxhighlight lang=ecmascript>var subtract = Fn.new { |b1, b2|
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)")
}</langsyntaxhighlight>
 
{{out}}
Line 5,177:
 
=={{header|XBS}}==
<langsyntaxhighlight lang=xbs>settype Bearing = {Angle:number}
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());
}</langsyntaxhighlight>
{{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.
<langsyntaxhighlight lang=XPL0>real B1, B2, Ang;
[Text(0, " Bearing 1 Bearing 2 Difference");
loop [B1:= RlIn(1);
Line 5,239:
RlOut(0, Ang);
];
]</langsyntaxhighlight>
 
{{out}}
Line 5,260:
=={{header|Yabasic}}==
{{trans|Python}}
<langsyntaxhighlight lang=freebasic>// Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
// by Jjuanhdez, 06/2022
 
Line 5,284:
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>
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.