programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# cast an int to a long and store it in a float

Ranch Hand
Posts: 192
This is the code: It compiles fine and the output is 9.0. I do not understand how you can cast an int to a long and store it in a float; a float is 32-bit floating-point value and a long is a 64-bit ingetral value.

Saloon Keeper
Posts: 7993
143
Floats may be less precise than longs, they are still wider. Longs represent values between -2^63 and 2^63-1, while floats represent values between minus infinity and plus infinity.

Urs Waefler
Ranch Hand
Posts: 192
I do not think that floats represent values between minus infinity and plus infinity; that seems to be impossible. A float is 32-bit floating-point value and not an infinity-bit floating value.

Saloon Keeper
Posts: 4036
94
• 1
It is not impossible, as https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 outlines. Java's float and double implement IEEE-754 which allow for the full range of FP numbers between negative and positive infinity.

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
The trick here is that floating point values become less and less precise as their magnitude increases. When you coerce a long into a float, it will take the float value that is closest to the exact long value. You can think of it as being "the same number, just not very precise".

Tim Moores
Saloon Keeper
Posts: 4036
94
You can think of it as being "the same number, just not very precise".

Or: "a number that's close to it, very accurately" :-)

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Tim Moores wrote:Or: "a number that's close to it, very accurately" :-)

Depends on your definition of "close". There is a value that will give you an absolute round-off error of 2^103.

Marshal
Posts: 56600
172
Stephan van Hulst wrote:. . . floating point values become less and less precise as their magnitude increases. . . .
They also become less and less precise when their magnitude becomes smaller, i.e. closer to 0.

Stephan van Hulst
Saloon Keeper
Posts: 7993
143
Campbell Ritchie wrote:
Stephan van Hulst wrote:. . . floating point values become less and less precise as their magnitude increases. . . .
They also become less and less precise when their magnitude becomes smaller, i.e. closer to 0.

Huh? You'll have to explain that one to me. The smaller the magnitude, the more bits in the mantissa are used for decimals.

Sheriff
Posts: 4289
127
I think we're talking about negative exponents, like

Sheriff
Posts: 11494
180
I leaning Stephan's way. Maybe the crux is the difference between "precise" and "accurate". Those small numbers are very precise but they may not be very accurate in the context of a calculation.

Campbell Ritchie
Marshal
Posts: 56600
172
Stephan van Hulst wrote:. . . . The smaller the magnitude, the more bits in the mantissa are used for decimals.
The smaller the magnitude, at least such that |x| < z where z = Float#MIN_NORMAL, the more 0 bits there are in the mantissa and the fewer 1 bits to support precision.I think \u00db is ×
By the time you get to such small subnormal values, there are no values between x and 0.0, nor between x and 2x. At such small values, the eleven bits (in a double) for the exponent are all 0. It is part of the standard behaviour of IEEE754 numbers.

Damn! I have written %...f when I meant %...e. Correcting it.