C++ std::fma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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
CallWhat 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

ParameterDescription
xRequired. The first factor.
yRequired. The second factor.
zRequired. The number that is added to the product x * y.

Return Value

Return ValueDescription
x * y + zThe result, rounded once. The type is the promoted type of the arguments.
NaNIf an argument is NaN, or if the product is 0 * inf.
infIf 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.

C++
#include <cmath>
#include <iostream>

int main() {
    std::cout << "fma(2, 3, 4)     = " << std::fma(2.0, 3.0, 4.0) << '\n';
    std::cout << "fma(1.5, 2, 0.5) = " << std::fma(1.5, 2.0, 0.5) << '\n';
    std::cout << "fma(-2, 5, 3)    = " << std::fma(-2.0, 5.0, 3.0) << '\n';
    std::cout << "fma(4, 0.25, -1) = " << std::fma(4.0, 0.25, -1.0) << '\n';
    return 0;
}

How It Works

  1. fma(2, 3, 4) multiplies 2 * 3 to get 6 and then adds 4, which gives 10.
  2. 1.5 * 2 + 0.5 is 3.5. For a negative product, -2 * 5 + 3 is -10 + 3, which gives -7.
  3. 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;
}

How It Works

  1. The double 0.1 is stored as 0.1000000000000000055..., a tiny bit above one tenth.
  2. x * y rounds to exactly 1 by itself, so the tiny extra part is lost. Adding -1 then gives 0.
  3. 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.

C++
#include <cmath>
#include <iostream>

double poly(double x) {
    double r = std::fma(3.0, x, 2.0);   // 3x + 2
    return std::fma(r, x, 1.0);         // (3x + 2)x + 1
}

int main() {
    for (double x : {2.0, 0.5, -1.0}) {
        std::cout << "p(" << x << ") = " << poly(x) << '\n';
    }
    return 0;
}

How It Works

  1. The first fma() turns 3x + 2 into one value r. The second one multiplies it by x and adds 1.
  2. 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.
  3. 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.

Next: fmax()

Keep going through the <cmath> functions. The larger of two numbers, ignoring NaN.

fmax() 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.

10 people found this page helpful