473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Is there such thing as invalid floating point ?

Hi,

I am working on an IO library, and I am reading a binary blob which
represent a floating point. I would like to know what is the correct
way to interpret it. For integer type using a stringstream works like
a charm, but for floating point I am getting a behavior that I don't
quite understand, for instance (*).
BTW NaN is the IEEE with all bit set, right ? What is the
representation for infinity ?

Thanks !
-Mathieu

(*)
#include <limits>
#include <iostream>
#include <sstream>

int main()
{
double inf = std::numeric_limits<double>::infinity();
//double nan = std::numeric_limits<double>::quiet_NaN();
std::cout << inf << std::endl;
std::cout << nan << std::endl;
std::stringstream ss;
ss << inf;
double out;
ss >out;
std::cout << out << std::endl;
return 0;
}

returns:
inf
nan
4.86135e-270
Nov 27 '07 #1
4 1800
On Nov 27, 2:29 pm, mathieu <mathieu.malate...@gmail.comwrote:
Hi,

I am working on an IO library, and I am reading a binary blob which
represent a floating point. I would like to know what is the correct
way to interpret it. For integer type using a stringstream works like
a charm, but for floating point I am getting a behavior that I don't
quite understand, for instance (*).
BTW NaN is the IEEE with all bit set, right ? What is the
representation for infinity ?

Thanks !
-Mathieu

(*)
#include <limits>
#include <iostream>
#include <sstream>

int main()
{
double inf = std::numeric_limits<double>::infinity();
//double nan = std::numeric_limits<double>::quiet_NaN();
std::cout << inf << std::endl;
std::cout << nan << std::endl;
std::stringstream ss;
ss << inf;
double out;
ss >out;
std::cout << out << std::endl;
return 0;

}

returns:
inf
nan
4.86135e-270

D'oh ! I am missing the if test:

if( ss >out )
std::cout << out << std::endl;

My issue is solved, I know how to detect invalid floating point value.

Sorry for the noise,

-Mathieu
Nov 27 '07 #2
mathieu wrote:
double out;
ss >out;
You need to check the error state here. The compiler will
not read 'inf' into double. It fails and leaves the out variable
with what ever non-initialized value it had before.

double out = 123.456;

if(ss >out) {
cout << "input failed (just for giggles out = " << out << "\n";
} else {
cout << out;
}
Nov 27 '07 #3
mathieu wrote:
>
D'oh ! I am missing the if test:

if( ss >out )
std::cout << out << std::endl;

My issue is solved, I know how to detect invalid floating point value.
Don't forget to clear the error if you want to read anything further
from that stream.
Nov 27 '07 #4
On Nov 27, 3:35 pm, Ron Natalie <r...@spamcop.netwrote:
mathieu wrote:
D'oh ! I am missing the if test:
if( ss >out )
std::cout << out << std::endl;
My issue is solved, I know how to detect invalid floating point value.

Don't forget to clear the error if you want to read anything further
from that stream.
Thanks !
Nov 27 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Allcomp | last post by:
Hello, I have seen something really strange in VB6 If I do a Int ( (5 * 1.2)) , I receive the value 5, but I should receive 6? Is this a bug or something really "normal". I can see that if I...
3
by: Simon Reye | last post by:
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...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
21
by: Allin Cottrell | last post by:
OK, I realize that what I am asking here is not likely to have a answer within the C standard. Nonetheless, it is not specific to any particular platform, so I'll hazard the question anyway. A...
2
by: jawilson | last post by:
Hello, I am working on a project in Borland Developer Studio 2006, and am running into an error I don't understand. Here is the relevant code: ...
2
by: marko.suonpera | last post by:
I'm using the Newmat library where element access in ColumnVector type is defined as follows: typedef double Real; Real& ColumnVector::operator()(int m) { REPORT if (m<=0 || mnrows)...
3
by: LSW | last post by:
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...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
7
by: pcauchon | last post by:
When I try to run this code on my machine (iMac with MacOS 10.5), I get very strange results. I am using this compiler: i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5465) The first...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.