Checking for invalid floating point numbers

Discussion in 'C++' started by LSW, Jun 8, 2007.

  1. LSW

    LSW Guest

    I'm using Borland Turbo C++ 3.0 to develop an embedded system to shift
    data around a network. At the moment we receive a string of bytes over a
    serial line and reassemble them into floating point values.

    If the bytes are not assembled correctly then it's possible to produce
    some floating point values that aren't 'genuine' numbers. Does anyone
    have any suggestions on how to check a float/double is valid (i.e.
    checking for NANs, infinity etc.)?

    Turbo C++ doesn't seem to have much support for this.

    Thanks,
    --
    L.
     
    LSW, Jun 8, 2007
    #1
    1. Advertising

  2. LSW wrote:
    > I'm using Borland Turbo C++ 3.0 to develop an embedded system to shift
    > data around a network. At the moment we receive a string of bytes
    > over a serial line and reassemble them into floating point values.
    >
    > If the bytes are not assembled correctly then it's possible to produce
    > some floating point values that aren't 'genuine' numbers. Does anyone
    > have any suggestions on how to check a float/double is valid (i.e.
    > checking for NANs, infinity etc.)?


    Implementation-specific stuff. Ask in the newsgroup for your compiler.
    The IEEE 754 specification leaves very little to interpretation. If
    the system for which you're programming supports it, you can make the
    checking yourself.

    > Turbo C++ doesn't seem to have much support for this.


    <shrug> The standard has 'std::numeric_limits' that do include the
    stuff like 'infinity' or 'isNaN' or some such, RTFM. If your compiler
    doesn't implement them it's either non-compliant, or that stuff is
    implementation-specific (allowed not to be implemented), or both.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jun 8, 2007
    #2
    1. Advertising

  3. On 2007-06-08 18:25, LSW wrote:
    > I'm using Borland Turbo C++ 3.0 to develop an embedded system to shift
    > data around a network. At the moment we receive a string of bytes over a
    > serial line and reassemble them into floating point values.
    >
    > If the bytes are not assembled correctly then it's possible to produce
    > some floating point values that aren't 'genuine' numbers. Does anyone
    > have any suggestions on how to check a float/double is valid (i.e.
    > checking for NANs, infinity etc.)?
    >
    > Turbo C++ doesn't seem to have much support for this.


    Include <cmath> or <math.h> and use isnan(), isinf(), etc.

    By they way, I don't know much about Borland compilers but isn't 3.0
    quite old? It might be worthwhile to look into upgrading.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Jun 8, 2007
    #3
  4. LSW

    Puppet_Sock Guest

    On Jun 8, 12:25 pm, LSW <nospam> wrote:
    [snip]
    > Does anyone
    > have any suggestions on how to check a float/double is valid (i.e.
    > checking for NANs, infinity etc.)?


    Other than what Victor said (which was great) I have
    a couple thoughts.

    How do you know that the transmitted data isn't actually
    a NAN etc.? Is it impossible for such things to get pushed
    into the other end of the pipe? So, if you reject a NAN,
    maybe you are rejecting the intended data.

    What about some sort of CRC or other similar check? That
    would be appropriate for some length of message chunks.
    They get packaged somehow at that end, then reassembled
    at this end, and the CRC checked.
    Socks
     
    Puppet_Sock, Jun 8, 2007
    #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. H aka N
    Replies:
    15
    Views:
    15,724
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,516
  3. Replies:
    4
    Views:
    1,310
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,374
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    274
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page