Angle difference between two bearings: Difference between revisions

Added Algol 68
m (syntax highlighting fixup automation)
(Added Algol 68)
 
(17 intermediate revisions by 10 users not shown)
Line 34:
 
=={{header|11l}}==
<syntaxhighlight lang="11l">F get_difference(b1, b2)
R wrap(b2 - b1, -180.0, 180.0)
 
Line 74:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<syntaxhighlight lang="360asm">* Angle difference between two bearings - 06/06/2018
ANGLEDBB CSECT
USING ANGLEDBB,R13 base register
Line 134:
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang=AArch64"aarch64 Assemblyassembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program diffAngle64.s */
Line 720:
{{libheader|Action! Tool Kit}}
{{libheader|Action! Real Math}}
<syntaxhighlight lang=Action"action!">INCLUDE "H6:REALMATH.ACT"
 
INT FUNC AngleI(INT a1,a2)
Line 798:
=={{header|Ada}}==
Ada does not provide a built-in mod function for floating point types. This program supplies one.
<syntaxhighlight lang=Ada"ada">
-----------------------------------------------------------------------
-- Angle difference between two bearings
Line 864:
Difference between -154146.6649 and 1174.8381 is -161.5030
Difference between 42213.0719 and 60175.7731 is 37.2989
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|11l}}
<syntaxhighlight lang="algol68">
BEGIN # angle difference between 2 bearings - translated from the 11l sample #
 
PROC wrap = (REAL v, l1, l2 )REAL:
BEGIN
REAL result := v;
WHILE result < l1 DO result +:= 2 * l2 OD;
WHILE result > l2 DO result +:= 2 * l1 OD;
result
END # wrap # ;
PROC get_difference = ( REAL b1, b2 )REAL: wrap( b2 - b1, -180.0, 180.0 );
 
OP FMT = ( REAL v )STRING:
BEGIN
STRING result := fixed( ABS v, 0, 3 );
IF result[ LWB result ] = "." THEN "0" +=: result FI;
WHILE result[ UPB result ] = "0" DO result := result[ : UPB result - 1 ] OD;
IF result[ UPB result ] = "." THEN result := result[ : UPB result - 1 ] FI;
IF v < 0 THEN "-" ELSE " " FI + result
END # FMT # ;
 
print( ( FMT get_difference( 20.0, 45.0 ), newline ) );
print( ( FMT get_difference( -45.0, 45.0 ), newline ) );
print( ( FMT get_difference( -85.0, 90.0 ), newline ) );
print( ( FMT get_difference( -95.0, 90.0 ), newline ) );
print( ( FMT get_difference( -45.0, 125.0 ), newline ) );
print( ( FMT get_difference( -45.0, 145.0 ), newline ) );
print( ( FMT get_difference( -45.0, 125.0 ), newline ) );
print( ( FMT get_difference( -45.0, 145.0 ), newline ) );
print( ( FMT get_difference( 29.4803, -88.6381 ), newline ) );
print( ( FMT get_difference( -78.3251, -159.036 ), newline ) );
print( ( newline ) );
print( ( FMT get_difference( -70099.74233810938, 29840.67437876723 ), newline ) );
print( ( FMT get_difference( -165313.6666297357, 33693.9894517456 ), newline ) );
print( ( FMT get_difference( 1174.8380510598456, -154146.66490124757 ), newline ) );
print( ( FMT get_difference( 60175.77306795546, 42213.07192354373 ), newline ) )
 
END
</syntaxhighlight>
{{out}}
<pre>
25
90
175
-175
170
-170
170
-170
-118.118
-80.711
 
-139.583
-72.344
-161.503
37.299
</pre>
 
=={{header|APL}}==
Returns an angle in (-180,180]; so two opposite bearings have a difference of 180 degrees, which is more natural than -180 degrees.
<syntaxhighlight lang=APL"apl">[0] D←B1 DIFF B2
[1] D←180+¯360|180+B2-B1
</syntaxhighlight>
Line 893 ⟶ 954:
180
</pre>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi <br> or android 32 bits with application Termux}}
<syntaxhighlight lang=ARM"arm Assemblyassembly">
/* ARM assembly Raspberry PI or android with termux */
/* program diffAngle.s */
Line 1,418 ⟶ 1,480:
=={{header|Arturo}}==
{{trans|Ruby}}
<syntaxhighlight lang="rebol">getDifference: function [b1, b2][
r: (b2 - b1) % 360.0
 
Line 1,467 ⟶ 1,529:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">Angles:= [[20, 45]
,[-45, 45]
,[-85, 90]
Line 1,503 ⟶ 1,565:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK
BEGIN {
Line 1,557 ⟶ 1,619:
 
=={{header|BASIC}}==
==={{header|Chipmunk Basic}}===
{{trans|Phyton}}
{{works with|Chipmunk Basic|3.6.4}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 cls
110 sub getdifference(b1,b2)
120 r = (b2-b1) mod 360
130 if r >= 180 then r = r-360
140 print using "#######.######";b1;
150 print using " #######.######";b2;
160 print using " #######.######";r
170 end sub
180 print "Input in -180 to +180 range:"
190 print " b1 b2 difference"
200 print " -------------------------------------------------"
210 getdifference(20,45)
220 getdifference(-45,45)
230 getdifference(-85,90)
240 getdifference(-95,90)
250 getdifference(-45,125)
260 getdifference(-45,145)
270 getdifference(-45,125)
280 getdifference(-45,145)
290 getdifference(29.4803,-88.6381)
300 getdifference(-78.3251,-159.036)
310 getdifference(-70099.742338,29840.674379)
320 getdifference(-165313.66663,33693.989452)
330 getdifference(1174.838051,-154146.664901)
340 print
350 print "Input in wider range:"
360 print " b1 b2 difference"
370 print " -------------------------------------------------"
380 getdifference(-70099.742338,29840.674379)
390 getdifference(-165313.66663,33693.989452)
400 getdifference(1174.838051,-154146.664901)
410 getdifference(60175.773068,42213.071924)</syntaxhighlight>
{{out}}
<pre>Input in -180 to +180 range:
b1 b2 difference
-------------------------------------------------
20.000000 45.000000 25.000000
-45.000000 45.000000 90.000000
-85.000000 90.000000 175.000000
-95.000000 90.000000 -175.000000
-45.000000 125.000000 170.000000
-45.000000 145.000000 -170.000000
-45.000000 125.000000 170.000000
-45.000000 145.000000 -170.000000
29.480300 -88.638100 -118.000000
-78.325100 -159.036000 -80.000000
-70099.742338 29840.674379 -140.000000
-165313.666630 33693.989452 -73.000000
1174.838051 -154146.664901 -161.000000
 
Input in wider range:
b1 b2 difference
-------------------------------------------------
-70099.742338 29840.674379 -140.000000
-165313.666630 33693.989452 -73.000000
1174.838051 -154146.664901 -161.000000
60175.773068 42213.071924 -322.000000</pre>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">SUB getDifference (b1!, b2!)
r! = (b2 - b1) MOD 360!
IF r >= 180! THEN r = r - 360!
Line 1,582 ⟶ 1,706:
 
==={{header|True BASIC}}===
<syntaxhighlight 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,606 ⟶ 1,730:
=={{header|BASIC256}}==
{{trans|Python}}
<syntaxhighlight lang=BASIC256"basic256"># Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
# by Jjuanhdez, 06/2022
 
Line 1,634 ⟶ 1,758:
 
=={{header|Befunge}}==
<syntaxhighlight 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**-^
Line 1,671 ⟶ 1,795:
25 90 175 -175 170 -170 -117 -81 -141 -74 -160 38
</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">Adiff ← 180 - 360 | 180 + -
 
tests ← [20‿45, ¯45‿45, ¯85‿90, ¯95‿90, ¯45‿125, ¯45‿145
99‿279, 29.4803‿¯88.6381, ¯78.3251‿¯159.036
¯70099.74233810938‿29840.67437876723
¯165313.6666297357‿33693.9894517456
1174.8380510598456‿¯154146.66490124757
60175.77306795546‿42213.07192354373]
 
Round ← ⌊∘+⟜0.5⌾(1e3⊸×)
 
Round∘∾⟜(Adiff´)˘ tests</syntaxhighlight>
{{out}}
<pre>┌─
╵ 20 45 25
¯45 45 90
¯85 90 175
¯95 90 ¯175
¯45 125 170
¯45 145 ¯170
99 279 180
29.48 ¯88.638 ¯118.118
¯78.325 ¯159.036 ¯80.711
¯70099.742 29840.674 ¯139.583
¯165313.667 33693.989 ¯72.344
1174.838 ¯154146.665 ¯161.503
60175.773 42213.072 37.299
┘</pre>
 
=={{header|C}}==
This implementation either reads two bearings from the console or a file containing a list of bearings. Usage printed on incorrect invocation.
<syntaxhighlight lang=C"c">
#include<stdlib.h>
#include<stdio.h>
Line 1,758 ⟶ 1,912:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
namespace Angle_difference_between_two_bearings
Line 1,859 ⟶ 2,013:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
using namespace std;
Line 1,914 ⟶ 2,068:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn angle-difference [a b]
(let [r (mod (- b a) 360)]
(if (>= r 180)
Line 1,928 ⟶ 2,082:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
******************************************************************
* COBOL solution to Angle difference challange
Line 2,081 ⟶ 2,235:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="common lisp">
(defun angle-difference (b1 b2)
(let ((diff (mod (- b2 b1) 360)))
Line 2,105 ⟶ 2,259:
 
</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">precision 4
 
define s1 = 0, s2 = 0
 
dim b1[20, -45, -85, -95, -45, -45, 29.4803, -78.3251]
dim b2[45, 45, 90, 90, 125, 145, -88.6381, -159.036]
 
arraysize s1, b1
arraysize s2, b2
 
if s1 = s2 then
 
for i = 0 to s1 - 1
 
let r = (b2[i] - b1[i]) % 360
 
if r >= 180 then
 
let r = r - 360
 
endif
 
print "bearing 1: ", b1[i], " bearing 2: ", b2[i], " difference: ", r
 
next i
 
endif</syntaxhighlight>
{{out| Output}}
<pre>bearing 1: 20 bearing 2: 45 difference: 25
bearing 1: -45 bearing 2: 45 difference: 90
bearing 1: -85 bearing 2: 90 difference: 175
bearing 1: -95 bearing 2: 90 difference: -175
bearing 1: -45 bearing 2: 125 difference: 170
bearing 1: -45 bearing 2: 145 difference: -170
bearing 1: 29.4803 bearing 2: -88.6381 difference: -118
bearing 1: -78.3251 bearing 2: -159.0360 difference: -80</pre>
 
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang=D"d">import std.stdio;
 
double getDifference(double b1, double b2) {
Line 2,160 ⟶ 2,352:
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight lang=easylang>
func angdiff a b .
r = (b - a) mod 360
if r < -180
r += 360
elif r >= 180
r -= 360
.
return r
.
proc pd a b . .
print b & " " & a & " -> " & angdiff a b
.
pd 20 45
pd -45 45
pd -85 90
pd -95 90
pd -45 125
pd -45 145
pd 29.4803 -88.6381
pd -78.3251 -159.036
pd -70099.74233810938 29840.67437876723
pd -165313.6666297357 33693.9894517456
pd 1174.8380510598456 -154146.66490124757
pd 60175.77306795546 42213.07192354373
</syntaxhighlight>
 
=={{header|Erlang}}==
The real number calculations are done using integer arithmetic to better handle
Line 2,165 ⟶ 2,386:
The module is tested by running the test function, which in turn matches expected results
with the result of function call.
<syntaxhighlight lang=Erlang"erlang">
-module(bearings).
 
Line 2,246 ⟶ 2,467:
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">ANGLEBETWEENBEARINGS
=LAMBDA(ab,
DEGREES(
Line 2,389 ⟶ 2,610:
 
=={{header|F Sharp|F#}}==
<syntaxhighlight lang="fsharp">let deltaBearing (b1:double) (b2:double) =
let r = (b2 - b1) % 360.0;
if r > 180.0 then
Line 2,430 ⟶ 2,651:
{{trans|F#}}
{{works with|Factor|0.99 development release 2019-03-17}}
<syntaxhighlight lang="factor">USING: combinators generalizations kernel math prettyprint ;
IN: rosetta-code.bearings
 
Line 2,474 ⟶ 2,695:
=={{header|Forth}}==
Developed with Gforth 0.7.9_20211014 using floating point math.
<syntaxhighlight lang="forth">
: Angle-Difference-stack ( b1 b2 - a +/-180)
\ Algorithm with stack manipulation without branches ( s. Frotran Groovy)
Line 2,524 ⟶ 2,745:
As it happens, the test data did not provoke any objections from the ACOSD function, but even so, a conversion to using ''arctan'' instead of ''arccos'' to recover angles would be safer. By using the four-quadrant ''arctan(x,y)'' function, the sign of the angle difference is also delivered and although that result could be in 0°-360° it turns out to be in ±180° as desired. On the other hand, the library of available functions did not include an arctan for complex parameters, so the complex number Z had to be split into its real and imaginary parts, thus requiring two appearances and to avoid repeated calculation, a temporary variable Z is needed. Otherwise, the statement could have been just <code>T = ATAN2D(Z1*CONJG(Z2))</code> and the whole calculation could be effected in one statement, <code>T = ATAN2D(CIS(90 - B1)*CONJG(CIS(90 - B2)))</code> And, since ''cis(t) = exp(i.t)'', <code>T = ATAN2D(EXP(CMPLX(0,90 - B1))*CONJG(EXP(CMPLX(0,90 - B2))))</code> - although using the arithmetic statement function does seem less intimidating.
 
The source style is F77 (even using the old-style arithmetic statement function) except for the convenience of generic functions taking the type of their parameters to save on the bother of DCMPLX instead of just CMPLX, etc. Floating-point constants in the test data are specified with ~D0, the exponential form that signifies double precision otherwise they would be taken as single precision values. Some compilers offer an option stating that all floating-point constants are to be taken as double precision. REAL*8 precision amounts to about sixteen decimal digits, so some of the supplied values will not be accurately represented, unless something beyond REAL*8 is available. <syntaxhighlight lang=Fortran"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,575 ⟶ 2,796:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 28-01-2019
' compile with: fbc -s console
 
Line 2,639 ⟶ 2,860:
1174.8380510598461 -154146.6649012475973 -161.5029523074336
60175.7730679554588 42213.0719235437282 37.2988555882694</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn GetDifference( b1 as float, b2 as float )
float r = ( b2 - b1 ) mod 360.0
if r >= 180.0 then r = r - 360.0
printf @"%9.1f\u00B0 %10.1f\u00B0 = %7.1f\u00B0", b1, b2, r
end fn
 
printf @"Input in -180 to +180 range:"
printf @"-----------------------------------"
printf @"%9s %12s %15s", "b1", "b2", "distance"
printf @"-----------------------------------"
fn GetDifference( 20.0, 45.0 )
fn GetDifference( -45.0, 45.0 )
fn GetDifference( -85.0, 90.0 )
fn GetDifference( -95.0, 90.0 )
fn GetDifference( -45.0, 125.0 )
fn GetDifference( -45.0, 145.0 )
fn GetDifference( -45.0, 125.0 )
fn GetDifference( -45.0, 145.0 )
fn GetDifference( 29.4803, -88.6381 )
fn GetDifference( -78.3251, -159.036 )
fn GetDifference( -70099.74233810938, 29840.67437876723 )
fn GetDifference( -165313.6666297357, 33693.9894517456 )
fn GetDifference( 1174.8380510598456, -154146.66490124757 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
-----------------------------------
b1 b2 distance
-----------------------------------
20.0° 45.0° = 25.0°
-45.0° 45.0° = 90.0°
-85.0° 90.0° = 175.0°
-95.0° 90.0° = -175.0°
-45.0° 125.0° = 170.0°
-45.0° 145.0° = -170.0°
-45.0° 125.0° = 170.0°
-45.0° 145.0° = -170.0°
29.5° -88.6° = -118.1°
-78.3° -159.0° = -80.7°
-70099.7° 29840.7° = -139.6°
-165313.7° 33694.0° = -72.3°
1174.8° -154146.7° = -161.5°
</pre>
 
=={{header|Go}}==
Line 2,644 ⟶ 2,913:
 
One feature of this solution is that if you can rely on the input bearings being in the range -180 to 180, you don't have to use math.Mod. Another feature is the bearing type and method syntax.
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 2,691 ⟶ 2,960:
 
A feature here is that the function body is a one-liner sufficient for the task test cases.
<syntaxhighlight lang="go">package main
 
import (
Line 2,741 ⟶ 3,010:
'''Solution A:'''
{{trans|C++}}
<syntaxhighlight lang="groovy">def angleDifferenceA(double b1, double b2) {
r = (b2 - b1) % 360.0
(r > 180.0 ? r - 360.0
Line 2,750 ⟶ 3,019:
'''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.
<syntaxhighlight lang="groovy">def angleDifferenceB(double b1, double b2) {
((b2 - b1) % 360.0 - 540.0) % 360.0 + 180.0
}</syntaxhighlight>
Line 2,756 ⟶ 3,025:
 
'''Test:'''
<syntaxhighlight lang="groovy">println " b1 b2 diff A diff B"
[
[b1: 20, b2: 45 ],
Line 2,791 ⟶ 3,060:
 
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell"haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
Line 2,850 ⟶ 3,119:
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang=IS"is-BASICbasic">100 INPUT PROMPT "1. angle: ":A1
110 INPUT PROMPT "2. angle: ":A2
120 LET B=MOD(A2-A1,360)
Line 2,858 ⟶ 3,127:
 
=={{header|J}}==
<syntaxhighlight lang="j">relativeBearing=: 180 - 360 | 180 + -
 
tests=: _99&".;._2 noun define
<syntaxhighlight lang=j>relativeBearing=: (180 -~ 360 | 180 + -~)/"1</syntaxhighlight>
<syntaxhighlight lang=j>tests=: _99&".;._2 noun define
20 45
-45 45
Line 2,873 ⟶ 3,142:
1174.8380510598456 -154146.66490124757
60175.77306795546 42213.07192354373
)</syntaxhighlight>
)
 
tests ,. relativeBearing tests
<pre> tests ,. relativeBearing/"1 tests
20 45 25
_45 45 90
Line 2,886 ⟶ 3,156:
_165314 33694 _72.3439
1174.84 _154147 _161.503
60175.8 42213.1 37.2989</syntaxhighlightpre>
 
=={{header|Java}}==
{{trans|C++}}
<syntaxhighlight lang="java">public class AngleDifference {
 
public static double getDifference(double b1, double b2) {
Line 2,944 ⟶ 3,214:
This approach should be reliable but it is also very inefficient.
 
<syntaxhighlight lang="javascript">function relativeBearing(b1Rad, b2Rad)
{
b1y = Math.cos(b1Rad);
Line 2,998 ⟶ 3,268:
===ES6===
 
<syntaxhighlight lang=JavaScript"javascript">(() => {
"use strict";
 
Line 3,083 ⟶ 3,353:
 
=={{header|Jsish}}==
<syntaxhighlight lang="javascript">/* Angle difference between bearings, in Jsish */
function angleDifference(bearing1:number, bearing2:number):number {
var angle = (bearing2 - bearing1) % 360;
Line 3,133 ⟶ 3,403:
 
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):
10000 as $scale
Line 3,198 ⟶ 3,468:
{{trans|Python}}
 
<syntaxhighlight lang="julia">using Printf
 
function angdiff(a, b)
Line 3,241 ⟶ 3,511:
 
=={{header|K}}==
<syntaxhighlight lang=K"k">
/ Angle difference between two angles
/ angledif.k
Line 3,276 ⟶ 3,546:
=={{header|Klingphix}}==
{{trans|NewLISP}}
<syntaxhighlight lang=Klingphix"klingphix">include ..\Utilitys.tlhy
 
:bearing sub 360 mod 540 add 360 mod 180 sub ;
Line 3,300 ⟶ 3,570:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
class Angle(d: Double) {
Line 3,356 ⟶ 3,626:
Each bearing will be stored in an object that inherits methods to accomplish all parts of the task: accept a new number of degrees, automatically adjusting to the range [-180, 180]; construct new bearing objects; subtract another bearing from itself and return the difference; construct a list of new bearing objects given a list of arbitrary degree sizes; and format the number of degrees into a modest human-readable format. Bearings will be zero-initialized by default if no degree size is provided.
 
<syntaxhighlight lang="lua">bearing = {degrees = 0} -- prototype object
 
function bearing:assign(angle)
Line 3,433 ⟶ 3,703:
=={{header|Maple}}==
{{trans|C++}}
<syntaxhighlight lang=Maple"maple">getDiff := proc(b1,b2)
local r:
r := frem(b2 - b1, 360):
Line 3,468 ⟶ 3,738:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang=Mathematica"mathematica">ClearAll[AngleDifference]
AngleDifference[b1_, b2_] := Mod[b2 - b1, 360, -180]
AngleDifference[20, 45]
Line 3,498 ⟶ 3,768:
=={{header|Modula-2}}==
{{trans|Java}}
<syntaxhighlight lang="modula2">FROM Terminal IMPORT *;
 
PROCEDURE WriteRealLn(value : REAL);
Line 3,546 ⟶ 3,816:
Taken from Racket solution
 
<syntaxhighlight 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,581 ⟶ 3,851:
=={{header|Nim}}==
 
<syntaxhighlight lang="nim">import math
import strutils
 
Line 3,628 ⟶ 3,898:
1174.84 -154146.66 -161.50
60175.77 42213.07 37.30
</pre>
 
=={{header|Nutt}}==
{{trans|Java}}
<syntaxhighlight lang="Nutt">
module main
imports native.io.output.say
 
funct get_difference(b1:Float,b2:Float):Float=
var r:Float=(b2-b1)%360.0
doif r< -180.0 r=r+360.0
doif r>=180.0 r=r-360.0
yield r
return
 
 
say("Input in -180 to +180 range")
say(get_difference(20.0,45.0))
say(get_difference(-45.0,45.0))
say(get_difference(-85.0,90.0))
say(get_difference(-95.0,90.0))
say(get_difference(-45.0,125.0))
say(get_difference(-45.0,145.0))
say(get_difference(-45.0,125.0))
say(get_difference(-45.0,145.0))
say(get_difference(29.4803,-88.6381))
say(get_difference(-78.3251,-159.036))
 
say("Input in wider range")
say(get_difference(-70099.74233810938,29840.67437876723))
say(get_difference(-165313.6666297357,33693.9894517456))
say(get_difference(1174.8380510598456,-154146.66490124757))
say(get_difference(60175.77306795546,42213.07192354373))
 
end
</syntaxhighlight>
 
{{output}}
<pre>
Input in -180 to +180 range
25.0
90.0
175.0
-175.0
170.0
-170.0
170.0
-170.0
-118.1184
-80.7109
Input in wider range
-139.58328312339
-72.3439185187
-161.5029523074156
37.29885558827
</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class AngleBearings {
function : Main(args : String[]) ~ Nil {
"Input in -180 to +180 range"->PrintLine();
Line 3,689 ⟶ 4,014:
=={{header|OCaml}}==
{{trans|D}}
<syntaxhighlight lang="ocaml">let get_diff b1 b2 =
let r = mod_float (b2 -. b1) 360.0 in
if r < -180.0
Line 3,736 ⟶ 4,061:
 
=={{header|PARI/GP}}==
<syntaxhighlight lang="parigp">centerliftmod1(x)=frac(x+1/2)-1/2;
anglediff(x,y)=centerliftmod1((y-x)/360)*360;
vecFunc(f)=v->call(f,v);
Line 3,747 ⟶ 4,072:
This program is meant to be saved in the same folder as a file <code>angles.txt</code> containing the input. Each pair of angles to subtract appears on its own line in the input file.
 
<syntaxhighlight lang=Pascal"pascal">
Program Bearings;
{ Reads pairs of angles from a file and subtracts them }
Line 3,829 ⟶ 4,154:
=={{header|Perl}}==
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 {
Line 3,873 ⟶ 4,198:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang=Phix"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,927 ⟶ 4,252:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
 
( "16" 1 "16" 1 "16" ) var al
Line 3,977 ⟶ 4,302:
examples with ''Get-Examples''. There is also a full help file accessed by typing
''Get-Help Get-AngleDiff -full''
<syntaxhighlight lang=PowerShell"powershell">
<#
.Synopsis
Line 4,115 ⟶ 4,440:
'''Results of user inputs'''<br>
using -45 and 98 with ''Get-AngleDiff'':
<syntaxhighlight lang=PowerShell"powershell">PS C:\WINDOWS\system32> Get-AngleDiff -45 98
The difference between -45 and 98 is 143</syntaxhighlight>
 
Line 4,141 ⟶ 4,466:
=={{header|PureBasic}}==
{{trans|Python}}
<syntaxhighlight lang=PureBasic"purebasic">Procedure.f getDifference (b1.f, b2.f)
r.f = Mod((b2 - b1), 360)
If r >= 180: r - 360
Line 4,170 ⟶ 4,495:
=={{header|Python}}==
{{trans|C++}}
<syntaxhighlight lang="python">from __future__ import print_function
def getDifference(b1, b2):
Line 4,220 ⟶ 4,545:
Or, generalising a little by deriving the degrees from a (Radians -> Radians) function, and formatting the output in columns:
{{Works with|Python|3.7}}
<syntaxhighlight lang="python">'''Difference between two bearings'''
 
from math import (acos, cos, pi, sin)
Line 4,326 ⟶ 4,651:
The table output use print formatting only available from python 3. Tested with 3.6.
{{Works with|Python|3.6}}
<syntaxhighlight lang="python">"""
Difference between two bearings
"""
Line 4,362 ⟶ 4,687:
Using the Quackery big number rational arithmetic library <code>bigrat.qky</code>.
 
<syntaxhighlight lang=Quackery"quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ v- -v
Line 4,412 ⟶ 4,737:
''see my comments in discussion regards bearing-heading or vice versa''
 
<syntaxhighlight lang="racket">#lang racket
(define (% a b) (- a (* b (truncate (/ a b)))))
 
Line 4,459 ⟶ 4,784:
{{works with|Rakudo|2016.11}}
 
<syntaxhighlight lang=perl6"raku" line>sub infix:<∠> (Real $b1, Real $b2) {
(my $b = ($b2 - $b1 + 720) % 360) > 180 ?? $b - 360 !! $b;
}
Line 4,493 ⟶ 4,818:
=={{header|REXX}}==
Some extra coding was added for a better visual presentation; &nbsp; the angles were centered, &nbsp; 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*/
call show 20, 45 /*display angular difference (deg).*/
Line 4,531 ⟶ 4,856:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Angle difference between two bearings
 
Line 4,567 ⟶ 4,892:
-118.1184
-80.7109
</pre>
 
=={{header|RPL}}==
≪ SWAP - 360 MOD
'''IF''' DUP 180 > '''THEN''' 360 - '''END''' ≫ 'BDIFF' STO
{{in}}
<pre>
20 45 BDIFF
-45 45 BDIFF
-85 90 BDIFF
-95 90 BDIFF
-45 125 BDIFF
-45 145 BDIFF
29.4803 -88.6381 BDIFF
-78.3251 -159.036 BDIFF
</pre>
{{out}}
<pre>
8: 25
7: 90
6 175
5: -175
4: 170
3: -170
2: -118.1184
1: -80.7109
</pre>
 
=={{header|Ruby}}==
{{trans|C++}}
<syntaxhighlight lang="ruby">def getDifference(b1, b2)
r = (b2 - b1) % 360.0
# Ruby modulus has same sign as divisor, which is positive here,
Line 4,620 ⟶ 4,971:
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">sub getDifference b1, b2
r = (b2 - b1) mod 360
if r >= 180 then r = r - 360
Line 4,643 ⟶ 4,994:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
/// Calculate difference between two bearings, in -180 to 180 degrees range
pub fn angle_difference(bearing1: f64, bearing2: f64) -> f64 {
Line 4,687 ⟶ 5,038:
=={{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)].
<syntaxhighlight lang=Scala"scala">object AngleDifference extends App {
private def getDifference(b1: Double, b2: Double) = {
val r = (b2 - b1) % 360.0
Line 4,715 ⟶ 5,066:
=={{header|Scheme}}==
R6RS standard.
<syntaxhighlight lang="scheme">#!r6rs
 
(import (rnrs base (6))
Line 4,768 ⟶ 5,119:
which can be used to solve this task easily.
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,822 ⟶ 5,173:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func bearingAngleDiff(b1, b2) {
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b
}
Line 4,865 ⟶ 5,216:
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">func angleDifference(a1: Double, a2: Double) -> Double {
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360)
 
Line 4,899 ⟶ 5,250:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
proc angleDiff {b1 b2} {
set angle [::tcl::mathfunc::fmod [expr ($b2 - $b1)] 360]
Line 4,950 ⟶ 5,301:
 
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function tx(a As Variant) As String
Dim s As String
s = CStr(Format(a, "0.######"))
Line 5,001 ⟶ 5,352:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal
Line 5,066 ⟶ 5,417:
37.2988555882</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">import math
type Bearing = f64
Line 5,128 ⟶ 5,479:
 
=={{header|Wren}}==
<syntaxhighlight lang=ecmascript"wren">var subtract = Fn.new { |b1, b2|
var d = (b2 - b1) % 360
if (d < -180) d = d + 360
Line 5,177 ⟶ 5,528:
 
=={{header|XBS}}==
<syntaxhighlight lang="xbs">settype Bearing = {Angle:number}
class Bearing {
private method construct(Angle:number=0)
Line 5,227 ⟶ 5,578:
Pairs of bearing angles are input from the console or from a file
(terminated with Ctrl+C) redirected on the command line.
<syntaxhighlight lang=XPL0"xpl0">real B1, B2, Ang;
[Text(0, " Bearing 1 Bearing 2 Difference");
loop [B1:= RlIn(1);
Line 5,260 ⟶ 5,611:
=={{header|Yabasic}}==
{{trans|Python}}
<syntaxhighlight lang="freebasic">// Rosetta Code problem: http://rosettacode.org/wiki/Angle_difference_between_two_bearings
// by Jjuanhdez, 06/2022
 
Line 5,288 ⟶ 5,639:
=={{header|zkl}}==
{{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;
}</syntaxhighlight>
<syntaxhighlight 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;"
3,032

edits