Generate invalid float

Discussion in 'C++' started by Simon Reye, Jul 21, 2003.

  1. Simon Reye

    Simon Reye Guest

    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
     
    Simon Reye, Jul 21, 2003
    #1
    1. Advertising

  2. "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
     
    Patrick Frankenberger, Jul 21, 2003
    #2
    1. Advertising

  3. Simon Reye

    Rolf Magnus Guest

    Victor Bazarov wrote:

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

    >
    > 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.
     
    Rolf Magnus, Jul 21, 2003
    #3
  4. "Rolf Magnus" <> wrote...
    > Victor Bazarov wrote:
    >
    > > "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.

    > >
    > > 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
     
    Victor Bazarov, Jul 21, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy
    Replies:
    7
    Views:
    6,319
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,491
    Jeff Schwab
    Jan 16, 2004
  3. Jukka Lehtonen

    operator== (float, float)

    Jukka Lehtonen, Aug 4, 2004, in forum: C++
    Replies:
    5
    Views:
    2,452
    Jukka Lehtonen
    Aug 5, 2004
  4. bd
    Replies:
    0
    Views:
    660
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,628
    James Kanze
    Oct 8, 2009
Loading...

Share This Page