在 Java 中使用 Haversine 公式计算大圆的公里距离时输出不正确
Incorrect output when computing the distance in kilometres of the Great Circle using Haversine formula in Java
我正在尝试使用 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
。有人可以指出我哪里错了吗?
公式执行不正确。它在进行以下更正后起作用。
在公式中,我们取整个表达式的反正弦值。
// 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 ");
}
}
你忘记计算了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))));```
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)));
我正在尝试使用 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
。有人可以指出我哪里错了吗?
公式执行不正确。它在进行以下更正后起作用。 在公式中,我们取整个表达式的反正弦值。
// 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 ");
}
}
你忘记计算了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))));```
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)));