fma() function in C++


Given the task is to show the working of fma() function in C++. In this article we will look into what parameters this function need and what results it will be returning.

fma() is an inbuilt function of cmath header file, which accepts three parameters x, y and z and return the result x*y+z without losing the precision in any intermediate result.

Syntax

float fma(float x, float y, float z);

Or

double fma(double x, double y, double z);

Or

long double fma(long double x, long double y, long double z);

Parameters

  • x − The first element to be multiplied.

  • y − The second element with which x is to be multiplied.

  • z − Third element which will be added to the result of x and y.

Return value

The function returns the exact result of x*y+z.

Example

 Live Demo

#include<iostream>
#include<cmath>
using namespace std;
int main() {
   double x = 2.1, y = 4.2, z = 9.4, answer;
   answer = fma(x, y, z);
   cout << x << " * " << y << " + " << z << " = " << answer << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

2.1 * 4.2 + 9.4 = 18.22

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std; int main() {
   double a = 7.4, b = 9.3, c = 1.2;
   double ans = fma(a, b, c);
   cout << a << " * " << b << " + " << c << " = " << ans << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

7.4 * 9.3 + 1.2 = 70.02

Updated on: 28-Feb-2020

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements