I am trying to calculate the longitude of a point from another longitude. They have the same latitude, and the distance between them is known. I am trying to use the sperical law of cosines formula .
# 'lat' short for 'latitude', 'lng' short for 'longitude'.
# EARTH_RADIUS = 6371000.0, unit is meter.
#
distance = Math.acos( Math.sin(lat1)*Math.sin(lat2) +
Math.cos(lat1)*Math.cos(lat2) *
Math.cos(lng2-lng1)) * EARTH_RADIUS
If the two point latitudes are equal (lat1 == lat2), I can calculate lng2 from lng1 with distance. Therefore, I am reasoning the formula from the formula sperical law of cosines
lng2 = Math.acos((Math.cos(distance/EARTH_RADIUS) - Math.sin(lat)*Math.sin(lat))/(Math.cos(lat)*Math.cos(lat))) + lng
This formula works very well, except in some situations.
how
lat_degrees = -89.8345981836319
lng_degrees = 96.42309331893921
lat = lat1 = lat2 = (lat_degrees * Math::PI)/180
lng = (lng_degrees * Math::PI)/180
distance = 67544.06725769254
This will result in an error.
Math::DomainError: Numerical argument is out of domain - "acos"
Because the value in Math.acos (value) is -2.5100189069914602, which is less than -1. I have no idea about this. Is a derived formal error incorrect?
source
share