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
It has the following syntax.
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.
It returns the result of x*y+z.
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.
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
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
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India