CKTIS / C
fmax — C23 mathematical function
`fmax(x, y)` from `<math.h>` returns the greater numeric value of two `double` arguments; `fmaxf` and `fmaxl` serve the other types. Its numeric contract is useful when one argument can be NaN: if exactly one is NaN, the numeric value is returned.
Version: C23
Syntax
double fmax(double x, double y);fmax(x, y) from <math.h> returns the greater numeric value of two double arguments; fmaxf and fmaxl serve the other types. Its numeric contract is useful when one argument can be NaN: if exactly one is NaN, the numeric value is returned.
The example evaluates fmax(-3.0, 2.0), fmax(NAN, 5.0), and the sign of fmax(-0.0, +0.0). Output 2 5 0 demonstrates the maximum, suppression of a single NaN, and the positive zero required by Annex F.
When both arguments are NaN, the result is NaN. Do not treat x > y ? x : y as a complete replacement: it handles NaNs and signed zeros differently and can evaluate expressions differently. The function reports no range error; qualify exact signed-zero behavior as an Annex F requirement.
Examples
#include <math.h>
#include <stdio.h>
int main(void) { volatile double nz=-0.0, pz=0.0; printf("%.0f %.0f %d\n", fmax(-3.0,2.0), fmax(NAN,5.0), signbit(fmax(nz,pz)) != 0); return 0; }
Result
2 5 0
The example evaluates `fmax(-3.0, 2.0)`, `fmax(NAN, 5.0)`, and the sign of `fmax(-0.0, +0.0)`. Output `2 5 0` demonstrates the maximum, suppression of a single NaN, and the positive zero required by Annex F.