What is fmax() in C?

fmax() is a C library function that returns the larger numeric value of its two arguments.

Library

#include<math.h>

Declaration

Following is the declaration of fmax() where x and y are the floating-point values to be compared:

double fmax(double x, double y);

Note: Integer(s) can also be passed as a parameter(s) to fmax(), but will be implicitly cast to double.

Return Value

The function returns the larger of the two numbers as double datatype.

  • If one argument is NANnot a number, the other one is returned.
  • If both arguments are NAN, NAN is returned.

Quiz

Q

What will fmax(-55.32, -55.321) return?

A)

-55.32

B)

-55.321

C)

-55.320000

D)

-55.321000

Code

The below code runs the fmax() function on different example cases:

#include<stdio.h>
#include<math.h>
int main() {
printf("fmax(3, 4) = %f\n", fmax(3, 4)); //find max of 3 and 4
printf("fmax(9, 7.8) = %f\n", fmax(9, 7.8)); //find max of 9 and 7.8
printf("fmax(20.3, 20.4) = %f\n", fmax(20.3, 20.4)); //find max of 20.3 and 20.4
printf("fmax(500.5, 500.5) = %f\n", fmax(500.5, 500.5)); //find max of 500.5 and 500.5
printf("fmax(1.2, nan) = %f\n", fmax(1.2, NAN)); //find max of 1.2 and nan
printf("fmax(nan, 1000.2) = %f\n", fmax(NAN, 1000.2)); //find max of nan and 1000.2
printf("fmax(nan, nan) = %f\n", fmax(NAN, NAN)); //find max of nan and nan
printf("fmax(-inf, nan) = %f\n", fmax(-INFINITY, NAN)); //find max of -inf and nan
printf("fmax(inf, nan) = %f\n", fmax(INFINITY, NAN)); //find max of inf and nan
return 0;
}
Copyright ©2024 Educative, Inc. All rights reserved