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
Call
What 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
Parameter
Description
a
The first factor.
b
The second factor.
c
The value added to the exact product a * b.
Return Value
Return Value
Description
double / float
a * b + c computed exactly and rounded once to the nearest representable value.
Compare with Similar Methods
Ways to combine a multiplication and an addition:
Method
How it differs
a * b + c
Two rounding steps; the plain operators are enough for most code.
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.
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.
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);
}
}
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.
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.
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+.
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.
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.
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.