"Rolf Magnus" <ra******@t-online.de> wrote...
Victor Bazarov wrote:
"Simon Reye" <si*******@yahoo.com.au> wrote... Is there a way to force a floating point value to be invalid? I'm
reading floating points from a device and every now and then I get an
invalid number, but it is too few and far between for me to be able
to
test exception handling properly. I want to be able to force an
invalid
floating point value so I can test it. I can put what ever hex
values I want into the device but don't have a clue what hex values
will create and invalid floating point value.
IIRC, IEEE floats are "invalid" if all bits are set. Such value
is called "Not a Number", NaN. So, if you create a file that has
only char values of -1, any attempt to read those as a float or
a double should likely get you NaN (on a two's complement system).
Or simply use std::numeric_limits<float>::quiet_NaN() or
std::numeric_limits<float>::signaling_NaN(). I don't know the
difference between those two, so I don't know if it's important which
one to use.
The difference between those is simple: the former does not cause
the hardware to generate a hardware-specific signal when reading
it from memory or storing it to memory, the latter does. If the
intention of the test is to debug the code that deals with those
things read from a stream, I'd try reading quiet_NaN first. Even
reading signaling_NaN should probably raise a signal...
BTW, an implementation does not have to provide those, to check
whether they are there, use std::numeric_limits<float>::has_XXXX
functions.
OTOH, it's still simpler to create a file with 0xFF in all bytes
to test reading those NaN values, IMHO. A hex editor is all you
need...
Victor