Checking for invalid floating point numbers

L

LSW

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,
 
V

Victor Bazarov

LSW said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Puppet_Sock

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top