Angle difference between two bearings: Difference between revisions

m
Automated syntax highlighting fixup (second round - minor fixes)
m (syntax highlighting fixup automation)
m (Automated syntax highlighting fixup (second round - minor fixes))
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 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.
<syntaxhighlight lang=APL"apl">[0] D←B1 DIFF B2
[1] D←180+¯360|180+B2-B1
</syntaxhighlight>
Line 895:
=={{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:
=={{header|Arturo}}==
{{trans|Ruby}}
<syntaxhighlight lang="rebol">getDifference: function [b1, b2][
r: (b2 - b1) % 360.0
 
Line 1,467:
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang=AutoHotkey"autohotkey">Angles:= [[20, 45]
,[-45, 45]
,[-85, 90]
Line 1,503:
 
=={{header|AWK}}==
<syntaxhighlight lang=AWK"awk">
# syntax: GAWK -f ANGLE_DIFFERENCE_BETWEEN_TWO_BEARINGS.AWK
BEGIN {
Line 1,560:
{{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:
 
==={{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:
=={{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:
 
=={{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,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"c">
#include<stdlib.h>
#include<stdio.h>
Line 1,758:
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">using System;
 
namespace Angle_difference_between_two_bearings
Line 1,859:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cmath>
#include <iostream>
using namespace std;
Line 1,914:
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn angle-difference [a b]
(let [r (mod (- b a) 360)]
(if (>= r 180)
Line 1,928:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
******************************************************************
* COBOL solution to Angle difference challange
Line 2,081:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="common lisp">
(defun angle-difference (b1 b2)
(let ((diff (mod (- b2 b1) 360)))
Line 2,108:
=={{header|D}}==
{{trans|Java}}
<syntaxhighlight lang=D"d">import std.stdio;
 
double getDifference(double b1, double b2) {
Line 2,165:
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:
 
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">ANGLEBETWEENBEARINGS
=LAMBDA(ab,
DEGREES(
Line 2,389:
 
=={{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:
{{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:
=={{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:
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:
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 28-01-2019
' compile with: fbc -s console
 
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.
<syntaxhighlight lang="go">package main
 
import "fmt"
Line 2,691:
 
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:
'''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:
'''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:
 
'''Test:'''
<syntaxhighlight lang="groovy">println " b1 b2 diff A diff B"
[
[b1: 20, b2: 45 ],
Line 2,791:
 
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell"haskell">import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
Line 2,850:
 
=={{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,859:
=={{header|J}}==
 
<syntaxhighlight lang="j">relativeBearing=: (180 -~ 360 | 180 + -~)/"1</syntaxhighlight>
<syntaxhighlight lang="j">tests=: _99&".;._2 noun define
20 45
-45 45
Line 2,890:
=={{header|Java}}==
{{trans|C++}}
<syntaxhighlight lang="java">public class AngleDifference {
 
public static double getDifference(double b1, double b2) {
Line 2,944:
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:
===ES6===
 
<syntaxhighlight lang=JavaScript"javascript">(() => {
"use strict";
 
Line 3,083:
 
=={{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:
 
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:
{{trans|Python}}
 
<syntaxhighlight lang="julia">using Printf
 
function angdiff(a, b)
Line 3,241:
 
=={{header|K}}==
<syntaxhighlight lang=K"k">
/ Angle difference between two angles
/ angledif.k
Line 3,276:
=={{header|Klingphix}}==
{{trans|NewLISP}}
<syntaxhighlight lang=Klingphix"klingphix">include ..\Utilitys.tlhy
 
:bearing sub 360 mod 540 add 360 mod 180 sub ;
Line 3,300:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.2
 
class Angle(d: Double) {
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.
 
<syntaxhighlight lang="lua">bearing = {degrees = 0} -- prototype object
 
function bearing:assign(angle)
Line 3,433:
=={{header|Maple}}==
{{trans|C++}}
<syntaxhighlight lang=Maple"maple">getDiff := proc(b1,b2)
local r:
r := frem(b2 - b1, 360):
Line 3,468:
 
=={{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:
=={{header|Modula-2}}==
{{trans|Java}}
<syntaxhighlight lang="modula2">FROM Terminal IMPORT *;
 
PROCEDURE WriteRealLn(value : REAL);
Line 3,546:
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:
=={{header|Nim}}==
 
<syntaxhighlight lang="nim">import math
import strutils
 
Line 3,632:
=={{header|Objeck}}==
{{trans|Java}}
<syntaxhighlight lang="objeck">class AngleBearings {
function : Main(args : String[]) ~ Nil {
"Input in -180 to +180 range"->PrintLine();
Line 3,689:
=={{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:
 
=={{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:
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:
=={{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:
 
=={{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:
 
=={{header|Phixmonti}}==
<syntaxhighlight lang=Phixmonti"phixmonti">include ..\Utilitys.pmt
 
( "16" 1 "16" 1 "16" ) var al
Line 3,977:
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:
'''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:
=={{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:
=={{header|Python}}==
{{trans|C++}}
<syntaxhighlight lang="python">from __future__ import print_function
def getDifference(b1, b2):
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}}
<syntaxhighlight lang="python">'''Difference between two bearings'''
 
from math import (acos, cos, pi, sin)
Line 4,326:
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:
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:
''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:
{{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:
=={{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:
 
=={{header|Ring}}==
<syntaxhighlight lang="ring">
# Project : Angle difference between two bearings
 
Line 4,571:
=={{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:
 
=={{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:
 
=={{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:
=={{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:
=={{header|Scheme}}==
R6RS standard.
<syntaxhighlight lang="scheme">#!r6rs
 
(import (rnrs base (6))
Line 4,768:
which can be used to solve this task easily.
 
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "float.s7i";
 
Line 4,822:
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func bearingAngleDiff(b1, b2) {
(var b = ((b2 - b1 + 720) % 360)) > 180 ? (b - 360) : b
}
Line 4,865:
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">func angleDifference(a1: Double, a2: Double) -> Double {
let diff = (a2 - a1).truncatingRemainder(dividingBy: 360)
 
Line 4,899:
 
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
proc angleDiff {b1 b2} {
set angle [::tcl::mathfunc::fmod [expr ($b2 - $b1)] 360]
Line 4,950:
 
=={{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:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Module Module1
 
Function Delta_Bearing(b1 As Decimal, b2 As Decimal) As Decimal
Line 5,068:
=={{header|Vlang}}==
{{trans|go}}
<syntaxhighlight lang="vlang">import math
type Bearing = f64
Line 5,128:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascript">var subtract = Fn.new { |b1, b2|
var d = (b2 - b1) % 360
if (d < -180) d = d + 360
Line 5,177:
 
=={{header|XBS}}==
<syntaxhighlight lang="xbs">settype Bearing = {Angle:number}
class Bearing {
private method construct(Angle:number=0)
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.
<syntaxhighlight lang=XPL0"xpl0">real B1, B2, Ang;
[Text(0, " Bearing 1 Bearing 2 Difference");
loop [B1:= RlIn(1);
Line 5,260:
=={{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:
=={{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;"
10,327

edits