Arun Prasath

Ranch Hand

Posts: 60

posted 11 years ago

Rolling back to zero when reaching 4 bytes.

[My Blog]
*All roads lead to JavaRanch*

posted 11 years ago

Indeed, a float is only 32 bits (4 bytes) while a long is 64 bits (8 bytes).

But float is ofcourse a different kind of number - a floating point number instead of an integer. The limits (minimum and maximum values that it can hold) are different. The range of float is even larger than that of float, even though it has less bits!

The trick is that float is less precise - it has only about 6 decimal digits of precision. So you cannot store any number that fits into a long with the same precision into a float.

Try this, for example:

The output is:

123456789

123456792

You see that the float cannot exactly contain the number as the long, there is a rounding error in the last digits.

[ August 29, 2006: Message edited by: Jesper Young ]

But float is ofcourse a different kind of number - a floating point number instead of an integer. The limits (minimum and maximum values that it can hold) are different. The range of float is even larger than that of float, even though it has less bits!

The trick is that float is less precise - it has only about 6 decimal digits of precision. So you cannot store any number that fits into a long with the same precision into a float.

Try this, for example:

The output is:

123456789

123456792

You see that the float cannot exactly contain the number as the long, there is a rounding error in the last digits.

[ August 29, 2006: Message edited by: Jesper Young ]