acos(1) returns NaN for some values, not others

前端 未结 2 797
礼貌的吻别
礼貌的吻别 2020-12-20 16:39

I have a list of latitude and longitude values, and I\'m trying to find the distance between them. Using a standard great circle method, I need to find:

aco         


        
2条回答
  •  清酒与你
    2020-12-20 17:32

    A simple workaround is to use pmin(), like this:

    acos(pmin(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) * cos(long2-long1),1))
    

    It now ensures that the precision loss leads to a value no higher than exactly 1.

    This doesn't explain what is happening, however.

    (Edit: Matthew Lundberg pointed out I need to use pmin to get it tow work with vectorized inputs. This fixes the problem with getting it to work, but I'm still not sure why it is rounding incorrectly.)

提交回复
热议问题