# Why do I get a domain error for the following computation using law of cosines

Following are two points. lat1, lon1 represent one point and lat2, lon2 represent another point.

lat1, lon1 = 37.317233, -121.94331
lat2, lon2 = 37.317233, -121.94331


When I use the following formula (law of cosines) I get a math domain error

sine_part = sin(lat1_in_radians) * sin(lat2_in_radians)


where degrees to radians is converted by decimal_degree / 57.2957795

The weird thing is, the following points work as expected though. I get 0 as my distance.

lat1 = 54.11102, lon1 = -111.12345
lat2 = 54.11102, lon2 = -111.12345


I do understand that the values resulted by applying one of these trignometric functions is not in the interval it could work with. However I would like to know why this error happens in more detail from one of the experts here please.

I'm hoping I haven't done a school boy error, however I have forgotten my school maths so I won't be surprised if I have missed a trivial point!

• Do you really want your "two" points to be the same point? Commented Apr 11, 2014 at 12:07
• Not really, but I'm curious to know why this happens. Commented Apr 11, 2014 at 12:43
• With rounding, the value inside acos() might be fractionally over 1 in your example with the error. I guess it should be exactly 1?
– Paul
Commented Apr 11, 2014 at 13:15

sine_part = sin(lat1_in_radians) * sin(lat2_in_radians)

As everyone is telling you in the comments, because of floating point errors, temp, which should be between $-1$ and $1$, might be fractionally outside of this range.