Next Prev Up Contents
A Appendix: Floating Point
- A.1 - Special Values
-
- A.2 - Binary Format Conversion
-
- A.3 - Ordering
-
- A.4 - Summary of IEEE-754 Differences
-
This appendix discusses properties of Java floating point arithmetic: general precision notes and special values, binary format conversion, ordering. At the end is a section summarizing the differences between Java arithmetic and the IEEE 754 standard. For more information on the IEEE 754 standard, see "IEEE Standard for Binary Floating-Point Arithmetic, ANSI/IEEE Std. 754-1985."
Operations involving only single-precision float and integer values are performed using at least single-precision arithmetic and produce a single-precision result. Other operations are performed in double precision and produce a double precision result. Java floating-point arithmetic produces no exceptions.
Underflow is gradual.
A.1 Special Values
There is both a positive zero and a negative zero. The latter can be produced in a number of special circumstances: the total underflow of a * or / of terms of different sign; the addition of -0 to itself or subtraction of positive zero from it; the square root of -0. Converting -0 to a string results in a leading `-'. Apart from this, the two zeros are indistinguishable.
Calculations which would produce a value beyond the range of the arithmetic being used deliver a signed infinite result. An infinity (Inf) has a larger magnitude than any value with the same sign. Infinities of the same sign cannot be distinguished. Thus, for instance (1./0.) + (1./0.) == (1./0.). Division of a finite value by infinity yields a 0 result.
Calculations which cannot produce any meaningful numeric result deliver a distinguished result called Not A Number (NaN). Any operation having a NaN as an operand produces a NaN as the result. NaN is not signed and not ordered (see "Ordering"). Division of infinity by infinity yields NaN, as does subtraction of one infinity from another of the same sign.
A.2 Binary Format Conversion
Converting a floating-point value to an integer format results in a value with the same sign as the argument value and having the largest magnitude less than or equal to that of the argument. In other words, conversion rounds towards zero. Converting infinity or any value beyond the range of the target integer type gives a result having the same sign as the argument and the maximum magnitude of that sign. Converting NaN results in 0.
Converting an integer to a floating format results in the closest possible value in the target format. Ties are broken in favor of the most even value (having 0 as the least-significant bit).
The usual relational operators can be applied to floating-point values. With the exception of NaN, all floating values are ordered, with -Inf < all finite values < Inf.
-Inf == -Inf, +Inf == +Inf, -0. == 0. The ordering relations are transitive. Equality and inequality are reflexive.
NaN is unordered. Thus the result of any order relation between NaN and any other value is false and produces 0. The one exception is that "NaN != anything" is true.
Note that, because NaN is unordered, Java's logical inversion operator, !, does not distribute over floating point relationals as it can over integers.
A.4 Summary of IEEE-754 Differences
Java arithmetic is a subset of the IEEE-754 standard. Here is a summary of the key differences.
- Nonstop Arithmetic--The Java system will not throw exceptions, traps, or otherwise signal the IEEE exceptional conditions: invalid operation, division by zero, overflow, underflow, or inexact. Java has no signaling NaN.
- Rounding--Java rounds inexact results to the nearest representable value, with ties going to the value with a 0 least-significant bit. This is the IEEE default mode. But, Java rounds towards zero when converting a floating value to an integer. Java does not provide the user-selectable rounding modes for floating-point computations: up, down, or towards zero.
- Relational set--Java has no relational predicates which include the unordered condition, except for !=. However, all cases but one can be constructed by the programmer, using the existing relations and logical inversion. The exception case is ordered but unequal. There is no specific IEEE requirement here.
- Extended formats--Java does not support any extended formats, except that double will serve as single-extended. Other extended formats are not a requirement of the standard.
Next Prev Up Contents
The Java Language Specification
Generated with CERN WebMaker