我们在iOS和Android中使用相同的函数,相同的输入数据,输出与第二个/第三个十进制数不同。
我无法理解原因,我们真的需要完全相同的输出。
遵循 iOS 功能:
func angleBoundSetFromPosition(userPosition: CLLocationCoordinate2D, distance: Double, bearing: Double) -> (CLLocationCoordinate2D) {
let dist = Float(distance) / Float(6371)
let brg = Float(bearing.degreesToRadians)
let lat1 = Float(userPosition.latitude).degreesToRadians
let lon1 = Float(userPosition.longitude).degreesToRadians
let lat2 = asin(sin(lat1) * cos(dist) + cos(lat1) * sin(dist) * cos(brg))
let lon2 = lon1 + atan2(sin(brg) * sin(dist) * cos(lat1), cos(dist) - sin(lat1) * sin(lat2))
print("for: (bearing), dist: (dist), bearing: (brg), lat1: (lat1), lon1: (lon1), lat2: (lat2), lon2: (lon2)")
return CLLocationCoordinate2D(latitude: Double(lat2.radiansToDegrees), longitude: Double(lon2.radiansToDegrees))
}
扩展 iOS:
extension FloatingPoint {
var degreesToRadians: Self { return self / 180 * .pi}
var radiansToDegrees: Self { return self * 180 / .pi }
}
这里是爪哇:
public static LatLng getDestinationPoint(LatLng source, double brng, double dist) {
dist = dist / 6371;
brng = Math.toRadians(brng);
double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
Math.cos(lat1),
Math.cos(dist) - Math.sin(lat1) *
Math.sin(lat2));
if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
return null;
}
return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
}
输入:
UserPosition(45.6091463, 8.8496677)
distance = 10
called once with bearing = -45, and once with bearing 135
输出:
人造人
{latitude 45.621861885764005, longitude 8.83148183051733}
{latitude 45.596425230386295, longitude 8.867843324603944}
苹果
{latitude 45.67270183849526, longitude 8.758660791031469}
{latitude 45.54551866514264, longitude 8.940468486905017}
当您将函数从一种语言转换为另一种语言时,您应该尝试使用相同的变量名称。在这种情况下,对于 Java,您可能还希望静态导入所有 Math 函数,这样您就不会拥有一个实际上最终导致无法进行比较的函数。
这是快速函数:
let lat2 = asin(sin(lat1)) * cos(newDistance) + cos(lat1) * sin(newDistance) * cos(newBearing)
^
这是美化的Java函数:
double lat2 = asin(sin(lat1) * cos(newDistance) + cos(lat1) * sin(newDistance) * cos(newBearing));
^
您的 Swift 函数在 sins 后面有一个括号,而 Java 公式在末尾有一个括号。
因此,分歧来自这样一个事实,即这两个方程都不匹配,而不是任何一种破碎的双数学。
双精度漂移通常只发生在小尺度上。如果您的测试数据显示的值差异很大,则更有可能是实现不正确,而不是解释。
这是盒装测试代码。
public static void main(String[] args) {
System.out.println(getDestinationPoint(new LatLng(45.6091463, 8.8496677), -45, 10));
System.out.println(getDestinationPoint(new LatLng(45.6091463, 8.8496677), 135, 10));
}
public static LatLng getDestinationPoint(LatLng source, double bearing, double distance) {
double newDistance = distance / 6371;
double newBearing = toRadians(bearing);
double lat1 = toRadians(source.latitude);
double lon1 = toRadians(source.longitude);
double lat2 = asin(sin(lat1) * cos(newDistance) + cos(lat1) * sin(newDistance) * cos(newBearing));
double lon2 = lon1 + atan2(sin(newBearing) * sin(newDistance) * cos(lat1), cos(newDistance) - sin(lat1) * sin(lat2));
if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
return null;
}
return new LatLng(toDegrees(lat2), toDegrees(lon2));
}
static class LatLng {
double latitude;
double longitude;
public LatLng(double lat, double lon) {
latitude = lat;
longitude = lon;
}
public String toString() {
return latitude + ", " + longitude;
}
}
此问题应仅在"双精度"值上发生。"漂浮物"应该不受此影响。它与"IEEE 754"浮点运算有关。
如果您有兴趣,这是一个链接:https://www.appinf.com/download/FPIssues.pdf