As I understand it java will store a float in memory as a 32 bit integer with the following properties:
This leaves no spare bits for the three special cases:
I can guess that negative 0 could be used to store one of these.
How are these actually represented in memory?
Scalars of type float are stored using four bytes (32-bits). The format used follows the IEEE-754 standard. The mantissa represents the actual binary digits of the floating-point number. The power of two is represented by the exponent.
In computing, NaN (/næn/), standing for Not a Number, is a member of a numeric data type that can be interpreted as a value that is undefined or unrepresentable, especially in floating-point arithmetic.
By default, floating-point numbers are double in Java. In order to store them in the float variable, you need to cast them explicitly or suffix with 'f' or 'F'. Float uses 1 bit for sign, 8 bits for exponent and 23 bits for mantissa but double uses 1 bit for sign, 11 bits for exponent and 52 bits for the mantissa.
There are 64 bits to represent a double (compared to 32 for int). The sign is represented by a bit this time (1 for "-" and 0 for "+"). The exponent is an 11-bit binary number, but is stored as a "positive" number in the range 0..
Java specifies that floating point numbers follow the IEEE 754 standard.
This is how it's stored:
Now, I have executed below method with different double values:
public static void print(double d){ System.out.println(Long.toBinaryString(Double.doubleToRawLongBits(d))); }
I executed with these values:
print(Double.NaN); print(Double.NEGATIVE_INFINITY); print(Double.POSITIVE_INFINITY); print(-Double.MAX_VALUE); print(Double.MAX_VALUE);
And got the following output for the values above (formatted for readability):
NaN: 0111111111111000000000000000000000000000000000000000000000000000 -Inf: 1111111111110000000000000000000000000000000000000000000000000000 +Inf: 0111111111110000000000000000000000000000000000000000000000000000 -Max: 1111111111101111111111111111111111111111111111111111111111111111 +Max: 0111111111101111111111111111111111111111111111111111111111111111
Wikipedia explains that when the exponent field is all-bits-1, the number is either Inf or NaN. Inf has all bits of the mantissa zero; NaN has at least one bit in the mantissa set to 1. The sign bit retains its normal meaning for Inf but is not meaningful for NaN. Java's Double.NaN
is one particular value that will be interpreted as NaN, but there are 253−3 others.
From here:
Q. How are zero, infinity and NaN represented using IEEE 754?
A. By setting all the exponent bits to 1. Positive infinity = 0x7ff0000000000000 (all exponent bits 1, sign bit 0 and all mantissa bits 0), negative infinity = 0xfff0000000000000 (all exponent bits 1, sign bit 1 and all mantissa bits 0), NaN = 0x7ff8000000000000 (all exponent bits 1, at least one mantissa bit set). Positive zero = all bits 0. Negative zero = all bits 0, except sign bit which is 1.
Also refer the Javadocs about NAN, Positive Infinity and Negative Infinity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With