Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are NaN and Infinity of a float or double stored in memory?

Tags:

java

nan

infinity

As I understand it java will store a float in memory as a 32 bit integer with the following properties:

  • The first bit is used to determine the sign
  • The next 8 bits represent the exponent
  • The final 23 bits are used to store the fraction

This leaves no spare bits for the three special cases:

  • NaN
  • Positive Infinity
  • Negative Infinity

I can guess that negative 0 could be used to store one of these.

How are these actually represented in memory?

like image 919
roblovelock Avatar asked Mar 22 '16 09:03

roblovelock


People also ask

How are float values stored 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.

How is NaN represented in memory?

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.

How is a float or double value stored in memory in Java?

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.

How is a double represented in memory?

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..


2 Answers

Java specifies that floating point numbers follow the IEEE 754 standard.

This is how it's stored:

  • bit 0 : sign bit
  • bits 1 to 11 : exponent
  • bits 12 to 63 : fraction

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.

like image 131
Darshan Mehta Avatar answered Sep 29 '22 06:09

Darshan Mehta


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.

like image 38
Rahul Tripathi Avatar answered Sep 29 '22 05:09

Rahul Tripathi