Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

Converting Doubles to Floats

nupshur
Beginner
287 Views

Hi,

I wrote the following test program

int main() {

double a;

cin >> a;

float b = a;

cout << a << endl;

}

When I input 3.88e+308, the same comes as output. Isn't this much larger than the maximum representable value for a float? Does the compiler just take care of this for me, i.e. do I ever have to worry about anything other than loss of precision when converting doubles to floats?

Thanks,

~Nafis

0 Kudos
2 Replies
TimP
Honored Contributor III
287 Views
Did you intend to display the value of b? Are you using a 32-bit ICL, in which case the result might be affected by whether you use an SSE2 option (-xW et al) or a -fp-port option, including -Op or -fp:precise? Translate those if you are using linux C++.
0 Kudos
nupshur
Beginner
287 Views

Oops --I had been awake for 48 hours straight when I wrote that.Yes, I meant to display b, and it prints as infinity. A more general question, then. Is there a standard way in C++ to make sure that a double isn't too small or too big to fit into a float? Thanks,

~Nafis

0 Kudos
Reply