Generate invalid float

S

Simon Reye

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.

Simon
 
P

Patrick Frankenberger

Simon Reye said:
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.

Use std::numeric_limits<yourfloatingpointtype> from <limits>.
You can choose between quiet_NaN(), signaling_NaN() and infinity().

double f;
f = std::numeric_limits<double>::quiet_NaN();

Be aware that NaN behave quite different than all other numbers. f!=f
returns true for example.

HTH,
Patrick
 
R

Rolf Magnus

Victor said:
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.
 
V

Victor Bazarov

Rolf Magnus said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,127
Messages
2,570,757
Members
47,315
Latest member
robertsoker

Latest Threads

Top