Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Rewrite this program in Python while keeping its functionality equivalent to the Erlang version. |
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8,
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.
| 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 Erlang snippet without changing its computational steps. |
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8,
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.
| 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
>>>
|
Port the provided Erlang code into VB while preserving the original functionality. |
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8,
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.
| 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 VB code behaves exactly like the original Erlang snippet. |
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8,
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.
| 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 Erlang snippet. |
-module(haversine).
-export([main/0]).
main() ->
haversine(36.12, -86.67, 33.94, -118.40).
haversine(Lat1, Long1, Lat2, Long2) ->
V = math:pi()/180,
R = 6372.8,
Diff_Lat = (Lat2 - Lat1)*V ,
Diff_Long = (Long2 - Long1)*V,
NLat = Lat1*V,
NLong = Lat2*V,
A = math:sin(Diff_Lat/2) * math:sin(Diff_Lat/2) + math:sin(Diff_Long/2) * math:sin(Diff_Long/2) * math:cos(NLat) * math:cos(NLong),
C = 2 * math:asin(math:sqrt(A)),
R*C.
| 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 F# to C without modifying what it does. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| #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. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| #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 F# code. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 this program into C# but keep the logic exactly as in F#. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 F# block to C++, preserving its control flow and logic. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| #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;
}
|
Port the provided F# code into C++ while preserving the original functionality. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| #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;
}
|
Transform the following F# implementation into Java, maintaining the same output and logic. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 F# code in Java. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 an equivalent Python version of this F# code. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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
>>>
|
Ensure the translated Python code behaves exactly like the original F# snippet. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 VB translation of this F# snippet without changing its computational steps. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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
|
Produce a functionally identical VB code for the snippet given in F#. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 F# to Go, ensuring the logic remains intact. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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 F# block to Go, preserving its control flow and logic. | open System
[<Measure>] type deg
[<Measure>] type rad
[<Measure>] type km
let haversine (θ: float<rad>) = 0.5 * (1.0 - Math.Cos(θ/1.0<rad>))
let radPerDeg = (Math.PI / 180.0) * 1.0<rad/deg>
type pos(latitude: float<deg>, longitude: float<deg>) =
member this.φ = latitude * radPerDeg
member this.ψ = longitude * radPerDeg
let rEarth = 6372.8<km>
let hsDist (p1: pos) (p2: pos) =
2.0 * rEarth *
Math.Asin(Math.Sqrt(haversine(p2.φ - p1.φ)+
Math.Cos(p1.φ/1.0<rad>)*Math.Cos(p2.φ/1.0<rad>)*haversine(p2.ψ - p1.ψ)))
[<EntryPoint>]
let main argv =
printfn "%A" (hsDist (pos(36.12<deg>, -86.67<deg>)) (pos(33.94<deg>, -118.40<deg>)))
0
| 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)))
}
|
Can you help me rewrite this code in C instead of Factor, keeping it the same logically? | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| #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;
}
|
Can you help me rewrite this code in C instead of Factor, keeping it the same logically? | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| #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;
}
|
Ensure the translated C# code behaves exactly like the original Factor snippet. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 Factor to C#, ensuring the logic remains intact. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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)));
}
|
Preserve the algorithm and functionality while converting the code from Factor to C++. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| #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;
}
|
Rewrite the snippet below in C++ so it works the same as the original Factor code. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| #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;
}
|
Translate the given Factor code snippet into Java without altering its behavior. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 the following code from Factor to Java, ensuring the logic remains intact. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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));
}
}
|
Translate the given Factor code snippet into Python without altering its behavior. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 Factor code. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 Factor version. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 Factor snippet to VB and keep its semantics consistent. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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
|
Keep all operations the same but rewrite the snippet in Go. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 a version of this Factor function in Go with identical behavior. | USING: arrays kernel math math.constants math.functions math.vectors sequences ;
: haversin ( x -- y ) cos 1 swap - 2 / ;
: haversininv ( y -- x ) 2 * 1 swap - acos ;
: haversineDist ( as bs -- d )
[ [ 180 / pi * ] map ] bi@
[ [ swap - haversin ] 2map ]
[ [ first cos ] bi@ * 1 swap 2array ]
2bi
v.
haversininv R_earth * ;
| 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 provided Forth code into C while preserving the original functionality. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| #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;
}
|
Ensure the translated C code behaves exactly like the original Forth snippet. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| #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;
}
|
Ensure the translated C# code behaves exactly like the original Forth snippet. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 Forth, keeping it the same logically? | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 Forth code in C++. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| #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 an equivalent C++ version of this Forth code. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| #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 Forth code in Java. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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));
}
}
|
Keep all operations the same but rewrite the snippet in Java. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 Forth snippet to Python and keep its semantics consistent. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 VB translation of this Forth snippet without changing its computational steps. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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
|
Translate this program into VB but keep the logic exactly as in Forth. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 the snippet below in Go so it works the same as the original Forth code. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 Go version of this Forth code. | : s>f s>d d>f ;
: deg>rad 174532925199433e-16 f* ;
: difference f- deg>rad 2 s>f f/ fsin fdup f* ;
: haversine
frot difference
frot frot fover fover
fswap difference
fswap deg>rad fcos
frot deg>rad fcos f*
frot f* f+
fsqrt fasin 127456 s>f f* 10 s>f f/
;
36.12e -86.67e 33.94e -118.40e haversine cr f.
| 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 C# but keep the logic exactly as in Fortran. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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 a version of this Fortran function in C# with identical behavior. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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 this program in C++ while keeping its functionality equivalent to the Fortran version. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| #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++. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| #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;
}
|
Translate the given Fortran code snippet into C without altering its behavior. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| #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;
}
|
Can you help me rewrite this code in C instead of Fortran, keeping it the same logically? | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| #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;
}
|
Write a version of this Fortran function in Java with identical behavior. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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));
}
}
|
Translate this program into Java but keep the logic exactly as in Fortran. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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));
}
}
|
Ensure the translated Python code behaves exactly like the original Fortran snippet. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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
>>>
|
Transform the following Fortran implementation into Python, maintaining the same output and logic. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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
>>>
|
Write the same code in VB as shown below in Fortran. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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
|
Transform the following Fortran implementation into VB, maintaining the same output and logic. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| 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
|
Produce a functionally identical PHP code for the snippet given in Fortran. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| class POI {
private $latitude;
private $longitude;
public function __construct($latitude, $longitude) {
$this->latitude = deg2rad($latitude);
$this->longitude = deg2rad($longitude);
}
public function getLatitude() {
return $this->latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function getDistanceInMetersTo(POI $other) {
$radiusOfEarth = 6371; // Earth's radius in kilometers.
$diffLatitude = $other->getLatitude() - $this->latitude;
$diffLongitude = $other->getLongitude() - $this->longitude;
$a = sin($diffLatitude / 2) ** 2 +
cos($this->latitude) *
cos($other->getLatitude()) *
sin($diffLongitude / 2) ** 2;
$c = 2 * asin(sqrt($a));
$distance = $radiusOfEarth * $c;
return $distance;
}
}
|
Convert the following code from Fortran to PHP, ensuring the logic remains intact. | program example
implicit none
real :: d
d = haversine(36.12,-86.67,33.94,-118.40)
print '(A,F9.4,A)', 'distance: ',d,' km'
contains
function to_radian(degree) result(rad)
real,intent(in) :: degree
real, parameter :: deg_to_rad = atan(1.0)/45
real :: rad
rad = degree*deg_to_rad
end function to_radian
function haversine(deglat1,deglon1,deglat2,deglon2) result (dist)
real,intent(in) :: deglat1,deglon1,deglat2,deglon2
real :: a,c,dist,dlat,dlon,lat1,lat2
real,parameter :: radius = 6372.8
dlat = to_radian(deglat2-deglat1)
dlon = to_radian(deglon2-deglon1)
lat1 = to_radian(deglat1)
lat2 = to_radian(deglat2)
a = (sin(dlat/2))**2 + cos(lat1)*cos(lat2)*(sin(dlon/2))**2
c = 2*asin(sqrt(a))
dist = radius*c
end function haversine
end program example
| class POI {
private $latitude;
private $longitude;
public function __construct($latitude, $longitude) {
$this->latitude = deg2rad($latitude);
$this->longitude = deg2rad($longitude);
}
public function getLatitude() {
return $this->latitude;
}
public function getLongitude() {
return $this->longitude;
}
public function getDistanceInMetersTo(POI $other) {
$radiusOfEarth = 6371; // Earth's radius in kilometers.
$diffLatitude = $other->getLatitude() - $this->latitude;
$diffLongitude = $other->getLongitude() - $this->longitude;
$a = sin($diffLatitude / 2) ** 2 +
cos($this->latitude) *
cos($other->getLatitude()) *
sin($diffLongitude / 2) ** 2;
$c = 2 * asin(sqrt($a));
$distance = $radiusOfEarth * $c;
return $distance;
}
}
|
Change the following Groovy code into C without altering its purpose. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| #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 Groovy to C. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| #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 this program in C# while keeping its functionality equivalent to the Groovy version. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 Groovy implementation. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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)));
}
|
Generate a C++ translation of this Groovy snippet without changing its computational steps. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| #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;
}
|
Transform the following Groovy implementation into C++, maintaining the same output and logic. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| #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 Groovy to Java. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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));
}
}
|
Transform the following Groovy implementation into Java, maintaining the same output and logic. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 Groovy block to Python, preserving its control flow and logic. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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
>>>
|
Write a version of this Groovy function in Python with identical behavior. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 VB translation of this Groovy snippet without changing its computational steps. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 VB while keeping its functionality equivalent to the Groovy version. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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
|
Keep all operations the same but rewrite the snippet in Go. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 Groovy implementation into Go, maintaining the same output and logic. | def haversine(lat1, lon1, lat2, lon2) {
def R = 6372.8
def dLat = Math.toRadians(lat2 - lat1)
def dLon = Math.toRadians(lon2 - lon1)
lat1 = Math.toRadians(lat1)
lat2 = Math.toRadians(lat2)
def a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2)
def c = 2 * Math.asin(Math.sqrt(a))
R * c
}
haversine(36.12, -86.67, 33.94, -118.40)
> 2887.25995060711
| 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 the snippet below in C so it works the same as the original Haskell code. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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;
}
|
Translate this program into C but keep the logic exactly as in Haskell. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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;
}
|
Preserve the algorithm and functionality while converting the code from Haskell to C#. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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 this program in C# while keeping its functionality equivalent to the Haskell version. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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 Haskell snippet. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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;
}
|
Change the following Haskell code into C++ without altering its purpose. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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;
}
|
Produce a language-to-language conversion: from Haskell to Java, same semantics. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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));
}
}
|
Convert the following code from Haskell to Java, ensuring the logic remains intact. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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));
}
}
|
Produce a language-to-language conversion: from Haskell to Python, same semantics. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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 the snippet below in Python so it works the same as the original Haskell code. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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
>>>
|
Keep all operations the same but rewrite the snippet in VB. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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
|
Produce a language-to-language conversion: from Haskell to VB, same semantics. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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
|
Transform the following Haskell implementation into Go, maintaining the same output and logic. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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 the given Haskell code snippet into Go without altering its behavior. | import Control.Monad (join)
import Data.Bifunctor (bimap)
import Text.Printf (printf)
haversine :: Float -> Float
haversine = (^ 2) . sin . (/ 2)
greatCircleDistance ::
(Float, Float) ->
(Float, Float) ->
Float
greatCircleDistance = distDeg 6371
where
distDeg radius p1 p2 =
distRad
radius
(deg2rad p1)
(deg2rad p2)
distRad radius (lat1, lng1) (lat2, lng2) =
(2 * radius)
* asin
( min
1.0
( sqrt $
haversine (lat2 - lat1)
+ ( (cos lat1 * cos lat2)
* haversine (lng2 - lng1)
)
)
)
deg2rad = join bimap ((/ 180) . (pi *))
main :: IO ()
main =
printf
"The distance between BNA and LAX is about %0.f km.\n"
(greatCircleDistance bna lax)
where
bna = (36.12, -86.67)
lax = (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)))
}
|
Produce a language-to-language conversion: from Icon to C, same semantics. | 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
| #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 Icon code snippet into C without altering its behavior. | 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
| #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;
}
|
Please provide an equivalent version of this Icon code in C#. | 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
| 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)));
}
|
Port the provided Icon code into C# while preserving the original functionality. | 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
| 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 Icon block to C++, 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
| #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++. | 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
| #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;
}
|
Rewrite the snippet below in Java so it works the same as the original Icon code. | 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
| 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 provided Icon code into Java while preserving the original functionality. | 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
| 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 Icon to Python. | 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
| 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
>>>
|
Write the same algorithm in Python as shown in this Icon implementation. | 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
| 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 Icon version. | 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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.