4.2.3 Floating-Point Types and Values

The floating-point types are float and double, representing the single-precision 32-bit and double-precision 64-bit format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Standard 754-1985 (IEEE, New York).

The IEEE 754 standard includes not only positive and negative sign-magnitude numbers, but also positive and negative zeros, positive and negative infinities, and a special Not-a-Number (hereafter abbreviated NaN). The NaN value is used to represent the result of certain operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN (§20.9.5) and Double.NaN (§20.10.5).

The finite nonzero values of type float are of the form , where s is +1 or -1, m is a positive integer less than , and e is an integer between -149 and 104, inclusive. Values of that form such that m is positive but less than and e is equal to -149 are said to be denormalized.

The finite nonzero values of type double are of the form , where s is +1 or -1, m is a positive integer less than , and e is an integer between -1075 and 970, inclusive. Values of that form such that m is positive but less than and e is equal to -1075 are said to be denormalized.

Except for NaN, floating-point values are ordered; arranged from smallest to largest, they are negative infinity, negative finite nonzero values, negative zero, positive zero, positive finite nonzero values, and positive infinity.

Positive zero and negative zero compare equal; thus the result of the expression 0.0==-0.0 is true and the result of 0.0>-0.0 is false. But other operations can distinguish positive and negative zero; for example, 1.0/0.0 has the value positive infinity, while the value of 1.0/-0.0 is negative infinity. The operations Math.min and Math.max also distinguish positive zero and negative zero.

NaN is unordered, so the numerical comparison operators <, <=, >, and >= return false if either or both operands are NaN (§15.19.1). The equality operator == returns false if either operand is NaN, and the inequality operator != returns true if either operand is NaN (§15.20.1). In particular, x!=x is true if and only if x is NaN, and (x<y) == !(x>=y) will be false if x or y is NaN.

Any value of a floating-point type may be cast to or from any numeric type. There are no casts between floating-point types and the type boolean.