WeixiangYan/CodeTransOcean huggingface dataset:
Task: QR decomposition




Go: package main

import (
    "fmt"
    "math"

    "github.com/skelterjohn/go.matrix"
)

func sign(s float64) float64 {
    if s > 0 {
        return 1
    } else if s < 0 {
        return -1
    }
    return 0
}

func unitVector(n int) *matrix.DenseMatrix {
    vec := matrix.Zeros(n, 1)
    vec.Set(0, 0, 1)
    return vec
}

func householder(a *matrix.DenseMatrix) *matrix.DenseMatrix {
    m := a.Rows()
    s := sign(a.Get(0, 0))
    e := unitVector(m)
    u := matrix.Sum(a, matrix.Scaled(e, a.TwoNorm()*s))
    v := matrix.Scaled(u, 1/u.Get(0, 0))
    
    prod, _ := v.Transpose().TimesDense(v)
    β := 2 / prod.Get(0, 0)

    prod, _ = v.TimesDense(v.Transpose())
    return matrix.Difference(matrix.Eye(m), matrix.Scaled(prod, β))
}

func qr(a *matrix.DenseMatrix) (q, r *matrix.DenseMatrix) {
    m := a.Rows()
    n := a.Cols()
    q = matrix.Eye(m)

    last := n - 1
    if m == n {
        last--
    }
    for i := 0; i <= last; i++ {
        
        b := a.GetMatrix(i, i, m-i, n-i).Copy()
        x := b.GetColVector(0)
        h := matrix.Eye(m)
        h.SetMatrix(i, i, householder(x))
        q, _ = q.TimesDense(h)
        a, _ = h.TimesDense(a)
    }
    return q, a
}

func main() {
    
    a := matrix.MakeDenseMatrixStacked([][]float64{
        {12, -51, 4},
        {6, 167, -68},
        {-4, 24, -41}})
    q, r := qr(a)
    fmt.Println("q:\n", q)
    fmt.Println("r:\n", r)

    
    x := matrix.MakeDenseMatrixStacked([][]float64{
        {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}})
    y := matrix.MakeDenseMatrixStacked([][]float64{
        {1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321}})
    fmt.Println("\npolyfit:\n", polyfit(x, y, 2))
}

func polyfit(x, y *matrix.DenseMatrix, n int) *matrix.DenseMatrix {
    m := x.Cols()
    a := matrix.Zeros(m, n+1)
    for i := 0; i < m; i++ {
        for j := 0; j <= n; j++ {
            a.Set(i, j, math.Pow(x.Get(0, i), float64(j)))
        }
    }
    return lsqr(a, y.Transpose())
}

func lsqr(a, b *matrix.DenseMatrix) *matrix.DenseMatrix {
    q, r := qr(a)
    n := r.Cols()
    prod, _ := q.Transpose().TimesDense(b)
    return solveUT(r.GetMatrix(0, 0, n, n), prod.GetMatrix(0, 0, n, 1))
}

func solveUT(r, b *matrix.DenseMatrix) *matrix.DenseMatrix {
    n := r.Cols()
    x := matrix.Zeros(n, 1)
    for k := n - 1; k >= 0; k-- {
        sum := 0.
        for j := k + 1; j < n; j++ {
            sum += r.Get(k, j) * x.Get(j, 0)
        }
        x.Set(k, 0, (b.Get(k, 0)-sum)/r.Get(k, k))
    }
    return x
}


Java: import Jama.Matrix;
import Jama.QRDecomposition;

public class Decompose {
    public static void main(String[] args) {
        var matrix = new Matrix(new double[][] {
            {12, -51,   4},
            { 6, 167, -68},
            {-4,  24, -41},
        });

        var qr = new QRDecomposition(matrix);
        qr.getQ().print(10, 4);
        qr.getR().print(10, 4);
    }
}
Task: QR decomposition





Java: import Jama.Matrix;
import Jama.QRDecomposition;

public class Decompose {
    public static void main(String[] args) {
        var matrix = new Matrix(new double[][] {
            {12, -51,   4},
            { 6, 167, -68},
            {-4,  24, -41},
        });

        var qr = new QRDecomposition(matrix);
        qr.getQ().print(10, 4);
        qr.getR().print(10, 4);
    }
}



Python: 

import numpy as np

def qr(A):
    m, n = A.shape
    Q = np.eye(m)
    for i in range(n - (m == n)):
        H = np.eye(m)
        H[i:, i:] = make_householder(A[i:, i])
        Q = np.dot(Q, H)
        A = np.dot(H, A)
    return Q, A

def make_householder(a):
    v = a / (a[0] + np.copysign(np.linalg.norm(a), a[0]))
    v[0] = 1
    H = np.eye(a.shape[0])
    H -= (2 / np.dot(v, v)) * np.dot(v[:, None], v[None, :])
    return H


a = np.array(((
    (12, -51,   4),
    ( 6, 167, -68),
    (-4,  24, -41),
)))

q, r = qr(a)
print('q:\n', q.round(6))
print('r:\n', r.round(6))


def polyfit(x, y, n):
    return lsqr(x[:, None]**np.arange(n + 1), y.T)

def lsqr(a, b):
    q, r = qr(a)
    _, n = r.shape
    return np.linalg.solve(r[:n, :], np.dot(q.T, b)[:n])

x = np.array((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
y = np.array((1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321))

print('\npolyfit:\n', polyfit(x, y, 2))
Task: QR decomposition



C#: using System;
using MathNet.Numerics.LinearAlgebra;
using MathNet.Numerics.LinearAlgebra.Double;


class Program
{

    static void Main(string[] args)
    {
        Matrix<double> A = DenseMatrix.OfArray(new double[,]
        {
                {  12,  -51,    4 },
                {   6,  167,  -68 },
                {  -4,   24,  -41 }
        });
        Console.WriteLine("A:");
        Console.WriteLine(A);
        var qr = A.QR();
        Console.WriteLine();
        Console.WriteLine("Q:");
        Console.WriteLine(qr.Q);
        Console.WriteLine();
        Console.WriteLine("R:");
        Console.WriteLine(qr.R);
    }
}



Java: import Jama.Matrix;
import Jama.QRDecomposition;

public class Decompose {
    public static void main(String[] args) {
        var matrix = new Matrix(new double[][] {
            {12, -51,   4},
            { 6, 167, -68},
            {-4,  24, -41},
        });

        var qr = new QRDecomposition(matrix);
        qr.getQ().print(10, 4);
        qr.getR().print(10, 4);
    }
}
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);





Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);







Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);








VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);


C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);



Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);






Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);


Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}
Task: Loops_Do-while



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);





Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);






VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while




Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}


Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);
Task: Loops_Do-while





Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);




VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while




Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}




Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);




Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);




Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);
Task: Loops_Do-while



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);



Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);
Task: Loops_Do-while




Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}





VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while







Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break


VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);







VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
Task: Loops_Do-while





Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);



Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while

C: int val = 0;
do{
   val++;
   printf("%d\n",val);
}while(val % 6 != 0);






PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);
Task: Loops_Do-while



C#: int a = 0;

do
{
    a += 1;
    Console.WriteLine(a);
} while (a % 6 != 0);




PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);
Task: Loops_Do-while


C++: int val = 0;
do{
   val++;
   std::cout << val << std::endl;
}while(val % 6 != 0);





PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);
Task: Loops_Do-while




Go: package main

import "fmt"

func main() {
	var value int
	for {
		value++
		fmt.Println(value)
                if value%6 != 0 {
                        break
                }
	}
}



PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);
Task: Loops_Do-while





Java: int val = 0;
do{
   val++;
   System.out.println(val);
}while(val % 6 != 0);


PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);
Task: Loops_Do-while






PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);


Python: val = 0
while True:
   val +=1
   print val
   if val % 6 == 0: break
Task: Loops_Do-while






PHP: $val = 0;
do {
   $val++;
   print "$val\n";
} while ($val % 6 != 0);



VB: Public Sub LoopDoWhile()
    Dim value As Integer
    value = 0
    Do
        value = value + 1
        Debug.Print value;
    Loop While value Mod 6 <> 0
End Sub
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;
}


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

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;
}




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
    }
}
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;
}





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

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;
}







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

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;
}








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


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;
}




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));
    }
}