Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Convert this Icon block to VB, preserving its control flow and logic.
link printf procedure main() printf("BNA to LAX is %d km (%d miles)\n", d := gcdistance([36.12, -86.67],[33.94, -118.40]),d*3280/5280) end procedure gcdistance(a,b) a[2] -:= b[2] every (x := a|b)[i := 1 to 2] := dtor(x[i]) dz := sin(a[1]) - sin(b[1]) dx := cos(a[2]) * cos(a[1]) - cos(b[1]) dy := sin(a[2]) * cos(a[1]) return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * 6371 end
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Please provide an equivalent version of this Icon code in Go.
link printf procedure main() printf("BNA to LAX is %d km (%d miles)\n", d := gcdistance([36.12, -86.67],[33.94, -118.40]),d*3280/5280) end procedure gcdistance(a,b) a[2] -:= b[2] every (x := a|b)[i := 1 to 2] := dtor(x[i]) dz := sin(a[1]) - sin(b[1]) dx := cos(a[2]) * cos(a[1]) - cos(b[1]) dy := sin(a[2]) * cos(a[1]) return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * 6371 end
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Preserve the algorithm and functionality while converting the code from Icon to Go.
link printf procedure main() printf("BNA to LAX is %d km (%d miles)\n", d := gcdistance([36.12, -86.67],[33.94, -118.40]),d*3280/5280) end procedure gcdistance(a,b) a[2] -:= b[2] every (x := a|b)[i := 1 to 2] := dtor(x[i]) dz := sin(a[1]) - sin(b[1]) dx := cos(a[2]) * cos(a[1]) - cos(b[1]) dy := sin(a[2]) * cos(a[1]) return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * 6371 end
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Convert this J block to C, preserving its control flow and logic.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Translate the given J code snippet into C without altering its behavior.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Keep all operations the same but rewrite the snippet in C#.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Change the programming language of this snippet from J to C# without modifying what it does.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Change the programming language of this snippet from J to C++ without modifying what it does.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Produce a language-to-language conversion: from J to Java, same semantics.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Convert this J block to Java, preserving its control flow and logic.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Produce a functionally identical Python code for the snippet given in J.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Can you help me rewrite this code in Python instead of J, keeping it the same logically?
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Generate an equivalent VB version of this J code.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert the following code from J to VB, ensuring the logic remains intact.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Write a version of this J function in Go with identical behavior.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Convert the following code from J to Go, ensuring the logic remains intact.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Maintain the same structure and functionality when rewriting this code in C.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Maintain the same structure and functionality when rewriting this code in C.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Convert the following code from Julia to C#, ensuring the logic remains intact.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Write the same algorithm in C# as shown in this Julia implementation.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Can you help me rewrite this code in C++ instead of Julia, keeping it the same logically?
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Change the programming language of this snippet from Julia to C++ without modifying what it does.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Change the programming language of this snippet from Julia to Java without modifying what it does.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Convert this Julia block to Java, preserving its control flow and logic.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Rewrite this program in Python while keeping its functionality equivalent to the Julia version.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Rewrite this program in Python while keeping its functionality equivalent to the Julia version.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Convert this Julia block to VB, preserving its control flow and logic.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert the following code from Julia to VB, ensuring the logic remains intact.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Port the following code from Julia to Go with equivalent syntax and logic.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Transform the following Julia implementation into Go, maintaining the same output and logic.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Change the programming language of this snippet from Lua to C without modifying what it does.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Maintain the same structure and functionality when rewriting this code in C.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Keep all operations the same but rewrite the snippet in C#.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Keep all operations the same but rewrite the snippet in C#.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert the following code from Lua to C++, ensuring the logic remains intact.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Please provide an equivalent version of this Lua code in C++.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Convert this Lua block to Java, preserving its control flow and logic.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Change the following Lua code into Java without altering its purpose.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Please provide an equivalent version of this Lua code in Python.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Rewrite the snippet below in Python so it works the same as the original Lua code.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Rewrite this program in VB while keeping its functionality equivalent to the Lua version.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert this Lua block to VB, preserving its control flow and logic.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Write the same algorithm in Go as shown in this Lua implementation.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Port the following code from Lua to Go with equivalent syntax and logic.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Convert this Mathematica block to C, preserving its control flow and logic.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Preserve the algorithm and functionality while converting the code from Mathematica to C.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Translate the given Mathematica code snippet into C# without altering its behavior.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Translate the given Mathematica code snippet into C# without altering its behavior.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Translate the given Mathematica code snippet into C++ without altering its behavior.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Preserve the algorithm and functionality while converting the code from Mathematica to C++.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Change the following Mathematica code into Java without altering its purpose.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Preserve the algorithm and functionality while converting the code from Mathematica to Java.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Please provide an equivalent version of this Mathematica code in Python.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Keep all operations the same but rewrite the snippet in Python.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Translate this program into VB but keep the logic exactly as in Mathematica.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Preserve the algorithm and functionality while converting the code from Mathematica to VB.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert this Mathematica snippet to Go and keep its semantics consistent.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Translate this program into Go but keep the logic exactly as in Mathematica.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Generate an equivalent C version of this MATLAB code.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Rewrite the snippet below in C so it works the same as the original MATLAB code.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Convert this MATLAB snippet to C# and keep its semantics consistent.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Rewrite the snippet below in C# so it works the same as the original MATLAB code.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Ensure the translated C++ code behaves exactly like the original MATLAB snippet.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Generate a C++ translation of this MATLAB snippet without changing its computational steps.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Ensure the translated Java code behaves exactly like the original MATLAB snippet.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Write the same code in Java as shown below in MATLAB.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Port the following code from MATLAB to Python with equivalent syntax and logic.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Maintain the same structure and functionality when rewriting this code in Python.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Rewrite this program in VB while keeping its functionality equivalent to the MATLAB version.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Port the provided MATLAB code into VB while preserving the original functionality.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert the following code from MATLAB to Go, ensuring the logic remains intact.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Translate this program into Go but keep the logic exactly as in MATLAB.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Preserve the algorithm and functionality while converting the code from Nim to C.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Convert this Nim block to C, preserving its control flow and logic.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Please provide an equivalent version of this Nim code in C#.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Please provide an equivalent version of this Nim code in C++.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Keep all operations the same but rewrite the snippet in C++.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Can you help me rewrite this code in Java instead of Nim, keeping it the same logically?
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Write a version of this Nim function in Java with identical behavior.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Generate a Python translation of this Nim snippet without changing its computational steps.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Change the following Nim code into Python without altering its purpose.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Convert the following code from Nim to VB, ensuring the logic remains intact.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Port the following code from Nim to VB with equivalent syntax and logic.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Ensure the translated Go code behaves exactly like the original Nim snippet.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Rewrite this program in Go while keeping its functionality equivalent to the Nim version.
import math proc radians(x): float = x * Pi / 180 proc haversine(lat1, lon1, lat2, lon2): float = const r = 6372.8 let dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat/2)*sin(dLat/2) + cos(lat1)*cos(lat2)*sin(dLon/2)*sin(dLon/2) c = 2*arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Write the same algorithm in C as shown in this OCaml implementation.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Port the provided OCaml code into C while preserving the original functionality.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Produce a functionally identical C# code for the snippet given in OCaml.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert this OCaml snippet to C# and keep its semantics consistent.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Produce a functionally identical C++ code for the snippet given in OCaml.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Maintain the same structure and functionality when rewriting this code in C++.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Preserve the algorithm and functionality while converting the code from OCaml to Java.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Please provide an equivalent version of this OCaml code in Java.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Change the following OCaml code into Python without altering its purpose.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Generate a Python translation of this OCaml snippet without changing its computational steps.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Change the programming language of this snippet from OCaml to VB without modifying what it does.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert the following code from OCaml to VB, ensuring the logic remains intact.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Rewrite this program in Go while keeping its functionality equivalent to the OCaml version.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Transform the following OCaml implementation into Go, maintaining the same output and logic.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }