Java Math fma() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 9+

Definition

In Java, Math.fma(a, b, c) is the fused multiply-add: it computes a * b + c as if with infinite precision and then rounds once. The expression a * b + c rounds twice: once after the multiplication and once after the addition.

That single rounding makes the result slightly more accurate, and it avoids an overflow in the middle of the calculation. It is useful for evaluating polynomials, dot products, and sums such as price times quantity plus tax. Many CPUs perform it in one hardware instruction.

A static method of the java.lang.Math class, added in Java 9 (JDK 9). Call it on the class name, as in Math.fma(...). No import is needed, because classes in java.lang are always visible. Requires Java 9 or newer.

Syntax

Pass three numbers of the same floating-point type. Mixed int and double arguments are widened to double.

Java
double total = Math.fma(19.99, 3, 5.0);       // 19.99 * 3 + 5.0
float f = Math.fma(2.5f, 4.0f, 0.5f);         // 10.5f
double next = Math.fma(result, x, coef);      // one Horner step
double exact = Math.fma(a, b, -(a * b));      // rounding error of a * b
CallWhat it does
Math.fma(2.0, 3.0, 4.0)2 * 3 + 4 = 10.0.
Math.fma(price, qty, tax)Total cost with one rounding step.
Math.fma(sum, x, coef)One step of Horner's polynomial rule.
Math.fma(a, b, -(a * b))The tiny error that the plain product a * b lost.

There are no int or long versions: integer products and sums are exact until they overflow. On hardware with a fused instruction, fma is fast; otherwise Java computes the exact result in software, which is slower than a * b + c.

Library declarations
Java
// java.lang.Math (Java 9+)
public static double fma(double a, double b, double c)
public static float fma(float a, float b, float c)

Parameters

ParameterDescription
aThe first factor.
bThe second factor.
cThe value added to the exact product a * b.

Return Value

Return ValueDescription
double / floata * b + c computed exactly and rounded once to the nearest representable value.

Compare with Similar Methods

Ways to combine a multiplication and an addition:

MethodHow it differs
a * b + cTwo rounding steps; the plain operators are enough for most code.
pow(a, n)Raising to a power; for a polynomial, Horner with fma needs no pow calls.
multiplyExact(a, b)Exact integer multiplication that throws on overflow.
BigDecimal.multiply(b).add(c)Exact decimal arithmetic for money, but much slower.

Java Examples

Example 1 — Price Times Quantity Plus Delivery

Compute an order total with fma, then compare with the plain expression and try the float overload. This example needs Java 9 or newer.

Java
public class MathFmaTotal {
    public static void main(String[] args) {
        double price = 19.99;
        int qty = 3;
        double delivery = 5.0;
        System.out.println("fma:   " + Math.fma(price, qty, delivery));
        System.out.println("plain: " + (price * qty + delivery));
        System.out.println("same?  " + (Math.fma(price, qty, delivery) == price * qty + delivery));
        System.out.println("fma(2.0, 3.0, 4.0) = " + Math.fma(2.0, 3.0, 4.0));
        System.out.println("float: " + Math.fma(2.5f, 4.0f, 0.5f));
    }
}

How It Works

  1. Widening. qty is an int, so it becomes the double 3.0 and the double overload runs; the result is the order total, 64.97.
  2. Often identical. For these values the two roundings of the plain expression happen to land on the same double as the single rounding, so same? prints true. Differences appear only in rare cases.
  3. Exact inputs stay exact. 2.0 * 3.0 + 4.0 is 10.0 and 2.5f * 4.0f + 0.5f is 10.5f; all of them are exactly representable, so no rounding happens at all, and the float overload returns a float.

Example 2 — Horner's Rule and a Dot Product

Evaluate 3x squared + 2x + 1 with one fma per coefficient, then add up the products of two vectors. Java 9+.

Java
public class MathFmaHorner {
    public static void main(String[] args) {
        double[] coef = {3, 2, 1};
        double[] xs = {0.0, 1.0, 2.0, 0.5};
        for (double x : xs) {
            double result = 0.0;
            for (double c : coef) {
                result = Math.fma(result, x, c);
            }
            System.out.println("p(" + x + ") = " + result);
        }

        double[] a = {1, 2, 3};
        double[] b = {4, 5, 6};
        double dot = 0.0;
        for (int i = 0; i < a.length; i++) {
            dot = Math.fma(a[i], b[i], dot);
        }
        System.out.println("dot = " + dot);
    }
}

How It Works

  1. One step per coefficient. Starting from 0.0, each fma(result, x, c) turns 3 into 3x + 2 and then into (3x + 2)x + 1, which equals 3x squared + 2x + 1 without any pow call.
  2. Checking a value. For x = 2.0 the steps are 3, then 3 * 2 + 2 = 8, then 8 * 2 + 1 = 17.0; for x = 0.5 they are 3, 3.5, and 2.75.
  3. Running dot product. The sum grows as 1 * 4 = 4, then 4 + 2 * 5 = 14, then 14 + 3 * 6 = 32.0, each step being a multiply-add into dot.

Example 3 — When One Rounding Beats Two

The classic demonstration: the plain expression loses a tiny amount that fma keeps, and an intermediate overflow disappears. Java 9+.

Java
public class MathFmaRounding {
    public static void main(String[] args) {
        System.out.println("0.1 * 10.0 - 1.0     = " + (0.1 * 10.0 - 1.0));
        System.out.println("fma(0.1, 10.0, -1.0) = " + Math.fma(0.1, 10.0, -1.0));
        System.out.println("overflow, plain: " + (1e308 * 2.0 - 1e308));
        System.out.println("overflow, fma:   " + Math.fma(1e308, 2.0, -1e308));
        System.out.println("infinity * 0:    " + Math.fma(Double.POSITIVE_INFINITY, 0.0, 1.0));
    }
}

How It Works

  1. The first rounding hides the error. The double 0.1 is slightly above one tenth, so 0.1 * 10.0 is 1.0000000000000000555 exactly, but rounding that product to a double gives exactly 1.0, and 1.0 - 1.0 is 0.0.
  2. fma keeps it. With no rounding after the multiplication, the exact product minus 1.0 is about 5.55E-17, and a single rounding of that tiny number is the value printed.
  3. No intermediate overflow. 1e308 * 2.0 is too big for a double, so the plain expression is already Infinity; fma subtracts 1e308 before rounding and returns 1.0E308. Infinity times zero stays NaN either way.

Frequently Asked Questions

It computes a * b + c exactly and rounds the result once. The plain expression a * b + c rounds twice, once after the multiplication and once after the addition.
Java 9. It has a double overload and a float overload. On older versions you must use a * b + c.
It can be: because it rounds only once, the result is the correctly rounded value of the exact a * b + c. For many inputs both give the same double, but sometimes the last digits differ, as in Math.fma(0.1, 10.0, -1.0).
In numeric code that adds up many products, such as polynomial evaluation with Horner rule, dot products, or matrix math, where extra accuracy matters. For everyday sums like prices, the plain operators or BigDecimal are simpler.
No. Java requires the multiplication to be rounded before the addition, so the compiler must not fuse it. Call Math.fma explicitly if you want the single rounding.

Next: getExponent()

The unbiased binary exponent of a float or double.

getExponent() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful