Help coderanch get a
new server
by contributing to the fundraiser
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# how does long fit in float ?

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
hi I have a very fundamental question. The float datatype is of 32 bits and long is of 64 bits. Then why doesn't this cause a compiler error ?
long a = 1;
float c=a;
Thanks a lot
Vidya

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
The widening conversion from long to float is authorized, but it is not without consequences. Here is what JLS 5.1.2 Widening Primitive Conversion has to say about it:

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (�4.2.4).

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:
It's because a float can hold a much bigger number than a long. How? It uses scientific notation. The float is made up of a 1-bit sign, 8-bit exponent and 23-bit mantissa.
The largest positive finite float literal is 3.40282347e+38f.
The largest positive finite double literal is 1.79769313486231570e+308.
These are numbers so big that they don't even have names.
The float is a 3 followed by 38 zeroes! The double is a 1 followed by 308 zeroes!
Numbers this large obviously can't be stored with a great deal of accuracy. In fact, a float starts getting inaccurate around 6 million if memory serves.

 I didn't say it. I'm just telling you what this tiny ad said. We need your help - Coderanch server fundraiser https://coderanch.com/t/782867/Coderanch-server-fundraiser