C++ cmath fma , fmaf and fmal function


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);

The C++ <cmath> 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.

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

cout<< “fma(2.1342 , 89.9836 , 5.6)=” << fma(2.1342 , 89.9836 , 2.45236) << endl ;

/*Passing overflow value */
cout<< “fma(2.3e400 , 9 , 5.6)=” << fma(2.3e400 , 9 , 5.6) << endl;

/*Passing ∞ */
cout<< “fma(INFINITY , 9 , 5.6)=” << fma(INFINITY , 9 , 5.6) << endl;

/*Passing NaN */
cout<< “fma( 12 , 7.8 , NAN)=” << fma(12 , 7.8 , NAN) << endl ;

Output

fma(2.1342 , 89.9836 , 5.6)= 194.495
fma(2.3e400 , 9 , 5.6)= inf
fma(INFINITY , 9 , 5.6 )= inf
fma(12 , 7.8 , NAN)= nan





fmaf and fmal

i)The 4th version fmaf is same as the 1st version float fma(float , float) .The last character ‘f‘ in ‘fmaf’ stands for ‘float’ which signify the argument type and return type of the function.

ii)The 5th version fmal is same as the 3rd version long double fma(long double , long double).The last character ‘l‘ in ‘fmal’ stands for ‘long double’ which signify the argument type and return type of the function.

Code example

#include <typeinfo>

/*fmaf */
cout<< “*fmaf\n”;
float f=2 , f1=99;
cout<< typeid( fmaf(2 , 99) ).name() << endl /*identify type of fmaf returned value */
<< typeid( fma(f , f1) ).name() << endl; /*identify type of fma(float , float) returned value */

/*fmal*/
cout<< “*fmal\n”;
long double ld=2 , ld1=89;
cout<< typeid( fmal(2 , 89) ).name() << endl /*identify type of fmal returned value */
<< typeid( fma(ld , ld1) ).name() << endl; /*identify type of fma(long double , long double) returned value*/

Output

*fmaf
f
f
*fmal
e (means ‘long double’)
e (means ‘long double’)

You can use ‘fmaf’ and ‘fmal’ instead of ‘fma’,they are all the same,except they accept different type.


*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 fma,fmaf and fmal math.h