Java Reference
In-Depth Information
Floating-point operations do not “flush to zero” if the calculated result is a denormalized
number.
The Java programming language requires that floating-point arithmetic behave as if every
floating-point operator rounded its floating-point result to the result precision. Inexact res-
ults must be rounded to the representable value nearest to the infinitely precise result; if
the two nearest representable values are equally near, the one with its least significant bit
zero is chosen. This is the IEEE 754 standard's default rounding mode known as round to
nearest .
The Java programming language uses round toward zero when converting a floating value
to an integer (§ 5.1.3 ) , which acts, in this case, as though the number were truncated, dis-
carding the mantissa bits. Rounding toward zero chooses at its result the format's value
closest to and no greater in magnitude than the infinitely precise result.
A floating-point operation that overflows produces a signed infinity.
A floating-point operation that underflows produces a denormalized value or a signed zero.
A floating-point operation that has no mathematically definite result produces NaN. All nu-
meric operations with NaN as an operand produce NaN as a result.
A floating-point operator can throw an exception (§11) for the following reasons:
• Any floating-point operator can throw a NullPointerException if unboxing conversion
5.1.8 ) of a null reference is required.
• The increment and decrement operators ++ 15.14.2 , § 15.15.1 ) and -- 15.14.3 ,
§ 15.15.2 ) can throw an OutOfMemoryError if boxing conversion (§ 5.1.7 ) is required
and there is not sufficient memory available to perform the conversion.
Example 4.2.4-1. Floating-point Operations
Click here to view code image
class Test {
public static void main(String[] args) {
// An example of overflow:
double d = 1e308;
System.out.print("overflow produces infinity: ");
System.out.println(d + "*10==" + d*10);
// An example of gradual underflow:
d = 1e-305 * Math.PI;
System.out.print("gradual underflow: " + d + "\n ");
for (int i = 0; i < 4; i++)
Search WWH ::




Custom Search