What is fmaf in C?

The fmaf function is defined in the <math.h> header file in C. It takes in three parameters of type float: x, y, and z. It then computes (xy)+z(x*y) + z and returns the resultant float value.

The illustration below shows how the fmaf function works:

How does fmaf work?

Declaration

The fmaf function is defined as follows:

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

Parameters

The fmaf function takes in three parameters of type float. The first two parameters, x and y, are multiplied together. Their product is then added to the third parameter, z.

Return value

The fmaf function returns the result of the computation of type float.

Error handling

The fmaf function returns special values for certain arguments:

x y z Returned Value
0 INFINITY Not NaN NaN
INFINITY 0 Not NaN NAN
INFINITY 0 NaN NaN
0 INFINITY NaN NaN
NaN valid float valid float NaN
valid float NaN valid float NaN
valid float valid float NaN NaN

Example

The following code snippet shows how we can use the fmaf function:

#include <stdio.h> // include header for printf
#include <math.h> // include header for fmaf
int main(){
// setting values fpr x, y, and z
float x = 2.0;
float y = 4.0;
float z = 5.0;
float result;
result = fmaf(x, y, z);
printf("The result is: %f", result);
return 0;
}

The following code snippet shows how error handling in the fmaf function works:

#include <stdio.h> // include header for printf
#include <math.h> // include header for fmaf
int main(){
float x = INFINITY; // setting x to INFINITY
float y = 0.0;
float z = 5.0;
float result;
result = fmaf(x, y, z);
printf("The result is: %f", result);
return 0;
}
Copyright ©2024 Educative, Inc. All rights reserved