C fmaximum_mag_num() Function

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

Definition

In C, fmaximum_mag_num() looks at two double values and returns the one with the larger magnitude (size). Sign does not decide the winner — only how far from zero matters. fmaximum_mag_num(-9.0, 3.0) returns -9.0 because 9 is bigger than 3.

If one argument is a quiet NaN and the other is a real number, you get the number back. You get NaN only when both are NaN. This is a C23-style helper in <math.h>. An older compiler or library may not provide it yet — you may need a newer C library.

Syntax

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

C
fmaximum_mag_num(-9.0, 3.0);
CallWhat it does
fmaximum_mag_num(-9.0, 3.0)Larger magnitude is 9, so returns -9.0.
fmaximum_mag_num(2.0, NAN)Prefers the number and returns 2.0.

Include <math.h>. On GCC, link with -lm. Needs a C23-capable math library. Compare magnitudes with fmaximum_mag() if you want NaN when either input is NaN.

Library declarations
C
double fmaximum_mag_num(double x, double y);
float fmaximum_mag_numf(float x, float y);
long double fmaximum_mag_numl(long double x, long double y);

Parameters

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

Return Value

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

C Examples

Example 1 — Larger Magnitude Wins

Ignore the sign. Pick the value farther from zero.

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

int main(void) {
    printf("%.1f\n", fmaximum_mag_num(-9.0, 3.0));
    printf("%.1f\n", fmaximum_mag_num(-1.5, -4.0));
    return 0;
}

How It Works

  1. Include math.h. That declares fmaximum_mag_num.
  2. Compare sizes. |-9| is 9 and |3| is 3, so -9.0 wins.
  3. Both negative. |-4| beats |-1.5|, so the result is -4.0.
  4. Sign kept. The returned value keeps the original sign of the winner.

Example 2 — Prefer a Number over NaN

When one side is NaN, the numeric argument comes through.

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

int main(void) {
    double a = fmaximum_mag_num(5.0, NAN);
    double b = fmaximum_mag_num(NAN, -2.5);

    printf("%.1f\n", a);
    printf("%.1f\n", b);
    return 0;
}

How It Works

  1. First call. 5.0 is a number, so NaN is ignored.
  2. Second call. -2.5 is kept even though the first argument is NaN.
  3. Both NaN. Only then would the result itself be NaN.
  4. Why it helps. Useful when you want a magnitude pick that still tolerates a missing value.

Example 3 — Keep the Stronger Spike

In a simple signal check, keep the reading with the bigger swing.

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

int main(void) {
    double left = -12.0;
    double right = 7.5;
    double spike = fmaximum_mag_num(left, right);

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

How It Works

  1. Two readings. Left is -12, right is 7.5.
  2. Magnitude compare. 12 is larger than 7.5.
  3. Return left. The function returns -12.0, the stronger spike.
  4. Real use. Handy for peak detection when direction (sign) still matters.

Frequently Asked Questions

It returns the argument with the larger absolute value. If one is NaN, it returns the number.
Both x and y are required doubles (or float / long double variants).
The value with larger magnitude. NaN only if both inputs are NaN.
Include <math.h>. On GCC, link with -lm. You also need a newer C library that supports C23 math.
fmaximum_mag() returns NaN if either argument is NaN. fmaximum_mag_num() prefers the non-NaN number.

Next: fmaximum_mag()

Pick the larger value and prefer a real number over NaN.

fmaximum_mag() 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