It depends entirely on what your unit tests are testing.
Most likely you'll be able to treat them as equivalent unless the testing you're doing is actually of the IEEE754 floating point software itself, or the C runtime code that prints them. Otherwise, you should treat them as identical if the code that uses what you're testing treats them as identical.
That's because the tests should echo your real usage, in every circumstance. An (admittedly contrived) example is if you're testing the function doCalc()
which returns a double. If it's only ever used thus:
x = doCalc()
if x is any sort of Nan:
doSomethingWithNan()
then your test should treat all NaN
values as equivalent. However, if you use it thus:
x = doCalc()
if x is +Nan:
doSomethingForPositive()
else:
if x is -Nan:
doSomethingForNegative()
then you'll want to treat them as distinct.
Similarly, if your implementation creates a useful payload in the fractional bits (see below), and your real code uses that, it should be checked by the unit tests as well.
Since a NaN is simply all 1-bits in the exponent and something other than all zero bits in the fraction, the sign bit may be positive or negative, and the fractional bits may be a wide variety of values. However, it's still a value or result that was outside the representation of the data type so, if you were expecting just that, it probably makes little difference what the sign or payload contain.
In terms of checking the textual output of NaN
values, the Wikipedia page on NaN indicates that different implementations may give you widely varying outputs, among them:
nan
NaN
NaN%
NAN
NaNQ
NaNS
qNaN
sNaN
1.#SNAN
1.#QNAN
-1.#IND
and even variants showing the varying sign and payload that have no affect on its NaN-ness:
-NaN
NaN12345
-sNaN12300
-NaN(s1234)
So, if you want to be massively portable in your unit test, you'll notice that all the output representations bar one have some variant of the string nan
in them. So a case-insensitive search through the value for the string nan
or ind
would pick them all up. That may not work in all environments but it has a very large coverage.
For what it's worth, the C standard has this to say about outputting floating point values with %f
(%F
uses uppercase letters):
A double
argument representing a NaN
is converted in one of the styles [-]nan
or [-]nan(n-char-sequence)
- which style, and the meaning of any n-char-sequence
, is implementation-defined.
So it would suffice there to simply check if the value had nan
somewhere inside it.