Reputation: 10151
Here: http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2.3 it says that:
The finite nonzero values of any floating-point value set can all be expressed in the form
s · m · 2^(e - N + 1)
, wheres
is+1
or-1
,m
is a positive integer less than2^N
, ande
is an integer betweenEmin = -(2^(K-1)-2)
andEmax = 2^(K-1)-1
, inclusive, and whereN
andK
are parameters that depend on the value set.
and there is a table below:
Parameter float
N 24
K 8
So let's say N = 24
and K = 8
then we can have the following value from the formula:
s · 2^N · 2^(2^(K-1)-1 - N + 1)
which gives us according to values specified in the table:
s * 2^24 * 2^(127 - 24)
which is equal to s * 2^127
. But float has only 32 bits so it's not possible to store in it such a big number.
So it's obvious that initial formula should be read in a different way. How then? Also in javadoc for Float max value: http://docs.oracle.com/javase/7/docs/api/java/lang/Float.html#MAX_VALUE
it says:
A constant holding the largest positive finite value of type float,
(2-2^-23)·2^127
This also doesn't make sense, as resulting value is much larger than 2^32 - which is possible the biggest value that can be stored in float variable. So again, I'm misreading this notation. So how it should be read?
Upvotes: 0
Views: 118
Reputation: 2937
The idea with the floating point notation is to store a much larger range of numbers than can be stored in the same space (bytes) with the integer representation. So, for example, you say that the "resulting value is much larger than 2^32". But, that would only be a problem if we're storing a typical binary number as one computes in a typical math class.
Instead, floating point representations break those 32 bytes into two main parts: - significand - exponent
For simplicity, imagine that 3 bytes are used for the significand and 1 byte for the exponent. Also assume that each of these is your typical binary integer style of representation. So, the three bytes can have a value 2^24, or 2^23 if you want to keep one bit for the sign. However, the other byte can store up to 2^7 (if you want a sign there too).
So, you could express 500^100, by storing the 500 in the three bytes and the 100 in the 1 byte.
Essentially, one cannot store every number precisely. One changes it into significant form and one can store as many significant digits as the portion reserved for the significand (3 bytes in this example).
Rather than try to explain the complications, check this Wikipedia article for more.
Upvotes: 2