Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Generate invalid float

Reply
Thread Tools

Generate invalid float

 
 
Simon Reye
Guest
Posts: n/a
 
      07-21-2003
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

 
Reply With Quote
 
 
 
 
Patrick Frankenberger
Guest
Posts: n/a
 
      07-21-2003

"Simon Reye" 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.


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


 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-21-2003
Victor Bazarov wrote:

> "Simon Reye" <(E-Mail Removed)> 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-21-2003
"Rolf Magnus" <(E-Mail Removed)> wrote...
> Victor Bazarov wrote:
>
> > "Simon Reye" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
operator== (float, float) Jukka Lehtonen C++ 5 08-05-2004 08:28 AM
need code to convert float format to internal java float format which is kept in 4 bytes integer Andy Java 7 05-10-2004 09:26 PM
static_cast<float>(a) versus float(a) Jim West C++ 4 01-16-2004 12:36 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments