Python math.fmin() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Recent Python

Definition

math.fmin(x, y) returns the smaller of x and y as a float.

Just like math.fmax(), it ignores a single nan argument and returns the other non-NaN value as a float (returning nan only if both inputs are nan). Consult the official docs for signed-zero tie-breaking details.

math.fmin() is a function in Python's standard math module: import it with import math and call math.fmin(...), or import the name directly with from math import fmin. New in recent Python versions (3.15 at the time of writing; check your version with import sys; print(sys.version)).

Syntax

Import math (guard with hasattr(math, "fmin") on older Python versions).

python
import math

lo = math.fmin(x, y)
CallWhat it does
math.fmin(3, 7)Returns 3.0 (float).
math.fmin(float("nan"), 2.5)Returns 2.5 (ignores single NaN).
math.fmin(-8, float("nan"))Returns -8.0.
math.fmin(float("nan"), float("nan"))Returns nan.

On Python versions without math.fmin(), return float(y) if math.isnan(x), float(x) if math.isnan(y), else float(x) if x <= y else float(y).

Function signatures
python
math.fmin(x, y, /)
# recent Python addition (3.15 at writing); returns float minimum ignoring single NaN

Parameters

ParameterDescription
xRequired positional real number (int or float).
yRequired positional real number (int or float).

Return Value

Return ValueDescription
floatSmaller of x and y (or the non-NaN argument when only one is NaN).

Python Examples

Example 1 — Compare NaN Handling Between fmin() and Built-in min()

Contrast NaN tolerance in a hasattr wrapper against built-in min().

python
import math


def safe_fmin(x, y):
    if hasattr(math, "fmin"):
        return math.fmin(x, y)
    if math.isnan(x):
        return float(y)
    if math.isnan(y):
        return float(x)
    return float(x) if x <= y else float(y)


print("fmin(nan, 2.5):", safe_fmin(math.nan, 2.5))
print("min(nan, 2.5) :", min(math.nan, 2.5))
print("both nan      :", math.isnan(safe_fmin(math.nan, math.nan)))

How It Works

  1. Treats single NaN as absent. Passing nan in either slot returns valid float 2.5.
  2. Built-in min keeps first NaN. Standard min(nan, 2.5) evaluates 2.5 < nan as False and returns nan.
  3. Consistent fallback. Testing NaNs first reproduces IEEE minimum semantics cleanly.

Example 2 — Track Overnight Minimum Temperatures With Sensor Outages

Combine readings from two weather probes where missing entries appear as nan.

python
import math

fmin_fn = getattr(
    math,
    "fmin",
    lambda x, y: (
        float(y) if math.isnan(x)
        else (float(x) if math.isnan(y) or x <= y else float(y))
    ),
)

probe_east = [-2.0, math.nan, 4.5]
probe_west = [1.5, -3.5, math.nan]
lows = [fmin_fn(e, w) for e, w in zip(probe_east, probe_west)]
print("overnight lows:", lows)

How It Works

  1. Outage resilience. Each morning gets a valid low reading as long as at least one probe stayed online.
  2. Negative temperatures compared. -2.0 beats 1.5 cleanly, and valid -3.5 survives an east-probe NaN.
  3. Portable getattr lookup. Works reliably across older and newer Python runtimes.

Example 3 — Detect math.fmin() Availability at Runtime

Print explicit outputs for Python versions with math.fmin() and older fallback code.

python
import math

if hasattr(math, "fmin"):
    print("math.fmin:", math.fmin(3, 7), math.fmin(-8, math.nan))
else:
    b = -8.0 if math.isnan(math.nan) else math.nan
    print("fallback:", 3.0, b)

How It Works

  1. Guarded native call. Calls math.fmin() when available and avoids AttributeError on older releases.
  2. Promotes ints to float. Inputs 3, 7 produce 3.0 and -8 becomes -8.0.
  3. Version verification note. Check import sys; print(sys.version) when testing recent math additions.

Frequently Asked Questions

It is new in recent Python versions (3.15 at the time of writing); check your version with import sys; print(sys.version) or hasattr(math, "fmin").
It ignores the NaN and returns the other non-NaN argument as a float.
It returns float("nan").
Built-in min() accepts any number of arguments or iterables and preserves ints, whereas math.fmin(x, y) takes two numbers, always returns float, and ignores a single NaN.
Return float(y) if math.isnan(x), float(x) if math.isnan(y), else float(x) if x <= y else float(y).

Next: math.fmod()

Return floating-point remainder keeping the sign of x.

fmod() →

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.

15 people found this page helpful