fma() Function in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

The <cmath> header file defines the fma() function in C++. It takes three arguments and returns a value of (x * y) + z as it follows the Fused multiply-add approach without losing the precision.

What is fma() Function in C++?

The Floating-point multiply-accumulate(FMA-C) or simply the Fused multiply-add function computes the product of two integers and adds it to an accumulator; it is particularly used in computing processes like digital signal processing. In C++ Language, the <cmath> header file provides the ability to use the fma-c function. This method calculates the result without losing the precision in the process. Some important points associated with the fma-c function are given below:

  • It can take three arguments and returns a value of (first_argument * second_argument) + third_argument.

  • It follows the Fused multiply-add approach without losing the precision in the process.

  • fma-c function computes the value of (x * y) + z faster than using this explicit operation in a C++ program.

  • Since the C++11 version, the return type of fma-c is promoted to long double; if any of the arguments are long double else, the return type is double or `float as per the arguments.

Syntax of fma() Function in C++

To call a fma-c function in a C++ program, we need to pass three arguments:

Example:

fma() function is declared in <cmath> header file as:

  • For float values:
  • For double values:
  • For long double values:

Parameters of fma() Function in C++

fma(x, y, z) contains three parameters in its definition as follows:

  1. x (First Parameter), y (Second Parameter): These parameters are multiplied by each other: (x * y).
  2. z (Third Parameter): This parameter is added to the product of the first two parameters:
    (x * y) + z.

Return Value of fma() Function in C++

When the fma-c function is used in a C++ program, it returns the sum of the product of the first two arguments and the third argument.

Return Value: (first_argument * second_argument) + third_argument

Return Type: float or double or long double (as per the arguments type)

Example

C++ Program:

Check and run this program using InterviewBit IDE.

Output:

Explanation:

First we declared and initialized three different double variables x, y & z and then we have printed the fma(x, y, z) return value and the sizeof() of the return data type in the output. fma() return value (3.4 * 5.6) + 9.8 comes out to be 28.84 and return type comes out to be double type i.e. with size 8 bytes.

fma() Prototype [As of C++11 standard]

In C++11 standards, we have got an additional overload of the fma-c function definition in the <cmath> header file in which we can pass any type of data in the arguments of a fma() function. It was created using the concept of templates.

Syntax:

As per the C++11 standards, the return type of fma-c is promoted to long double, if any of the arguments are long double. If this is not the case, then the return type is double or float as per the arguments.

Error Handling

Errors in the case of the fma-c function occur due to the invalid arguments passed in the function, and these errors are specified by the math_errhandling macro in the <cmath> header file.

  • If any of the first and second arguments passed in fma-c is in the combination of zero and infinite and the third argument is NaN (Not a Number), then FE_INVALID is raised, and NaN is returned.
  • NaN is returned if either x or y is NaN.
  • NaN is returned without the FE_INVALID, if third argument is a NaN and (first * second is neither (0 * ∞) or (∞ * 0).
  • If (first * second) is and third is -∞ (infinity with opposite sign), then FE_INVALID is raised, and NaN is returned.

Examples for fma() Function

1. float

C++ Program:

Check and run this program using InterviewBit IDE.

Output:

As we give float arguments to fma() it return a float value that is why typeid() return f.

2. double

C++ Program:

Check and run this program using InterviewBit IDE.

Output:

As we give double arguments to fma() it returns a double value which is why typeid() return d.

3. Long double

C++ Program:

Check and run this program using InterviewBit IDE.

Output:

As we give long double arguments to fma() it return a long double value that is why typeid() return e.

Conclusion

  • We need to include the <cmath> header file in the C++ program to use the fma() function.
  • fma-c calculates the result much faster than the explicit calculation.
  • It follows the Floating-point multiply-add approach to calculate the result.
  • fma-c takes three arguments and returns the value: (first_parameter * second_parameter) + third_parameter
  • The return type of fma-c is long double, if any of the arguments are long double else the return type is double or float as per the arguments.