My program during calculation can generate nan
or -nan
values.
I check if the values are nan
/-nan
using isnan
method.
I also have to distinguish if the nan value is positive or negative (nan
or -nan
). How can I do this?
Added:I need crossplatform solution for WIN and for Unix/Linux
Nearly all systems today use either IEEE single or double precision floating-point. So in that case you could (bitwise) convert it to an integer and read the sign-bit.
Here's one approach that uses unions. Although it's not fully standard-compliant, it should still work on nearly all systems.
Try
signbit
from<math.h>
:It's apparently part of C99 and POSIX.1-2001, but you could write a macro/function yourself if you don't want to use/conform to either of the two.
You could use the
copysign
function (C99, in<math.h>
);From C99 §7.12.11.1: