C fmaximum_num() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math.h

Definition

In C, fmaximum_num() returns the larger of two double values by numeric order. fmaximum_num(2.0, 9.0) is 9.0. Positive zero is treated as greater than negative zero.

If exactly one argument is NaN, the function returns the other number. You get NaN only when both are NaN. This belongs to the newer C23 math family in <math.h>. Your toolchain may need a newer C library before the name links.

Syntax

Pass two floating-point numbers. Start with this call.

C
fmaximum_num(2.0, 9.0);
CallWhat it does
fmaximum_num(2.0, 9.0)Returns 9.0, the larger value.
fmaximum_num(NAN, 4.0)Prefers the number and returns 4.0.

Include <math.h>. On GCC, link with -lm. Needs C23-era support. For the classic “prefer non-NaN max” from older C, see fmax().

Library declarations
C
double fmaximum_num(double x, double y);
float fmaximum_numf(float x, float y);
long double fmaximum_numl(long double x, long double y);

Parameters

ParameterDescription
xRequired. First floating-point value.
yRequired. Second floating-point value.

Return Value

Return ValueDescription
doubleThe larger argument. If one is NaN, returns the other. NaN only if both are NaN.

C Examples

Example 1 — Pick the Larger Value

Simple numeric maximum of two doubles.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", fmaximum_num(2.0, 9.0));
    printf("%.1f\n", fmaximum_num(-3.0, -8.0));
    return 0;
}

How It Works

  1. Include math.h. Declares fmaximum_num.
  2. Positive pair. 9 is larger than 2.
  3. Negative pair. -3 is larger than -8 on the number line.
  4. Result type. Return value is still double.

Example 2 — Number Beats NaN

A quiet NaN does not wipe out a real reading.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    printf("%.1f\n", fmaximum_num(NAN, 6.5));
    printf("%.1f\n", fmaximum_num(1.0, NAN));
    return 0;
}

How It Works

  1. NaN first. The second argument 6.5 is returned.
  2. NaN second. The first argument 1.0 is returned.
  3. Both NaN. Only that case yields NaN.
  4. Practical tip. Good when sensor data may be missing but you still want a max.

Example 3 — Raise a Value to a Floor

Use max to push a reading up to at least a minimum threshold.

C
#include <stdio.h>
#include <math.h>

int main(void) {
    double reading = 3.2;
    double floor_v = 5.0;
    double raised = fmaximum_num(reading, floor_v);

    printf("raised = %.1f\n", raised);
    return 0;
}

How It Works

  1. Reading is low. 3.2 is below the floor.
  2. Compare. fmaximum_num picks 5.0.
  3. If already high. A reading above the floor would stay unchanged.
  4. Pattern. Same idea as max(value, floor) in many languages.

Frequently Asked Questions

It returns the larger of two floating-point values. If one is NaN, it returns the number.
Both x and y are required.
The larger double, or the non-NaN argument when exactly one is NaN.
Include <math.h>. Link with -lm on GCC. Needs a newer C library for C23 math.
fmaximum() returns NaN if either argument is NaN. fmaximum_num() prefers the numeric argument.

Next: fmaximum()

Return the smaller of two floating-point values.

fmaximum() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful