Generate invalid float

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Reye

    Generate invalid float

    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

  • Patrick Frankenberger

    #2
    Re: Generate invalid float


    "Simon Reye" wrote:[color=blue]
    > 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.[/color]

    Use std::numeric_li mits<yourfloati ngpointtype> from <limits>.
    You can choose between quiet_NaN(), signaling_NaN() and infinity().

    double f;
    f = std::numeric_li mits<double>::q uiet_NaN();

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

    HTH,
    Patrick


    Comment

    • Rolf Magnus

      #3
      Re: Generate invalid float

      Victor Bazarov wrote:
      [color=blue]
      > "Simon Reye" <simonreye@yaho o.com.au> wrote...[color=green]
      >> 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.[/color]
      >
      > 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).[/color]

      Or simply use std::numeric_li mits<float>::qu iet_NaN() or
      std::numeric_li mits<float>::si gnaling_NaN(). I don't know the
      difference between those two, so I don't know if it's important which
      one to use.

      Comment

      • Victor Bazarov

        #4
        Re: Generate invalid float

        "Rolf Magnus" <ramagnus@t-online.de> wrote...[color=blue]
        > Victor Bazarov wrote:
        >[color=green]
        > > "Simon Reye" <simonreye@yaho o.com.au> wrote...[color=darkred]
        > >> 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.[/color]
        > >
        > > 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).[/color]
        >
        > Or simply use std::numeric_li mits<float>::qu iet_NaN() or
        > std::numeric_li mits<float>::si gnaling_NaN(). I don't know the
        > difference between those two, so I don't know if it's important which
        > one to use.
        >[/color]

        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_li mits<float>::ha s_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


        Comment

        Working...