I’m posting because of a very frustrating issue I stumbled upon while developing my photo planner: php’s acos function has an undocumented (yet found by others) behavior. It can return NaN, in a way that breaks a JSON. The PHP manual says (2019-04-02) Return Values: The arc cosine of arg in radians — and nothing about a NaN scenario. Nothing. On forums, it can be found that the function returns NaN when the argument is out of the range [-1, 1]. And as it turns out, due to rounding errors in the float, this can happen. Very, very, very annoying.
I have the following lines of code, obviously for astronomy, calculating the arc distance, in degrees, from radec pairs, in degrees.
function arcdistdeg($ra1, $dec1, $ra2, $dec2){ $cosA = sin(deg2rad($dec1)) * sin(deg2rad($dec2)) + cos(deg2rad($dec1)) * cos(deg2rad($dec2)) * cos(deg2rad($ra1 - $ra2)); return rad2deg(acos($cosA)); }
And this function returned NaN for perfectly valid input values (around NGC 4103). So the obvious solution was to force the range with a safe_acos function, similar to
function safe_acos($n){ return acos(min(max($n,-1.0),1.0)); };
Very, very annoying.






