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)
Call
What 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
Parameter
Description
x
Required positional real number (int or float).
y
Required positional real number (int or float).
Return Value
Return Value
Description
float
Smaller 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)))
Output
fmin(nan, 2.5): 2.5
min(nan, 2.5) : nan
both nan : True
How It Works
Treats single NaN as absent. Passing nan in either slot returns valid float 2.5.
Built-in min keeps first NaN. Standard min(nan, 2.5) evaluates 2.5 < nan as False and returns nan.
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)
Output
overnight lows: [-2.0, -3.5, 4.5]
How It Works
Outage resilience. Each morning gets a valid low reading as long as at least one probe stayed online.
Negative temperatures compared.-2.0 beats 1.5 cleanly, and valid -3.5 survives an east-probe NaN.
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)
Output
# On a Python version that has math.fmin:
math.fmin: 3.0 -8.0
# Fallback on older Python versions:
fallback: 3.0 -8.0
Lines starting with # label which branch ran; your Python interpreter prints only the matching line.
How It Works
Guarded native call. Calls math.fmin() when available and avoids AttributeError on older releases.
Promotes ints to float. Inputs 3, 7 produce 3.0 and -8 becomes -8.0.
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).