C fminimum_mag_num() Function

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

Definition

In C, fminimum_mag_num() returns the argument with the smaller magnitude. fminimum_mag_num(-9.0, 3.0) returns 3.0. Only absolute size decides the winner.

If one argument is NaN and the other is a number, you get the number. NaN appears only when both are NaN. This is C23-style math in <math.h> — a newer C library may be required.

Syntax

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

C
fminimum_mag_num(-9.0, 3.0);
CallWhat it does
fminimum_mag_num(-9.0, 3.0)Smaller magnitude is 3, so returns 3.0.
fminimum_mag_num(NAN, -1.5)Prefers the number and returns -1.5.

Include <math.h>. On GCC, link with -lm. Needs C23 support. Stricter sibling: fminimum_mag() returns NaN if either input is NaN.

Library declarations
C
double fminimum_mag_num(double x, double y);
float fminimum_mag_numf(float x, float y);
long double fminimum_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 with smaller absolute value. If one is NaN, returns the other number. NaN only if both are NaN.

C Examples

Example 1 — Closer to Zero

Choose the gentler signed value by magnitude.

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

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

How It Works

  1. Include math.h. Declares fminimum_mag_num.
  2. First call. 3 has the smaller size.
  3. Second call. |-2| beats |6|.
  4. Sign kept. The returned value keeps the winner’s sign.

Example 2 — Number Beats NaN

A missing value does not erase the real sample.

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

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

How It Works

  1. NaN first. Returns -1.5.
  2. NaN second. Returns 4.0.
  3. Both NaN. Only then is the result NaN.
  4. Why use it. Safer than fminimum_mag when data may be incomplete.

Example 3 — Keep the Soft Reading

Between two signed readings, keep the smaller swing.

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

int main(void) {
    double primary = -11.0;
    double backup = 3.5;
    double soft = fminimum_mag_num(primary, backup);

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

How It Works

  1. Two readings. Primary swings more.
  2. Magnitude compare. 3.5 is closer to zero.
  3. Return backup. Result is 3.5.
  4. Pattern. Useful for soft-limiting without forcing absolute value.

Frequently Asked Questions

It returns the argument with the smaller absolute value. If one is NaN, it returns the number.
Both x and y are required.
The closer-to-zero argument by magnitude. NaN only if both inputs are NaN.
Include <math.h>. Link with -lm on GCC. Needs a newer C library for C23.
fminimum_mag() returns NaN if either argument is NaN. fminimum_mag_num() prefers the non-NaN number.

Next: fminimum_mag()

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

fminimum_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