C fma() Function

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.
| Call | What 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
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
| Parameter | Description |
|---|
| x | Required. First factor of the product. |
| y | Required. Second factor of the product. |
| z | Required. Value added after the product. |
Return Value
| Return Value | Description |
|---|
| 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.
#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
- Include math.h. That declares
fma. - First call. (2 × 3) + 4 = 10.
- Second call. (1.5 × 2) + (−0.5) = 2.5.
- Three inputs. Product first, then add z.
Example 2 — Running Dot-Style Total
Accumulate a short product sum with fma.
#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
- Start at zero. Accumulator begins empty.
- Add 1×2. acc becomes 2.
- Add 3×4. acc becomes 14.
- Add 0.5×6. acc becomes 17.
Example 3 — Compare with Plain Expression
For many simple values the results match. fma still documents intent.
#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;
}
plain = 3.250000
fma = 3.250000
How It Works
- Pick simple numbers. Easy to check by hand.
- Plain expression. Multiply then add with two roundings.
- fma expression. Same math idea, fused rounding.
- 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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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