C programming fma , fmaf and fmal math.h


1 float fma(float x , float y , float z);
2 double fma(double x , double y , double z);
3 long double fma(long double x , long double y , long double z);
4 float fmaf(float x , float y , float z);
5 long double fmal(long double x , long double y , long double z);

In C programming the <math.h> fma,fmaf and fmal function compute the value of (x * y)+z .The declaration of the functions are given below.

Parameters:
x -The first value.

y -The second value.

z -The third value.

Return type
floating point -Returns the computed value of (x*y)+z.

All the functions are the same,they compute “(x * y)+z”.The 1st,2nd and the 3rd function have the same name i.e.’fma’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some points to note:

 i) The resultant value of the expression (x*y)+z,is rounded as one ternary operation.

 ii) A range error may occur if one of the arguments is overflow.If the argument provided is underflow the returned value is 0.

 iii) If one of the arguments is ±∞ ,the returned value is ±∞.

 iv) If one of the arguments is NaN ,the function returned NaN.

Code example

double dx = 10 , dy = 11 , dz=4 ,
x1 = 12e30 , x2=10.0 , x3=8 ;

float fx = 10 , fy = 11 , fz=4 ;

long double ldx = 10 , ldy = 11 , ldz=4 ;

printf(“%lf”, fma( dx , dy , dz) ) ; //calls the 2nd version
printf(“\n%f”, fma( fx , fy , fz) ) ; //calls the 1st version
printf(“\n%Lf”, fma( ldx , ldy , ldz) ) ;//calls the 3rd version

printf(“\n%lf”, fma( x1 , x2 , x3) ) ;
printf(“\n%lf”, fma( NAN , 90 , INFINITY) ) ;

Output in Visual Studio ,

114.000000
114.000000
114.000000
119999999999999995630755539582976.000000
-nan(ind)

The first argument of the 5th function call is NaN so ‘-nan(ind)'(NaN) is returned.






fmaf

The ‘fmaf‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘fmaf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x = 30 , y=3 , z=3 ;

printf(“%f”, fma( x , y , z) ); //Calls the 1st version
printf(“\n%f”, fmaf( 30 , 3 , 3) ); Calls the 4th version

Output in Visual Studio,

93.000000
93.000000


fmal

The ‘fmal‘ function is same as the 1st function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘fmal‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x = 30 , y=3 , z=3 ;

printf(“%Lf”, fma(x , y) ); //Calls the 3rd version
printf(“\n%Lf”, fmal(30 , 3 , 3) ); Calls the 5th version

Output in Visual Studio,

93.000000
93.000000


*Side Note

Some facts to note about fma function:

 i) fma(x , y , z) returns a NaN and optionally raises the ‘in valid’ floating-point
exception if one of x and y is infinite, the other is zero, and z is a NaN.

 ii) fma(x , y , z) returns a NaN and raises the ‘in valid’ floating-point exception if one of x and y is infinite, the other is zero, and z is not a NaN.

 iii) fma(x , y , z) returns a NaN and raises the ‘in valid’ floating-point exception if x times y is an exact infinity and z is also an infinity but with the opposite sign.


Related links

->C programming fmax,fmaxf and fmaxl math.h

->C programming fdim,fdimf and fdiml math.h