fma() function in C++

In this tutorial, we will learn about fma() function in C++. This is one of the popular functions among the Mathematical functions in C++.

fma stands for Fused Multiply-Add. This function takes three parameters and performs multiplication and addition on them. It basically multiplies the first two parameters and adds the third parameter to the product. The function is mostly used because of its high precision.

Syntax of fma() function

The syntax of the function is :

fma(a,b,c);

and the header file for this is :

#include <math.h>

Here, the parameters a,b,c are either all float or double or long double. The return type of the function is then float, double and long double respectively.

The operation performed by the fma function on its parameters a, b, c is :

a*b + c

Now, we write a program to show the use of fma() function.

C++ Program: Example of fma() function

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
  float p, q, r, t;
  cout << "Enter p,q,r : ";
  cin >> p >> q >> r;      //entering the values
  
  t = fma(p, q, r);           //performing the function
  
  cout << "Result of fma(a,b,c) = " << t;    //output
  return 0;
}

Output :

Enter p,q,r :
2.3
3.4
4.5
Result of fma(a,b,c) = 12.32

In this program, we enter three floating type values. these values are then passed as parameter to the fma() function. Then the function gives a result which we print as output.

Hope this was helpful. Enjoy Coding!

Also learn :

sinh() function in C++

Multithreading in C++

Leave a Reply

Your email address will not be published. Required fields are marked *