Like the title says, I don't understand how Java's primitive data type long
maximum and minimum values are smaller than a float
's maximum and minimum. Despite the long
being 64bit and float
being 32bit.
What is going on?
Like the title says, I don't understand how Java's primitive data type long
maximum and minimum values are smaller than a float
's maximum and minimum. Despite the long
being 64bit and float
being 32bit.
What is going on?
the reason is because a float using floating point precision. A Long can store a high number of precise digits, while I float can store a high value but without the same precision in its lower bits.
In a sense, a float stores values in scientific/exponential notation. thus a large value can be stored in a small number of bits. Think 2 x10^200, this is huge number but can be stored in a small number of bits.
A 32-bit float
isn't just a simple number, like a long
; it's broken up into fields. The fields are:
The exponent field is encoded (usually, it's just an offset from the real exponent), but it represents exponents from -126 to 127. (That covers 254 of the possible 256 values; the other two are used for special purposes.) The mantissa represents a value that can be from 1 to a value just below 2 (actually 2 - 2-23). This means that the largest possible float
is 2127 x (2 - 2-23), or almost 2128, which is larger than the largest possible long
value which is 263-1.
More information: