---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 15 Edition"
java_version: 15
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.2.4"
heading: "4.2.4. Floating-Point Operations"
parent: "4.2. Primitive Types and Values"
source: https://docs.oracle.com/javase/specs/jls/se15/html/jls-4.html#jls-4.2.4
id: "4.2.4"
url: https://jvm.obfuscated.software/jls/se15/4.2.4.html
raw: https://jvm.obfuscated.software/jls/se15/4.2.4.md
---

# 4.2.4. Floating-Point Operations

The Java programming language provides a number of operators that act on floating-point values:

- The comparison operators, which result in a value of type `boolean`:

  - The numerical comparison operators `<`, `<=`, `>`, and `>=` ([§15.20.1](15.20.1.md))
  - The numerical equality operators `==` and `!=` ([§15.21.1](15.21.1.md))

- The numerical operators, which result in a value of type `float` or `double`:

  - The unary plus and minus operators `+` and `-` ([§15.15.3](15.15.3.md), [§15.15.4](15.15.4.md))
  - The multiplicative operators `*`, `/`, and `%` ([§15.17](15.17.md))
  - The additive operators `+` and `-` ([§15.18.2](15.18.2.md))
  - The increment operator `++`, both prefix ([§15.15.1](15.15.1.md)) and postfix ([§15.14.2](15.14.2.md))
  - The decrement operator `--`, both prefix ([§15.15.2](15.15.2.md)) and postfix ([§15.14.3](15.14.3.md))

- The conditional operator `? :` ([§15.25](15.25.md))

- The cast operator ([§15.16](15.16.md)), which can convert from a floating-point value to a value of any specified numeric type

- The string concatenation operator `+` ([§15.18.1](15.18.1.md)), which, when given a `String` operand and a floating-point operand, will convert the floating-point operand to a `String` representing its value in decimal form (without information loss), and then produce a newly created `String` by concatenating the two strings

Other useful constructors, methods, and constants are predefined in the classes `Float`, `Double`, and `Math`.

If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral.

If at least one of the operands to a numerical operator is of type `double`, then the operation is carried out using 64-bit floating-point arithmetic, and the result of the numerical operator is a value of type `double`. If the other operand is not a `double`, it is first widened ([§5.1.5](5.1.5.md)) to type `double` by numeric promotion ([§5.6](5.6.md)).

Otherwise, the operation is carried out using 32-bit floating-point arithmetic, and the result of the numerical operator is a value of type `float`. (If the other operand is not a `float`, it is first widened to type `float` by numeric promotion.)

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

See [§4.2.5](4.2.5.md) for an idiom to convert floating-point expressions to `boolean`.

Operators on floating-point numbers behave as specified by IEEE 754, with the exception of certain operators described below. In particular, the Java programming language requires support of IEEE 754 *subnormal* floating-point numbers and *gradual underflow*, which make it easier to prove desirable properties of particular numerical algorithms. Floating-point operations do not "flush to zero" if the calculated result is a subnormal number.

Floating-point arithmetic is an approximation to real arithmetic. While there are an infinite number of real numbers, a particular floating-point format only has a finite number of values. In the Java programming language, a *rounding policy* is a function used to map from a real number to a floating-point value in a given format. For real numbers in the representable range of a floating-point format, a continuous segment of the real number line is mapped to a single floating-point value. The real number whose value is numerically equal to a floating-point value is mapped to that floating-point value; for example, the real number 1.5 is mapped to the floating-point value 1.5 in a given format. The Java programming language defines two rounding policies, as follows:

- The *round to nearest* rounding policy applies to all floating-point operations except for (i) conversion to an integer value, and (ii) floating-point remainder. Under the round to nearest rounding policy, inexact results must be rounded to the representable value nearest to the infinitely precise result; if the two nearest representable values are equally near, then the value whose least significant bit is zero is chosen.

  The round to nearest rounding policy corresponds to the default rounding-direction attribute for binary arithmetic in IEEE 754, *roundTiesToEven*.

  The *roundTiesToEven* rounding-direction attribute was known as the "round to nearest" rounding mode in the 1985 version of the IEEE 754 Standard. The rounding policy in the Java programming language is named after this rounding mode.

- The *round toward zero* rounding policy applies to (i) conversion of a floating-point value to an integer value ([§5.1.3](5.1.3.md)), and (ii) floating-point remainder ([§15.17.3](15.17.3.md)). Under the round toward zero rounding policy, inexact results are rounded to the nearest representable value that is not greater in magnitude than the infinitely precise result. For conversion to integer, the round toward zero rounding policy is equivalent to truncation where fractional significand bits are discarded.

  The round toward zero rounding policy corresponds to the *roundTowardZero* rounding-direction attribute for binary arithmetic in IEEE 754.

  The *roundTowardZero* rounding-direction attribute was known as the "round toward zero" rounding mode in the 1985 version of the IEEE 754 Standard. The rounding policy in the Java programming language is named after this rounding mode.

The Java programming language requires that floating-point arithmetic behave as if every floating-point operation rounds its floating-point result to the result precision. The rounding policy used for each floating-point operation is either round to nearest or round toward zero, as specified above.

A floating-point operation that overflows produces a signed infinity.

A floating-point operation that underflows produces a subnormal value or a signed zero.

A floating-point operation that has no unique mathematically defined result produces NaN.

All numeric operations with NaN as an operand produce NaN as a result.

A floating-point operator can throw an exception ([§11 (*Exceptions*)](11.md)) for the following reasons:

- Any floating-point operator can throw a `NullPointerException` if unboxing conversion ([§5.1.8](5.1.8.md)) of a null reference is required.
- The increment and decrement operators `++` ([§15.14.2](15.14.2.md), [§15.15.1](15.15.1.md)) and `--` ([§15.14.3](15.14.3.md), [§15.15.2](15.15.2.md)) can throw an `OutOfMemoryError` if boxing conversion ([§5.1.7](5.1.7.md)) is required and there is not sufficient memory available to perform the conversion.

**Example 4.2.4-1. Floating-point Operations**

```
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++)
            System.out.print(" " + (d /= 100000));
        System.out.println();
        // An example of NaN:
        System.out.print("0.0/0.0 is Not-a-Number: ");
        d = 0.0/0.0;
        System.out.println(d);
        // An example of inexact results and rounding:
        System.out.print("inexact results with float:");
        for (int i = 0; i < 100; i++) {
            float z = 1.0f / i;
            if (z * i != 1.0f)
                System.out.print(" " + i);
        }
        System.out.println();
        // Another example of inexact results and rounding:
        System.out.print("inexact results with double:");
        for (int i = 0; i < 100; i++) {
            double z = 1.0 / i;
            if (z * i != 1.0)
                System.out.print(" " + i);
        }
        System.out.println();
        // An example of cast to integer rounding:
        System.out.print("cast to int rounds toward 0: ");
        d = 12345.6;
        System.out.println((int)d + " " + (int)(-d));
    }
}
```

This program produces the output:

```
overflow produces infinity: 1.0E308*10==Infinity
gradual underflow: 3.141592653589793E-305
    3.1415926535898E-310 3.141592653E-315 3.142E-320 0.0
0.0/0.0 is Not-a-Number: NaN
inexact results with float: 0 41 47 55 61 82 83 94 97
inexact results with double: 0 49 98
cast to int rounds toward 0: 12345 -12345
```

This example demonstrates, among other things, that gradual underflow can result in a gradual loss of precision.

The results when `i` is `0` involve division by zero, so that `z` becomes positive infinity, and `z * 0` is NaN, which is not equal to `1.0`.
