C fma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, fma() computes (x * y) + z as one fused multiply-add. The multiply and add are done with a single rounding step.

You pass three double values. The result is a double. Include <math.h>. On GCC, link with -lm.

Syntax

Pass the two factors, then the addend. Start with this call.

C
fma(2.0, 3.0, 4.0);
CallWhat it does
fma(2.0, 3.0, 4.0)Returns 10, because (2 × 3) + 4 = 10.
fma(1.5, 2.0, -0.5)Returns 2.5, because 3.0 − 0.5 = 2.5.

Include <math.h>. On GCC, link with -lm. Fused form can be more accurate than writing x * y + z yourself.

Library declarations
C
double fma(double x, double y, double z);
float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z);

Parameters

ParameterDescription
xRequired. First factor of the product.
yRequired. Second factor of the product.
zRequired. Value added after the product.

Return Value

Return ValueDescription
double(x * y) + z computed with a fused multiply-add.

C Examples

Example 1 — Basic Fused Multiply-Add

Compute a simple (x * y) + z with fma.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", fma(2.0, 3.0, 4.0));
    printf("%.1f\n", fma(1.5, 2.0, -0.5));
    return 0;
}

How It Works

  1. Include math.h. That declares fma.
  2. First call. (2 × 3) + 4 = 10.
  3. Second call. (1.5 × 2) + (−0.5) = 2.5.
  4. Three inputs. Product first, then add z.

Example 2 — Running Dot-Style Total

Accumulate a short product sum with fma.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double acc = 0.0;
    acc = fma(1.0, 2.0, acc);
    acc = fma(3.0, 4.0, acc);
    acc = fma(0.5, 6.0, acc);

    printf("acc = %.1f\n", acc);
    return 0;
}

How It Works

  1. Start at zero. Accumulator begins empty.
  2. Add 1×2. acc becomes 2.
  3. Add 3×4. acc becomes 14.
  4. Add 0.5×6. acc becomes 17.

Example 3 — Compare with Plain Expression

For many simple values the results match. fma still documents intent.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double x = 1.25;
    double y = 2.5;
    double z = 0.125;

    printf("plain = %.6f\n", x * y + z);
    printf("fma   = %.6f\n", fma(x, y, z));
    return 0;
}

How It Works

  1. Pick simple numbers. Easy to check by hand.
  2. Plain expression. Multiply then add with two roundings.
  3. fma expression. Same math idea, fused rounding.
  4. Here they match. Harder cases may differ slightly.

Frequently Asked Questions

fma(x, y, z) computes (x * y) + z as a fused multiply-add.
x and y are the factors. z is added after the product. All are double.
A double equal to (x * y) + z with fused rounding.
Include <math.h>. On GCC, link with -lm.
fma rounds once. The plain expression may round twice, which can lose a little accuracy.

Next: fmax()

Return the larger of two floating-point values.

fmax() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful