How is the binary presentation of a float number in java? for example, what is the binary presentation of (5.34) for example.
thanks.
How is the binary presentation of a float number in java? for example, what is the binary presentation of (5.34) for example.
thanks.
https://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#longBitsToDouble(long)
If the argument is 0x7ff0000000000000L, the result is positive infinity.
If the argument is 0xfff0000000000000L, the result is negative infinity.
If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns. Distinct values of NaN are only distinguishable by use of the Double.doubleToRawLongBits method.
In all other cases, let s, e, and m be three values that can be computed from the argument:
int s = ((bits >> 63) == 0) ? 1 : -1;
int e = (int)((bits >> 52) & 0x7ffL);
long m = (e == 0) ?
(bits & 0xfffffffffffffL) << 1 :
(bits & 0xfffffffffffffL) | 0x10000000000000L;
Then the floating-point result equals the value of the mathematical expression s·m·2e-1075.
The conversion can be done in reverse. For "normal" numbers x, let s
be the sign of the value, e
be floor(log2(x)) + 1075, and m
be rounded from x/2e, and shift the values appropriately: s
is the high bit, e
is the next 11 bits, m
is the remaining 52 bits.
In layman terms floating points are represented using powers of 2. Every floating point has three parts.
Sign part is easy. It hold negative and positive value. Your floating point value represented approximately using mantissa and exponent is used to scale it. See following site for examples. Your 5.34 value is below. Note that it is approximation, as you can see below your number is not 5.34 but 5.340000152587891.