Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What is fmaf in C?

Hassaan Waqar

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

RELATED TAGS

c

CONTRIBUTOR

Hassaan Waqar
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring