C fminimum_mag() Function

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

Definition

In C, fminimum_mag() returns the argument with the smaller magnitude. Sign is ignored for the comparison. fminimum_mag(-9.0, 3.0) returns 3.0 because 3 is closer to zero than 9.

If either argument is NaN, the result is NaN. This is a C23-style function in <math.h>. An older compiler or math library may not have it — you may need a newer C library.

Syntax

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

C
fminimum_mag(-9.0, 3.0);
CallWhat it does
fminimum_mag(-9.0, 3.0)Smaller magnitude is 3, so returns 3.0.
fminimum_mag(2.0, NAN)Returns NaN because one argument is NaN.

Include <math.h>. On GCC, link with -lm. Needs C23 support. Use fminimum_mag_num() if you want to keep a number when the other side is NaN.

Library declarations
C
double fminimum_mag(double x, double y);
float fminimum_magf(float x, float y);
long double fminimum_magl(long double x, long double y);

Parameters

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

Return Value

Return ValueDescription
doubleThe argument with smaller absolute value, or NaN if either argument is NaN.

C Examples

Example 1 — Smaller Magnitude Wins

Pick the value nearer to zero, keeping its sign.

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

int main(void) {
    printf("%.1f\n", fminimum_mag(-9.0, 3.0));
    printf("%.1f\n", fminimum_mag(-1.0, -0.5));
    return 0;
}

How It Works

  1. Include math.h. Declares fminimum_mag.
  2. First pair. |3| is smaller than |-9|.
  3. Second pair. |-0.5| beats |-1|.
  4. Sign kept. The returned value keeps the original sign of the winner.

Example 2 — NaN Propagates

One NaN makes the magnitude pick fail closed.

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

int main(void) {
    double r = fminimum_mag(-2.0, NAN);

    if (isnan(r)) {
        printf("result is NaN\n");
    }
    return 0;
}

How It Works

  1. Mixed inputs. A number and a NaN.
  2. Strict rule. fminimum_mag returns NaN.
  3. Check with isnan. Confirm before you use the value.
  4. Softer option. fminimum_mag_num would return -2.0 instead.

Example 3 — Keep the Quieter Sample

Between two signed samples, keep the one with less energy.

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

int main(void) {
    double a = -0.8;
    double b = 2.5;
    double quiet = fminimum_mag(a, b);

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

How It Works

  1. Samples. -0.8 and 2.5.
  2. Compare |x|. 0.8 is smaller than 2.5.
  3. Return a. Result is -0.8.
  4. Use case. Useful when you want the gentler reading, not the algebraically smaller one.

Frequently Asked Questions

It returns the argument with the smaller absolute value. If either is NaN, the result is NaN.
Both x and y are required.
The value closer to zero (by magnitude), or NaN if either argument is NaN.
Include <math.h>. Link with -lm on GCC. Needs a newer C library for C23.
fminimum() compares signed values. fminimum_mag() compares absolute magnitudes.

Next: fminimum_num()

Smaller magnitude, but prefer a number over NaN.

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