Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Change the programming language of this snippet from Scala to C# without modifying what it does.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert this Scala block to C++, preserving its control flow and logic.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Rewrite the snippet below in C++ so it works the same as the original Scala code.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Keep all operations the same but rewrite the snippet in Java.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Keep all operations the same but rewrite the snippet in Java.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Port the following code from Scala to Python with equivalent syntax and logic.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Convert the following code from Scala to Python, ensuring the logic remains intact.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Port the following code from Scala to VB with equivalent syntax and logic.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Produce a language-to-language conversion: from Scala to VB, same semantics.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Port the following code from Scala to Go with equivalent syntax and logic.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Write a version of this Scala function in Go with identical behavior.
import java.lang.Math.* const val R = 6372.8 fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double { val λ1 = toRadians(lat1) val λ2 = toRadians(lat2) val Δλ = toRadians(lat2 - lat1) val Δφ = toRadians(lon2 - lon1) return 2 * R * asin(sqrt(pow(sin(Δλ / 2), 2.0) + pow(sin(Δφ / 2), 2.0) * cos(λ1) * cos(λ2))) } fun main(args: Array<String>) = println("result: " + haversine(36.12, -86.67, 33.94, -118.40))
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Rewrite this program in C while keeping its functionality equivalent to the Swift version.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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; }
Port the provided Swift code into C while preserving the original functionality.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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; }
Generate a C# translation of this Swift snippet without changing its computational steps.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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))); }
Translate the given Swift code snippet into C# without altering its behavior.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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))); }
Maintain the same structure and functionality when rewriting this code in C++.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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 Swift code into C++ without altering its purpose.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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; }
Rewrite this program in Java while keeping its functionality equivalent to the Swift version.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-118.40))
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Port the following code from Swift to Java with equivalent syntax and logic.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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 functionally identical Python code for the snippet given in Swift.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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 >>>
Port the provided Swift code into Python while preserving the original functionality.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-118.40))
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Convert this Swift block to VB, preserving its control flow and logic.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-118.40))
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Port the provided Swift code into VB while preserving the original functionality.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-118.40))
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert the following code from Swift to Go, ensuring the logic remains intact.
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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))) }
Can you help me rewrite this code in Go instead of Swift, keeping it the same logically?
import Foundation func haversine(lat1:Double, lon1:Double, lat2:Double, lon2:Double) -> Double { let lat1rad = lat1 * Double.pi/180 let lon1rad = lon1 * Double.pi/180 let lat2rad = lat2 * Double.pi/180 let lon2rad = lon2 * Double.pi/180 let dLat = lat2rad - lat1rad let dLon = lon2rad - lon1rad let a = sin(dLat/2) * sin(dLat/2) + sin(dLon/2) * sin(dLon/2) * cos(lat1rad) * cos(lat2rad) let c = 2 * asin(sqrt(a)) let R = 6372.8 return R * c } print(haversine(lat1:36.12, lon1:-86.67, lat2:33.94, lon2:-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 Tcl to C, same semantics.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Generate a C translation of this Tcl snippet without changing its computational steps.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Please provide an equivalent version of this Tcl code in C#.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Preserve the algorithm and functionality while converting the code from Tcl to C#.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Please provide an equivalent version of this Tcl code in C++.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Transform the following Tcl implementation into C++, maintaining the same output and logic.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Write the same code in Java as shown below in Tcl.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Translate this program into Java but keep the logic exactly as in Tcl.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Change the following Tcl code into Python without altering its purpose.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Write the same algorithm in Python as shown in this Tcl implementation.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Rewrite this program in VB while keeping its functionality equivalent to the Tcl version.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Keep all operations the same but rewrite the snippet in VB.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Rewrite this program in Go while keeping its functionality equivalent to the Tcl version.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Generate an equivalent Go version of this Tcl code.
package require Tcl 8.5 proc haversineFormula {lat1 lon1 lat2 lon2} { set rads [expr atan2(0,-1)/180] set R 6372.8 ; set dLat [expr {($lat2-$lat1) * $rads}] set dLon [expr {($lon2-$lon1) * $rads}] set lat1 [expr {$lat1 * $rads}] set lat2 [expr {$lat2 * $rads}] set a [expr {sin($dLat/2)**2 + sin($dLon/2)**2*cos($lat1)*cos($lat2)}] set c [expr {2*asin(sqrt($a))}] return [expr {$R * $c}] } puts [format "distance=%.1f km" [haversineFormula 36.12 -86.67 33.94 -118.40]]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Change the programming language of this snippet from Rust to PHP without modifying what it does.
struct Point { lat: f64, lon: f64, } fn haversine(origin: Point, destination: Point) -> f64 { const R: f64 = 6372.8; let lat1 = origin.lat.to_radians(); let lat2 = destination.lat.to_radians(); let d_lat = lat2 - lat1; let d_lon = (destination.lon - origin.lon).to_radians(); let a = (d_lat / 2.0).sin().powi(2) + (d_lon / 2.0).sin().powi(2) * lat1.cos() * lat2.cos(); let c = 2.0 * a.sqrt().asin(); R * c } #[cfg(test)] mod test { use super::{Point, haversine}; #[test] fn test_haversine() { let origin: Point = Point { lat: 36.12, lon: -86.67 }; let destination: Point = Point { lat: 33.94, lon: -118.4 }; let d: f64 = haversine(origin, destination); println!("Distance: {} km ({} mi)", d, d / 1.609344); assert_eq!(d, 2887.2599506071106); } }
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; } }
Produce a functionally identical PHP code for the snippet given in Rust.
struct Point { lat: f64, lon: f64, } fn haversine(origin: Point, destination: Point) -> f64 { const R: f64 = 6372.8; let lat1 = origin.lat.to_radians(); let lat2 = destination.lat.to_radians(); let d_lat = lat2 - lat1; let d_lon = (destination.lon - origin.lon).to_radians(); let a = (d_lat / 2.0).sin().powi(2) + (d_lon / 2.0).sin().powi(2) * lat1.cos() * lat2.cos(); let c = 2.0 * a.sqrt().asin(); R * c } #[cfg(test)] mod test { use super::{Point, haversine}; #[test] fn test_haversine() { let origin: Point = Point { lat: 36.12, lon: -86.67 }; let destination: Point = Point { lat: 33.94, lon: -118.4 }; let d: f64 = haversine(origin, destination); println!("Distance: {} km ({} mi)", d, d / 1.609344); assert_eq!(d, 2887.2599506071106); } }
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; } }
Keep all operations the same but rewrite the snippet in PHP.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
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; } }
Maintain the same structure and functionality when rewriting this code in PHP.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
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 Arturo code into PHP without altering its purpose.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
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; } }
Translate the given Arturo code snippet into PHP without altering its behavior.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
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; } }
Maintain the same structure and functionality when rewriting this code in PHP.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
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; } }
Port the following code from AutoHotKey to PHP with equivalent syntax and logic.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
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; } }
Transform the following AWK implementation into PHP, maintaining the same output and logic.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { 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 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
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; } }
Port the provided AWK code into PHP while preserving the original functionality.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { 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 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
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; } }
Write the same algorithm in PHP as shown in this BBC_Basic implementation.
PRINT "Distance = " ; FNhaversine(36.12, -86.67, 33.94, -118.4) " km" END DEF FNhaversine(n1, e1, n2, e2) LOCAL d() : DIM d(2) d() = COSRAD(e1-e2) * COSRAD(n1) - COSRAD(n2), \ \ SINRAD(e1-e2) * COSRAD(n1), \ \ SINRAD(n1) - SINRAD(n2) = ASN(MOD(d()) / 2) * 6372.8 * 2
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; } }
Write the same algorithm in PHP as shown in this BBC_Basic implementation.
PRINT "Distance = " ; FNhaversine(36.12, -86.67, 33.94, -118.4) " km" END DEF FNhaversine(n1, e1, n2, e2) LOCAL d() : DIM d(2) d() = COSRAD(e1-e2) * COSRAD(n1) - COSRAD(n2), \ \ SINRAD(e1-e2) * COSRAD(n1), \ \ SINRAD(n1) - SINRAD(n2) = ASN(MOD(d()) / 2) * 6372.8 * 2
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; } }
Transform the following Clojure implementation into PHP, maintaining the same output and logic.
(defn haversine [{lon1 :longitude lat1 :latitude} {lon2 :longitude lat2 :latitude}] (let [R 6372.8 dlat (Math/toRadians (- lat2 lat1)) dlon (Math/toRadians (- lon2 lon1)) lat1 (Math/toRadians lat1) lat2 (Math/toRadians lat2) a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lat1) (Math/cos lat2)))] (* R 2 (Math/asin (Math/sqrt a))))) (haversine {:latitude 36.12 :longitude -86.67} {:latitude 33.94 :longitude -118.40})
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 this Clojure snippet to PHP and keep its semantics consistent.
(defn haversine [{lon1 :longitude lat1 :latitude} {lon2 :longitude lat2 :latitude}] (let [R 6372.8 dlat (Math/toRadians (- lat2 lat1)) dlon (Math/toRadians (- lon2 lon1)) lat1 (Math/toRadians lat1) lat2 (Math/toRadians lat2) a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2))) (* (Math/sin (/ dlon 2)) (Math/sin (/ dlon 2)) (Math/cos lat1) (Math/cos lat2)))] (* R 2 (Math/asin (Math/sqrt a))))) (haversine {:latitude 36.12 :longitude -86.67} {:latitude 33.94 :longitude -118.40})
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; } }
Translate the given Common_Lisp code snippet into PHP without altering its behavior.
(defparameter *earth-radius* 6372.8) (defparameter *rad-conv* (/ pi 180)) (defun deg->rad (x) (* x *rad-conv*)) (defun haversine (x) (expt (sin (/ x 2)) 2)) (defun dist-rad (lat1 lng1 lat2 lng2) (let* ((hlat (haversine (- lat2 lat1))) (hlng (haversine (- lng2 lng1))) (root (sqrt (+ hlat (* (cos lat1) (cos lat2) hlng))))) (* 2 *earth-radius* (asin root)))) (defun dist-deg (lat1 lng1 lat2 lng2) (dist-rad (deg->rad lat1) (deg->rad lng1) (deg->rad lat2) (deg->rad lng2)))
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 programming language of this snippet from Common_Lisp to PHP without modifying what it does.
(defparameter *earth-radius* 6372.8) (defparameter *rad-conv* (/ pi 180)) (defun deg->rad (x) (* x *rad-conv*)) (defun haversine (x) (expt (sin (/ x 2)) 2)) (defun dist-rad (lat1 lng1 lat2 lng2) (let* ((hlat (haversine (- lat2 lat1))) (hlng (haversine (- lng2 lng1))) (root (sqrt (+ hlat (* (cos lat1) (cos lat2) hlng))))) (* 2 *earth-radius* (asin root)))) (defun dist-deg (lat1 lng1 lat2 lng2) (dist-rad (deg->rad lat1) (deg->rad lng1) (deg->rad lat2) (deg->rad lng2)))
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; } }
Translate the given D code snippet into PHP without altering its behavior.
import std.stdio, std.math; real haversineDistance(in real dth1, in real dph1, in real dth2, in real dph2) pure nothrow @nogc { enum real R = 6371; enum real TO_RAD = PI / 180; alias imr = immutable real; imr ph1d = dph1 - dph2; imr ph1 = ph1d * TO_RAD; imr th1 = dth1 * TO_RAD; imr th2 = dth2 * TO_RAD; imr dz = th1.sin - th2.sin; imr dx = ph1.cos * th1.cos - th2.cos; imr dy = ph1.sin * th1.cos; return asin(sqrt(dx ^^ 2 + dy ^^ 2 + dz ^^ 2) / 2) * 2 * R; } void main() { writefln("Haversine distance: %.1f km", haversineDistance(36.12, -86.67, 33.94, -118.4)); }
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 this D snippet to PHP and keep its semantics consistent.
import std.stdio, std.math; real haversineDistance(in real dth1, in real dph1, in real dth2, in real dph2) pure nothrow @nogc { enum real R = 6371; enum real TO_RAD = PI / 180; alias imr = immutable real; imr ph1d = dph1 - dph2; imr ph1 = ph1d * TO_RAD; imr th1 = dth1 * TO_RAD; imr th2 = dth2 * TO_RAD; imr dz = th1.sin - th2.sin; imr dx = ph1.cos * th1.cos - th2.cos; imr dy = ph1.sin * th1.cos; return asin(sqrt(dx ^^ 2 + dy ^^ 2 + dz ^^ 2) / 2) * 2 * R; } void main() { writefln("Haversine distance: %.1f km", haversineDistance(36.12, -86.67, 33.94, -118.4)); }
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; } }
Port the provided Delphi code into PHP while preserving the original functionality.
program HaversineDemo; uses Math; function HaversineDist(th1, ph1, th2, ph2:double):double; const diameter = 2 * 6372.8; var dx, dy, dz:double; begin ph1 := degtorad(ph1 - ph2); th1 := degtorad(th1); th2 := degtorad(th2); dz := sin(th1) - sin(th2); dx := cos(ph1) * cos(th1) - cos(th2); dy := sin(ph1) * cos(th1); Result := arcsin(sqrt(sqr(dx) + sqr(dy) + sqr(dz)) / 2) * diameter; end; begin Writeln('Haversine distance: ', HaversineDist(36.12, -86.67, 33.94, -118.4):7:2, ' km.'); end.
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; } }
Generate an equivalent PHP version of this Delphi code.
program HaversineDemo; uses Math; function HaversineDist(th1, ph1, th2, ph2:double):double; const diameter = 2 * 6372.8; var dx, dy, dz:double; begin ph1 := degtorad(ph1 - ph2); th1 := degtorad(th1); th2 := degtorad(th2); dz := sin(th1) - sin(th2); dx := cos(ph1) * cos(th1) - cos(th2); dy := sin(ph1) * cos(th1); Result := arcsin(sqrt(sqr(dx) + sqr(dy) + sqr(dz)) / 2) * diameter; end; begin Writeln('Haversine distance: ', HaversineDist(36.12, -86.67, 33.94, -118.4):7:2, ' km.'); end.
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; } }
Write a version of this Elixir function in PHP with identical behavior.
defmodule Haversine do @v :math.pi / 180 @r 6372.8 def distance({lat1, long1}, {lat2, long2}) do dlat = :math.sin((lat2 - lat1) * @v / 2) dlong = :math.sin((long2 - long1) * @v / 2) a = dlat * dlat + dlong * dlong * :math.cos(lat1 * @v) * :math.cos(lat2 * @v) @r * 2 * :math.asin(:math.sqrt(a)) end end bna = {36.12, -86.67} lax = {33.94, -118.40} IO.puts Haversine.distance(bna, lax)
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; } }
Write a version of this Elixir function in PHP with identical behavior.
defmodule Haversine do @v :math.pi / 180 @r 6372.8 def distance({lat1, long1}, {lat2, long2}) do dlat = :math.sin((lat2 - lat1) * @v / 2) dlong = :math.sin((long2 - long1) * @v / 2) a = dlat * dlat + dlong * dlong * :math.cos(lat1 * @v) * :math.cos(lat2 * @v) @r * 2 * :math.asin(:math.sqrt(a)) end end bna = {36.12, -86.67} lax = {33.94, -118.40} IO.puts Haversine.distance(bna, lax)
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; } }
Translate the given Erlang code snippet into PHP without altering its behavior.
-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.
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; } }
Port the provided Erlang code into PHP 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.
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; } }
Please provide an equivalent version of this F# code in PHP.
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
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; } }
Produce a language-to-language conversion: from F# to PHP, same semantics.
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
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; } }
Port the provided Factor code into PHP while preserving the original functionality.
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 * ;
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; } }
Translate the given Factor code snippet into PHP 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 * ;
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 programming language of this snippet from Forth to PHP without modifying what it does.
: 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.
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; } }
Rewrite the snippet below in PHP 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.
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; } }
Keep all operations the same but rewrite the snippet in PHP.
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 programming language of this snippet from Fortran to PHP without modifying what it does.
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; } }
Can you help me rewrite this code in PHP instead of Groovy, keeping it the same logically?
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
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; } }
Write the same code in PHP as shown below in Groovy.
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
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; } }
Write a version of this Haskell function in PHP with identical 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)
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; } }
Keep all operations the same but rewrite the snippet in PHP.
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)
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; } }
Write the same code in PHP as shown below in Icon.
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
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; } }
Transform the following Icon implementation into PHP, maintaining the same output 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
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; } }
Generate an equivalent PHP version of this J code.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
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; } }
Port the provided J code into PHP while preserving the original functionality.
require 'trig' haversin=: 0.5 * 1 - cos Rearth=: 6372.8 haversineDist=: Rearth * haversin^:_1@((1 , *&(cos@{.)) +/ .* [: haversin -)&rfd
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 programming language of this snippet from Julia to PHP without modifying what it does.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
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; } }
Transform the following Julia implementation into PHP, maintaining the same output and logic.
haversine(lat1, lon1, lat2, lon2) = 2 * 6372.8 * asin(sqrt(sind((lat2 - lat1) / 2) ^ 2 + cosd(lat1) * cosd(lat2) * sind((lon2 - lon1) / 2) ^ 2)) @show haversine(36.12, -86.67, 33.94, -118.4)
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; } }
Preserve the algorithm and functionality while converting the code from Lua to PHP.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
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; } }
Rewrite this program in PHP while keeping its functionality equivalent to the Lua version.
local function haversine(x1, y1, x2, y2) r=0.017453292519943295769236907684886127; x1= x1*r; x2= x2*r; y1= y1*r; y2= y2*r; dy = y2-y1; dx = x2-x1; a = math.pow(math.sin(dx/2),2) + math.cos(x1) * math.cos(x2) * math.pow(math.sin(dy/2),2); c = 2 * math.asin(math.sqrt(a)); d = 6372.8 * c; return d; end
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; } }
Port the following code from Mathematica to PHP with equivalent syntax and logic.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
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; } }
Generate a PHP translation of this Mathematica snippet without changing its computational steps.
distance[{theta1_, phi1_}, {theta2_, phi2_}] := 2*6378.14 ArcSin@ Sqrt[Haversine[(theta2 - theta1) Degree] + Cos[theta1*Degree] Cos[theta2*Degree] Haversine[(phi2 - phi1) Degree]]
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 this MATLAB snippet to PHP and keep its semantics consistent.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
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; } }
Transform the following MATLAB implementation into PHP, maintaining the same output and logic.
function rad = radians(degree) rad = degree .* pi / 180; end; function [a,c,dlat,dlon]=haversine(lat1,lon1,lat2,lon2) dlat = radians(lat2-lat1); dlon = radians(lon2-lon1); lat1 = radians(lat1); lat2 = radians(lat2); a = (sin(dlat./2)).^2 + cos(lat1) .* cos(lat2) .* (sin(dlon./2)).^2; c = 2 .* asin(sqrt(a)); arrayfun(@(x) printf("distance:  end; [a,c,dlat,dlon] = haversine(36.12,-86.67,33.94,-118.40);
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; } }
Write the same code in PHP as shown below in Nim.
import std/math proc haversine(lat1, lon1, lat2, lon2: float): float = const r = 6372.8 let dLat = degToRad(lat2 - lat1) dLon = degToRad(lon2 - lon1) lat1 = degToRad(lat1) lat2 = degToRad(lat2) a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2) c = 2 * arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
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; } }
Port the following code from Nim to PHP with equivalent syntax and logic.
import std/math proc haversine(lat1, lon1, lat2, lon2: float): float = const r = 6372.8 let dLat = degToRad(lat2 - lat1) dLon = degToRad(lon2 - lon1) lat1 = degToRad(lat1) lat2 = degToRad(lat2) a = sin(dLat / 2) * sin(dLat / 2) + cos(lat1) * cos(lat2) * sin(dLon / 2) * sin(dLon / 2) c = 2 * arcsin(sqrt(a)) result = r * c echo haversine(36.12, -86.67, 33.94, -118.40)
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; } }
Write the same code in PHP as shown below in OCaml.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
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; } }
Generate an equivalent PHP version of this OCaml code.
let pi = 4. *. atan 1. let radians_of_degrees = ( *. ) (pi /. 180.) let haversin theta = 0.5 *. (1. -. cos theta) type angle = Deg of float | Rad of float let as_radians = function | Deg d -> radians_of_degrees d | Rad r -> r module LatLong = struct type t = { lat: float; lng: float } let of_angles lat lng = { lat = as_radians lat; lng = as_radians lng } let sub a b = { lat = a.lat-.b.lat; lng = a.lng-.b.lng } let dist radius a b = let d = sub b a in let h = haversin d.lat +. haversin d.lng *. cos a.lat *. cos b.lat in 2. *. radius *. asin (sqrt h) end let earth_dist = LatLong.dist 6372.8 and bna = LatLong.of_angles (Deg 36.12) (Deg (-86.67)) and lax = LatLong.of_angles (Deg 33.94) (Deg (-118.4)) in earth_dist bna lax;;
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; } }
Write the same code in PHP as shown below in Pascal.
program HaversineDemo; uses Math; function HaversineDistance(const lat1, lon1, lat2, lon2:double):double;inline; const rads = pi / 180; dia = 2 * 6372.8; begin HaversineDistance := dia * arcsin(sqrt(sqr(cos(rads * (lon1 - lon2)) * cos(rads * lat1) - cos(rads * lat2)) + sqr(sin(rads * (lon1 - lon2)) * cos(rads * lat1)) + sqr(sin(rads * lat1) - sin(rads * lat2))) / 2); end; begin Writeln('Haversine distance between BNA and LAX: ', HaversineDistance(36.12, -86.67, 33.94, -118.4):7:2, ' km.'); end.
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 this Pascal snippet to PHP and keep its semantics consistent.
program HaversineDemo; uses Math; function HaversineDistance(const lat1, lon1, lat2, lon2:double):double;inline; const rads = pi / 180; dia = 2 * 6372.8; begin HaversineDistance := dia * arcsin(sqrt(sqr(cos(rads * (lon1 - lon2)) * cos(rads * lat1) - cos(rads * lat2)) + sqr(sin(rads * (lon1 - lon2)) * cos(rads * lat1)) + sqr(sin(rads * lat1) - sin(rads * lat2))) / 2); end; begin Writeln('Haversine distance between BNA and LAX: ', HaversineDistance(36.12, -86.67, 33.94, -118.4):7:2, ' km.'); end.
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; } }
Write the same algorithm in PHP as shown in this Perl implementation.
use ntheory qw/Pi/; sub asin { my $x = shift; atan2($x, sqrt(1-$x*$x)); } sub surfacedist { my($lat1, $lon1, $lat2, $lon2) = @_; my $radius = 6372.8; my $radians = Pi() / 180;; my $dlat = ($lat2 - $lat1) * $radians; my $dlon = ($lon2 - $lon1) * $radians; $lat1 *= $radians; $lat2 *= $radians; my $a = sin($dlat/2)**2 + cos($lat1) * cos($lat2) * sin($dlon/2)**2; my $c = 2 * asin(sqrt($a)); return $radius * $c; } my @BNA = (36.12, -86.67); my @LAX = (33.94, -118.4); printf "Distance: %.3f km\n", surfacedist(@BNA, @LAX);
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; } }
Rewrite this program in PHP while keeping its functionality equivalent to the Perl version.
use ntheory qw/Pi/; sub asin { my $x = shift; atan2($x, sqrt(1-$x*$x)); } sub surfacedist { my($lat1, $lon1, $lat2, $lon2) = @_; my $radius = 6372.8; my $radians = Pi() / 180;; my $dlat = ($lat2 - $lat1) * $radians; my $dlon = ($lon2 - $lon1) * $radians; $lat1 *= $radians; $lat2 *= $radians; my $a = sin($dlat/2)**2 + cos($lat1) * cos($lat2) * sin($dlon/2)**2; my $c = 2 * asin(sqrt($a)); return $radius * $c; } my @BNA = (36.12, -86.67); my @LAX = (33.94, -118.4); printf "Distance: %.3f km\n", surfacedist(@BNA, @LAX);
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; } }
Generate a PHP translation of this PowerShell snippet without changing its computational steps.
Add-Type -AssemblyName System.Device $BNA = New-Object System.Device.Location.GeoCoordinate 36.12, -86.67 $LAX = New-Object System.Device.Location.GeoCoordinate 33.94, -118.40 $BNA.GetDistanceTo( $LAX ) / 1000
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; } }
Translate the given PowerShell code snippet into PHP without altering its behavior.
Add-Type -AssemblyName System.Device $BNA = New-Object System.Device.Location.GeoCoordinate 36.12, -86.67 $LAX = New-Object System.Device.Location.GeoCoordinate 33.94, -118.40 $BNA.GetDistanceTo( $LAX ) / 1000
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; } }
Produce a functionally identical PHP code for the snippet given in R.
dms_to_rad <- function(d, m, s) (d + m / 60 + s / 3600) * pi / 180 great_circle_distance <- function(lat1, long1, lat2, long2) { a <- sin(0.5 * (lat2 - lat1)) b <- sin(0.5 * (long2 - long1)) 12742 * asin(sqrt(a * a + cos(lat1) * cos(lat2) * b * b)) } great_circle_distance( dms_to_rad(36, 7, 28.10), dms_to_rad( 86, 40, 41.50), dms_to_rad(33, 56, 32.98), dms_to_rad(118, 24, 29.05))
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; } }
Rewrite the snippet below in PHP so it works the same as the original R code.
dms_to_rad <- function(d, m, s) (d + m / 60 + s / 3600) * pi / 180 great_circle_distance <- function(lat1, long1, lat2, long2) { a <- sin(0.5 * (lat2 - lat1)) b <- sin(0.5 * (long2 - long1)) 12742 * asin(sqrt(a * a + cos(lat1) * cos(lat2) * b * b)) } great_circle_distance( dms_to_rad(36, 7, 28.10), dms_to_rad( 86, 40, 41.50), dms_to_rad(33, 56, 32.98), dms_to_rad(118, 24, 29.05))
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; } }
Rewrite the snippet below in PHP so it works the same as the original Racket code.
#lang racket (require math) (define earth-radius 6371) (define (distance lat1 long1 lat2 long2) (define (h a b) (sqr (sin (/ (- b a) 2)))) (* 2 earth-radius (asin (sqrt (+ (h lat1 lat2) (* (cos lat1) (cos lat2) (h long1 long2))))))) (define (deg-to-rad d m s) (* (/ pi 180) (+ d (/ m 60) (/ s 3600)))) (distance (deg-to-rad 36 7.2 0) (deg-to-rad 86 40.2 0) (deg-to-rad 33 56.4 0) (deg-to-rad 118 24.0 0))
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; } }