C++ fma()

Last Updated On Friday 15th Apr 2022

The fma() function takes three arguments x, y and z, and returns x*y+z without losing precision. It is defined in <cmath> header file.

Syntax

	fma(double x, double y, double z)
	

Parameters

The fma() takes three arguments.

  • x – The first argument to be multiplied.
  • y – The second argument to be multiplied with x.
  • z – The third argument to be added to the product of x and y.

Return value

Returns x*y+z as if calculated to infinite precision and rounded once to fit the result type.

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

	double fma(double x, double y, double z);
float fma(float x, float y, float z);
long double fma(long double x, long double y, long double z); 
Promoted fma(Type1 x, Type2 y, Type z);
	

Example : C++ fma()

	#include <stdio.h>
#include <math.h>

int main () {
  double x,y,z,result;
  x = 11.0, y = 22.0, z = 33.0;
#ifdef FP_FAST_FMA
  result = fma(x,y,z);
#else
  result = x*y+z;
#endif
  printf ("11.0 * 22.0 + 33.0 = %f\n", result);
  return 0;
}
	

Output

	11.0 * 22.0 + 33.0 = 275.000000