WeixiangYan/CodeTransOcean huggingface dataset:
Task: Angle difference between two bearings

C: #include<stdlib.h>
#include<stdio.h>
#include<math.h>

void processFile(char* name){
	
	int i,records;
	double diff,b1,b2;
	FILE* fp = fopen(name,"r");
	
	fscanf(fp,"%d\n",&records);
	
	for(i=0;i<records;i++){
		fscanf(fp,"%lf%lf",&b1,&b2);
		
		diff = fmod(b2-b1,360.0);
		printf("\nDifference between b2(%lf) and b1(%lf) is %lf",b2,b1,(diff<-180)?diff+360:((diff>=180)?diff-360:diff));	
	}
	
	fclose(fp);
}

int main(int argC,char* argV[])
{
	double diff;
	
	if(argC < 2)
		printf("Usage : %s <bearings separated by a space OR full file name which contains the bearing list>",argV[0]);
	else if(argC == 2)
		processFile(argV[1]);
	else{
		diff = fmod(atof(argV[2])-atof(argV[1]),360.0);
		printf("Difference between b2(%s) and b1(%s) is %lf",argV[2],argV[1],(diff<-180)?diff+360:((diff>=180)?diff-360:diff));
	}

	return 0;
}











Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings


C++: #include <cmath>
#include <iostream>
using namespace std;

double getDifference(double b1, double b2) {
	double r = fmod(b2 - b1, 360.0);
	if (r < -180.0)
		r += 360.0;
	if (r >= 180.0)
		r -= 360.0;
	return r;
}

int main()
{
	cout << "Input in -180 to +180 range" << endl;
	cout << getDifference(20.0, 45.0) << endl;
	cout << getDifference(-45.0, 45.0) << endl;
	cout << getDifference(-85.0, 90.0) << endl;
	cout << getDifference(-95.0, 90.0) << endl;
	cout << getDifference(-45.0, 125.0) << endl;
	cout << getDifference(-45.0, 145.0) << endl;
	cout << getDifference(-45.0, 125.0) << endl;
	cout << getDifference(-45.0, 145.0) << endl;
	cout << getDifference(29.4803, -88.6381) << endl;
	cout << getDifference(-78.3251, -159.036) << endl;
	
	cout << "Input in wider range" << endl;
	cout << getDifference(-70099.74233810938, 29840.67437876723) << endl;
	cout << getDifference(-165313.6666297357, 33693.9894517456) << endl;
	cout << getDifference(1174.8380510598456, -154146.66490124757) << endl;
	cout << getDifference(60175.77306795546, 42213.07192354373) << endl;

	return 0;
}










Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings




Go: package main

import "fmt"

type bearing float64

var testCases = []struct{ b1, b2 bearing }{
    {20, 45},
    {-45, 45},
    {-85, 90},
    {-95, 90},
    {-45, 125},
    {-45, 145},
    {29.4803, -88.6381},
    {-78.3251, -159.036},
}

func main() {
    for _, tc := range testCases {
        fmt.Println(tc.b2.Sub(tc.b1))
    }
}

func (b2 bearing) Sub(b1 bearing) bearing {
    switch d := b2 - b1; {
    case d < -180:
        return d + 360
    case d > 180:
        return d - 360
    default:
        return d
    }
}








Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings





Java: public class AngleDifference {

    public static double getDifference(double b1, double b2) {
        double r = (b2 - b1) % 360.0;
        if (r < -180.0)
            r += 360.0;
        if (r >= 180.0)
            r -= 360.0;
        return r;
    }

    public static void main(String[] args) {
        System.out.println("Input in -180 to +180 range");
        System.out.println(getDifference(20.0, 45.0));
        System.out.println(getDifference(-45.0, 45.0));
        System.out.println(getDifference(-85.0, 90.0));
        System.out.println(getDifference(-95.0, 90.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(29.4803, -88.6381));
        System.out.println(getDifference(-78.3251, -159.036));

        System.out.println("Input in wider range");
        System.out.println(getDifference(-70099.74233810938, 29840.67437876723));
        System.out.println(getDifference(-165313.6666297357, 33693.9894517456));
        System.out.println(getDifference(1174.8380510598456, -154146.66490124757));
        System.out.println(getDifference(60175.77306795546, 42213.07192354373));
    }
}







Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings







Python: from __future__ import print_function
 
def getDifference(b1, b2):
	r = (b2 - b1) % 360.0
	
	
	if r >= 180.0:
		r -= 360.0
	return r
 
if __name__ == "__main__":
	print ("Input in -180 to +180 range")
	print (getDifference(20.0, 45.0))
	print (getDifference(-45.0, 45.0))
	print (getDifference(-85.0, 90.0))
	print (getDifference(-95.0, 90.0))
	print (getDifference(-45.0, 125.0))
	print (getDifference(-45.0, 145.0))
	print (getDifference(-45.0, 125.0))
	print (getDifference(-45.0, 145.0))
	print (getDifference(29.4803, -88.6381))
	print (getDifference(-78.3251, -159.036))
 
	print ("Input in wider range")
	print (getDifference(-70099.74233810938, 29840.67437876723))
	print (getDifference(-165313.6666297357, 33693.9894517456))
	print (getDifference(1174.8380510598456, -154146.66490124757))
	print (getDifference(60175.77306795546, 42213.07192354373))





Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings








VB: Private Function tx(a As Variant) As String
    Dim s As String
    s = CStr(Format(a, "0.######"))
    If Right(s, 1) = "," Then
        s = Mid(s, 1, Len(s) - 1) & "       "
    Else
        i = InStr(1, s, ",")
        s = s & String$(6 - Len(s) + i, " ")
    End If
    tx = s
End Function
Private Sub test(b1 As Variant, b2 As Variant)
    Dim diff As Variant
    diff = (b2 - b1) - ((b2 - b1) \ 360) * 360
    diff = diff - IIf(diff > 180, 360, 0)
    diff = diff + IIf(diff < -180, 360, 0)
    Debug.Print Format(tx(b1), "@@@@@@@@@@@@@@@@"); Format(tx(b2), "@@@@@@@@@@@@@@@@@"); Format(tx(diff), "@@@@@@@@@@@@@@@@@")
End Sub
Public Sub angle_difference()
    Debug.Print "       b1               b2             diff"
    Debug.Print "---------------- ---------------- ----------------"
    test 20, 45
    test -45, 45
    test -85, 90
    test -95, 90
    test -45, 125
    test -45, 145
    test 29.4803, -88.6381
    test -78.3251, -159.036
    test -70099.7423381094, 29840.6743787672
    test -165313.666629736, 33693.9894517456
    test 1174.83805105985, -154146.664901248
    test 60175.7730679555, 42213.0719235437
End Sub




Ada: 


with Ada.Text_IO; use Ada.Text_IO;

procedure Bearing_Angles is
   type Real is digits 8;
   Package Real_Io is new Ada.Text_IO.Float_IO(Real);
   use Real_IO;
   type Angles is record
      B1 : Real;
      B2 : Real;
   end record;
   type Angle_Arr is array(Positive range <>) of Angles;
   
   function fmod(Left, Right : Real) return Real is
      Result : Real;
   begin
      Result := Left - Right*Real'Truncation(Left / Right);
      return Result;
   end fmod;
   
   The_Angles : Angle_Arr := ((20.0,45.0),(-45.0, 45.0), (-85.0, 90.0),
                              (-95.0, 90.0), (-14.0, 125.0), (29.4803, -88.6381),
                              (-78.3251, -159.036),
                              (-70099.74233810938, 29840.67437876723),
                              (-165313.6666297357, 33693.9894517456),
                              (1174.8380510598456, -154146.66490124757),
                              (60175.77306795546, 42213.07192354373));
   Diff : Real;
   
begin
   
   for A of The_Angles loop
      Diff := fmod(A.b2 - A.b1, 360.0);
      If Diff < -180.0 then
         Diff := Diff + 360.0;
      elsif Diff > 180.0 then
         Diff := Diff - 360.0;
      end if;
      
      Put("Difference between ");
      Put(Item => A.B2, Fore => 7, Aft => 4, Exp => 0);
      Put(" and ");
      Put(Item => A.B1, Fore => 7, Aft => 4, Exp => 0);
      Put(" is ");
      Put(Item => Diff, Fore => 4, Aft => 4, Exp => 0);
      New_Line;
   end loop;
   
end Bearing_Angles;
Task: Angle difference between two bearings

C: #include<stdlib.h>
#include<stdio.h>
#include<math.h>

void processFile(char* name){
	
	int i,records;
	double diff,b1,b2;
	FILE* fp = fopen(name,"r");
	
	fscanf(fp,"%d\n",&records);
	
	for(i=0;i<records;i++){
		fscanf(fp,"%lf%lf",&b1,&b2);
		
		diff = fmod(b2-b1,360.0);
		printf("\nDifference between b2(%lf) and b1(%lf) is %lf",b2,b1,(diff<-180)?diff+360:((diff>=180)?diff-360:diff));	
	}
	
	fclose(fp);
}

int main(int argC,char* argV[])
{
	double diff;
	
	if(argC < 2)
		printf("Usage : %s <bearings separated by a space OR full file name which contains the bearing list>",argV[0]);
	else if(argC == 2)
		processFile(argV[1]);
	else{
		diff = fmod(atof(argV[2])-atof(argV[1]),360.0);
		printf("Difference between b2(%s) and b1(%s) is %lf",argV[2],argV[1],(diff<-180)?diff+360:((diff>=180)?diff-360:diff));
	}

	return 0;
}
Task: Angle difference between two bearings



C#: using System;

namespace Angle_difference_between_two_bearings
{
	class Program
	{
		public static void Main(string[] args)
		{
			Console.WriteLine();
			Console.WriteLine("Hello World!");
			Console.WriteLine();
			
			
			Console.WriteLine(Delta_Bearing( 20M,45));
			Console.WriteLine(Delta_Bearing(-45M,45M));
			Console.WriteLine(Delta_Bearing(-85M,90M));
			Console.WriteLine(Delta_Bearing(-95M,90M));
			Console.WriteLine(Delta_Bearing(-45M,125M));
			Console.WriteLine(Delta_Bearing(-45M,145M));
			Console.WriteLine(Delta_Bearing( 29.4803M,-88.6381M));
			Console.WriteLine(Delta_Bearing(-78.3251M, -159.036M));
			
			
			Console.WriteLine(Delta_Bearing(-70099.74233810938M,   29840.67437876723M));
			Console.WriteLine(Delta_Bearing(-165313.6666297357M,   33693.9894517456M));
			Console.WriteLine(Delta_Bearing( 1174.8380510598456M, -154146.66490124757M));
			Console.WriteLine(Delta_Bearing( 60175.77306795546M,   42213.07192354373M));
			
			Console.WriteLine();
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
		
		static decimal Delta_Bearing(decimal b1, decimal b2)
		{
			
			
			
			
			
			
			decimal d = 0;
			
			
			if(b1<0)
				b1 += 360;
			if(b2<0)
				b2 += 360;
			
			
			
			
			d = (b2 - b1)%360;
			
			if(d>180)
				d -= 360;
			else if(d<-180)
				d += 360;
			
			return d;
			
			
			
			
		}
	}
}
Task: Angle difference between two bearings


C++: #include <cmath>
#include <iostream>
using namespace std;

double getDifference(double b1, double b2) {
	double r = fmod(b2 - b1, 360.0);
	if (r < -180.0)
		r += 360.0;
	if (r >= 180.0)
		r -= 360.0;
	return r;
}

int main()
{
	cout << "Input in -180 to +180 range" << endl;
	cout << getDifference(20.0, 45.0) << endl;
	cout << getDifference(-45.0, 45.0) << endl;
	cout << getDifference(-85.0, 90.0) << endl;
	cout << getDifference(-95.0, 90.0) << endl;
	cout << getDifference(-45.0, 125.0) << endl;
	cout << getDifference(-45.0, 145.0) << endl;
	cout << getDifference(-45.0, 125.0) << endl;
	cout << getDifference(-45.0, 145.0) << endl;
	cout << getDifference(29.4803, -88.6381) << endl;
	cout << getDifference(-78.3251, -159.036) << endl;
	
	cout << "Input in wider range" << endl;
	cout << getDifference(-70099.74233810938, 29840.67437876723) << endl;
	cout << getDifference(-165313.6666297357, 33693.9894517456) << endl;
	cout << getDifference(1174.8380510598456, -154146.66490124757) << endl;
	cout << getDifference(60175.77306795546, 42213.07192354373) << endl;

	return 0;
}
Task: Angle difference between two bearings





Java: public class AngleDifference {

    public static double getDifference(double b1, double b2) {
        double r = (b2 - b1) % 360.0;
        if (r < -180.0)
            r += 360.0;
        if (r >= 180.0)
            r -= 360.0;
        return r;
    }

    public static void main(String[] args) {
        System.out.println("Input in -180 to +180 range");
        System.out.println(getDifference(20.0, 45.0));
        System.out.println(getDifference(-45.0, 45.0));
        System.out.println(getDifference(-85.0, 90.0));
        System.out.println(getDifference(-95.0, 90.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(-45.0, 125.0));
        System.out.println(getDifference(-45.0, 145.0));
        System.out.println(getDifference(29.4803, -88.6381));
        System.out.println(getDifference(-78.3251, -159.036));

        System.out.println("Input in wider range");
        System.out.println(getDifference(-70099.74233810938, 29840.67437876723));
        System.out.println(getDifference(-165313.6666297357, 33693.9894517456));
        System.out.println(getDifference(1174.8380510598456, -154146.66490124757));
        System.out.println(getDifference(60175.77306795546, 42213.07192354373));
    }
}
Task: Angle difference between two bearings







Python: from __future__ import print_function
 
def getDifference(b1, b2):
	r = (b2 - b1) % 360.0
	
	
	if r >= 180.0:
		r -= 360.0
	return r
 
if __name__ == "__main__":
	print ("Input in -180 to +180 range")
	print (getDifference(20.0, 45.0))
	print (getDifference(-45.0, 45.0))
	print (getDifference(-85.0, 90.0))
	print (getDifference(-95.0, 90.0))
	print (getDifference(-45.0, 125.0))
	print (getDifference(-45.0, 145.0))
	print (getDifference(-45.0, 125.0))
	print (getDifference(-45.0, 145.0))
	print (getDifference(29.4803, -88.6381))
	print (getDifference(-78.3251, -159.036))
 
	print ("Input in wider range")
	print (getDifference(-70099.74233810938, 29840.67437876723))
	print (getDifference(-165313.6666297357, 33693.9894517456))
	print (getDifference(1174.8380510598456, -154146.66490124757))
	print (getDifference(60175.77306795546, 42213.07192354373))
Task: Angle difference between two bearings








VB: Private Function tx(a As Variant) As String
    Dim s As String
    s = CStr(Format(a, "0.######"))
    If Right(s, 1) = "," Then
        s = Mid(s, 1, Len(s) - 1) & "       "
    Else
        i = InStr(1, s, ",")
        s = s & String$(6 - Len(s) + i, " ")
    End If
    tx = s
End Function
Private Sub test(b1 As Variant, b2 As Variant)
    Dim diff As Variant
    diff = (b2 - b1) - ((b2 - b1) \ 360) * 360
    diff = diff - IIf(diff > 180, 360, 0)
    diff = diff + IIf(diff < -180, 360, 0)
    Debug.Print Format(tx(b1), "@@@@@@@@@@@@@@@@"); Format(tx(b2), "@@@@@@@@@@@@@@@@@"); Format(tx(diff), "@@@@@@@@@@@@@@@@@")
End Sub
Public Sub angle_difference()
    Debug.Print "       b1               b2             diff"
    Debug.Print "---------------- ---------------- ----------------"
    test 20, 45
    test -45, 45
    test -85, 90
    test -95, 90
    test -45, 125
    test -45, 145
    test 29.4803, -88.6381
    test -78.3251, -159.036
    test -70099.7423381094, 29840.6743787672
    test -165313.666629736, 33693.9894517456
    test 1174.83805105985, -154146.664901248
    test 60175.7730679555, 42213.0719235437
End Sub
Task: Angle difference between two bearings




Go: package main

import "fmt"

type bearing float64

var testCases = []struct{ b1, b2 bearing }{
    {20, 45},
    {-45, 45},
    {-85, 90},
    {-95, 90},
    {-45, 125},
    {-45, 145},
    {29.4803, -88.6381},
    {-78.3251, -159.036},
}

func main() {
    for _, tc := range testCases {
        fmt.Println(tc.b2.Sub(tc.b1))
    }
}

func (b2 bearing) Sub(b1 bearing) bearing {
    switch d := b2 - b1; {
    case d < -180:
        return d + 360
    case d > 180:
        return d - 360
    default:
        return d
    }
}