bin/109692: printing -NaN
dynamic-wind at mail.ru
Wed Feb 28 18:10:04 UTC 2007
>Synopsis: printing -NaN
>Arrival-Date: Wed Feb 28 18:10:03 GMT 2007
>Originator: Denis Koreshkov
>Release: freebsd 6.2-stable
The vfprintf() function correctly formats +Inf and -Inf but it has no provision for distinguishing between +NaN and -NaN. Hence all functions of printf(3) family output all -NaNs as NaNs.
printf("%g %g\n", +0.0/0.0, -0.0/0.0);
outputs: NaN NaN
which should be: NaN -NaN
vfprintf(3) tests for a 'double' argument being an Inf or a NaN using isinf() and isinf().
Then, an Inf's signum is detected by a trivial arithmetic comparison to 0.0
But arithmetic comparisons fail when an argument is a NaN, so there is no corresponding code to set a negative NaN's signum to '-'.
Consider using copysign(1.0, _double) which returns +1.0 and -1.0 for +NaN and -NaN. This is the case for the msun libm used in FreeBSD, though other implementations of copysign(3) may not derive a NaN's signum correctly, or raise an exception with signaling NaNs.
More information about the freebsd-bugs