C++ Math fma()

Last Updated : 22 May 2026

Math fma()Function

In C++, the fma() function is defined in the <cmath> or <math.h> header file and is used to compute the expression x * y + z as a single operation without losing precision in intermediate calculations. It performs multiplication and addition together with better accuracy compared to separate operations.

The fma() function supports float, double, and long double values.

Suppose numbers are x,y and z

Syntax

It has the following syntax.

Note: If any argument is long double type, then the return type is promoted to long double. If not, then the return type is promoted to double.

Parameter

x: The value which is to be multiplied.

y: The value which is to be multiplied with x.

z: The value which is to be added with the product of x and y.

Return value

It returns the result of x*y+z.

Example 1: Calculate x * y + z Using Integer Values

This example demonstrates how the fma() function computes the expression x * y + z using integer values.

Output:

Values of x,y,z are :2,3,4
fma(x,y,z) : 10   

Explanation:

In this example, fma() function computes the result of x*y+z and returns the value 10.

Example 2: Calculate x * y + z Using Floating-Point Values

This example demonstrates how the fma() function works with floating-point values for more precise calculations.

Output:

Values of x, y, z are : 2.5, 4.2, 1.3
fma(x, y, z) : 11.8  

Example 3: Use fma() with Negative Values

This example demonstrates how the fma() function behaves when one or more input values are negative.

Output:

Values of x, y, z are : -3, 5, 2
fma(x, y, z) : -13