Javatpoint Logo
Javatpoint Logo

fma() Function in C++

In this article, we will discuss the fma() function in C++ with its syntax, parameters, and examples.

fma() Function in C++

Introduction:

The C function fma() is designed to perform a merged multiplication operation that computes (x * y) + z as a single combined operation, reducing rounding errors that can occur when multiplication and addition are performed separately. This feature is particularly useful in numerical applications where accuracy is critical.

Syntax:

It has the following syntax:

Parameters:

It has the following parameters:

x: multiplier.

y: multiplier.

z: addition.

Return value: The result of the merged multiplication operation: (x * y) + z.

It follows the Fused-multiply-add approach without losing process precision. The fma-c function calculates the value of (x * y) + z faster than using this explicit function in a C++ program. Since C++11, the return type of fma-c has been changed to long double; if one of the arguments is a long double, the return type is double or float, depending on the arguments.

Fma () Prototype [As of C++11 standard]:

It means that FMA is overloaded for both float and double types. The function takes three arguments (x, y, and z) and operates (x * y) + z with greater precision and without the rounding errors that would occur if the multiplication and addition were done separately. If we need to operate on integers, we may need to convert them to floating point types before using the fma function. Note that fma is a relatively low-level function and may not be needed in all cases. In many situations, ordinary multiplication and addition operations are sufficient and can be optimized appropriately by the compiler. The use of Fma is usually more critical in numerical applications where accuracy is critical.

Error Handling in fma () Prototype:

The fma() function itself usually does not include special error-handling functions. Its main purpose is to provide a merged multiplication function without intermediate rounding. However, when the fma() function is used in a broader application, consideration should be given to handling possible floating-point exceptions or special case errors. Here are some general considerations for error handling when using fma or other floating point functions.

  • Floating point exceptions:

Floating-point exceptions such as overflow, underflow, division by zero, and invalid operation can be raised during mathematical operations. In C++, we can specify how these exceptions are handled using the std::fetestexcept and std::feclearexcept functions <cfenv> title.

Example:

Let us take an example to illustrate the use of the fma() function in C++.

Output:

Overflow occurred!
  • Special cases:

Consider special cases such as manipulation of NaN (not a number) or Inf (infinity) that may result from certain operations.

Example:

Output:

Result is NaN!
  • Checking input values:

It is good practice to check the validity of input values before using fma function to avoid unexpected behavior.

Example:

Program:

Let us take an example to illustrate the use of the fma() function in C++.

Output:

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

Explanation:

#include<iostream>:

This line contains the I/O stream library needed to perform I/O operations.

#include <math>:

This line contains the math library used for math operations. The fma (fused multiply-add) function is part of the math library.

using namespace std:

This line indicates that the program uses elements from namespace std (standard). It allows standard C++ functions to be used without specifying a namespace each time.

int main () {/* ... */ }:

This is the main function that starts the execution of the program.

int x = 2;

int y = 3;

int z = 4; These lines declare and initialize three integer variables x, y and z with the values 2, 3, and 4, respectively.

The x, y, and z values are " x" and "y" and "z" std::endl;: This line prints the x, y, and z values to the console.

cout "fma (x, y, z): " fma(x, y, z);:

This line calculates the product of the sum of x, y, and z using the fma function and prints the result to the console.

return 0;

It indicates that the program ran successfully and returns 0 to the operating system. The main purpose of this code is to illustrate the use of the fma function, which performs merged multiplication. After that, the result is printed to the console along with the initial values of x, y, and z.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA