我试图用Java中的Haversine公式计算大圆周的公里数,如下所示
/* Program to demonstrate the Floating-point numbers and the Math library.
* The great-circle distance is the length of the shortest path between two points (x1,y1) and (x2,y2) on the surface of a sphere, where the path is constrained to be along the surface.*/
public class GreatCircle
{
public static void main(String[] args)
{
double r = 6371.0; // Equatorial radius of the Earth
double x1 = Math.toRadians(Double.parseDouble(args[0]));
double y1 = Math.toRadians(Double.parseDouble(args[1]));
double x2 = Math.toRadians(Double.parseDouble(args[2]));
double y2 = Math.toRadians(Double.parseDouble(args[3]));
// Compute using Haversine formula
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));
// Output the distance
System.out.println(distance + " kilometers ");
}
}我正在运行输入java GreatCircle 60.0 15.0 120.0 105.0。预期的输出是4604.53989281927 kilometers,但我得到了13406.238676180266 kilometers。有人能指出我哪里出了问题吗?
发布于 2021-12-24 09:57:04
该公式的实施不正确。在做了以下修正之后,它就起了作用。在公式中,我们取整个表达式的弧罪恶。
// Compute using Haversine formula
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2 + Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2)));
// Output the distance
System.out.println(distance + " kilometers ");
}
}发布于 2021-12-27 13:53:05
您忘记计算Math.cos(x1) * Math.cos(x2)**,了,这就是为什么您得到了不同的结果。
// Compute using Haversine formula<br>
double distance = 2 * r * Math.asin(Math.sqrt((Math.pow(Math.sin((x2 - x1) / 2),2) + Math.cos(x1) * Math.cos(x2) * Math.pow(Math.sin((y2 - y1) / 2),2))));```发布于 2022-01-30 01:11:20
double distance = 2 * r * Math.asin(Math.sqrt(Math.pow(Math.sin((x2 - x1) / 2),2)
+ Math.cos(x2) * Math.cos(x1) * Math.pow(Math.sin((y2 - y1) / 2),2)));https://stackoverflow.com/questions/70447203
复制相似问题