In C++, std::fma(x, y, z) calculates x times y plus z. The name stands for "fused multiply-add". For fma(2.0, 3.0, 4.0) the answer is 2 * 3 + 4 = 10.
The word "fused" is the key. A normal x * y + z rounds twice: once after the multiplication and once after the addition. fma() keeps the exact product, adds z, and rounds once. The result is a little more accurate, and on many processors it is also faster.
std::fma() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.
Syntax
Include <cmath> and call std::fma. Start with these lines.
C++
double a = std::fma(2.0, 3.0, 4.0); // 10
double b = std::fma(-2.0, 5.0, 3.0); // -7
float c = std::fma(1.5f, 2.0f, 0.5f); // 3.5f
Call
What it does
std::fma(2.0, 3.0, 4.0)
2 * 3 + 4. Returns 10.
std::fma(-2.0, 5.0, 3.0)
-10 + 3. Returns -7.
std::fma(0.1, 10.0, -1.0)
Keeps the tiny error of 0.1. Returns 5.55112e-17, while 0.1 * 10 - 1 gives 0.
On a processor without an FMA instruction the library calculates it in software, which can be slow. Use it when accuracy matters.
Library declarations
C++
float fma(float x, float y, float z);
double fma(double x, double y, double z);
long double fma(long double x, long double y, long double z);
// plus overloads for mixed and integer arguments
Parameters
Parameter
Description
x
Required. The first factor.
y
Required. The second factor.
z
Required. The number that is added to the product x * y.
Return Value
Return Value
Description
x * y + z
The result, rounded once. The type is the promoted type of the arguments.
NaN
If an argument is NaN, or if the product is 0 * inf.
inf
If the result is too big for the type.
C++ Examples
Example 1 — Multiply and Add
Use fma() with whole numbers and decimals. The answers match the plain formula for easy values.
fma(2, 3, 4) multiplies 2 * 3 to get 6 and then adds 4, which gives 10.
1.5 * 2 + 0.5 is 3.5. For a negative product, -2 * 5 + 3 is -10 + 3, which gives -7.
4 * 0.25 - 1 is 1 - 1, so the answer is exactly 0. For numbers like these, fma() and the plain formula agree.
Example 2 — One Rounding Instead of Two
Compare x * y + z with fma(x, y, z) for 0.1 * 10 - 1. The true answer is a tiny positive number.
C++
#include <cmath>
#include <iostream>
int main() {
double x = 0.1;
double y = 10.0;
double z = -1.0;
double plain = x * y + z;
double fused = std::fma(x, y, z);
std::cout << "x * y + z = " << plain << '\n';
std::cout << "fma(x, y, z) = " << fused << '\n';
return 0;
}
Output
x * y + z = 0
fma(x, y, z) = 5.55112e-17
How It Works
The double 0.1 is stored as 0.1000000000000000055..., a tiny bit above one tenth.
x * y rounds to exactly 1 by itself, so the tiny extra part is lost. Adding -1 then gives 0.
fma() keeps the exact product 1.000000000000000055... until after the addition, so the extra part survives: 5.55112e-17. Compile with -ffp-contract=off to stop the compiler from fusing the plain line.
Example 3 — Evaluate a Polynomial (Horner's Method)
The polynomial 3x² + 2x + 1 can be written as (3x + 2)x + 1. Each step is a multiply and an add, so it fits fma() perfectly.
The first fma() turns 3x + 2 into one value r. The second one multiplies it by x and adds 1.
For x = 2 the first step gives 8 and the second gives 8 * 2 + 1 = 17. For x = 0.5 the result is 2.75.
For x = -1 the first step gives -1, and then -1 * -1 + 1 is 2. Horner's method needs only two multiplications, and each one is accurate.
Frequently Asked Questions
std::fma(x, y, z) computes x * y + z as one operation and rounds only once at the end. The name means "fused multiply-add".
The value x * y + z, correctly rounded once. The result type is float, double, or long double, depending on the arguments.
fma() rounds only once, so the result is more accurate. It also maps to a single fast instruction on many CPUs. It is helpful for dot products, polynomials, and numerical code.
Not always. On a CPU with a hardware FMA instruction it is fast. On a CPU without one, the library must do it in software, which is much slower. For plain code, the compiler may already choose FMA for you.
Include <cmath> and write std::fma(). That always works and picks the C++ overloads. The global name ::fma often works too, but the standard does not promise it, and it may only take a double.