math.fmax(x, y) returns the larger of x and y as a float.
Unlike built-in max(), which returns nan if its first argument is nan, math.fmax() treats a single nan as missing and returns the other valid number (returning nan only when both are nan). Check the official docs for signed-zero tie-breaking details.
math.fmax() is a function in Python's standard math module: import it with import math and call math.fmax(...), or import the name directly with from math import fmax. 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, "fmax") on older Python versions).
python
import math
hi = math.fmax(x, y)
Call
What it does
math.fmax(3, 7)
Returns 7.0 (float).
math.fmax(float("nan"), 5.0)
Returns 5.0 (ignores single NaN).
math.fmax(5.0, float("nan"))
Returns 5.0.
math.fmax(float("nan"), float("nan"))
Returns nan.
Portable fallback for older Python releases: 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.fmax(x, y, /)
# recent Python addition (3.15 at writing); returns float maximum 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
Larger of x and y (or the non-NaN argument when only one is NaN).
Python Examples
Example 1 — Compare NaN Handling Between fmax() and Built-in max()
Use a hasattr wrapper to contrast NaN ignoring with built-in max().
python
import math
def safe_fmax(x, y):
if hasattr(math, "fmax"):
return math.fmax(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("fmax(nan, 5):", safe_fmax(math.nan, 5.0))
print("max(nan, 5) :", max(math.nan, 5.0))
print("both nan :", math.isnan(safe_fmax(math.nan, math.nan)))
Output
fmax(nan, 5): 5.0
max(nan, 5) : nan
both nan : True
How It Works
Single NaN ignored. Regardless of which slot holds nan, safe_fmax returns valid float 5.0.
Built-in max returns first NaN. Because nan > 5.0 is False, built-in max(nan, 5.0) keeps the initial nan.
Two NaNs still yield NaN. When neither argument is numeric, the result stays NaN.
Example 2 — Take Peak Readings Across Two Sensors With Missing NaNs
Merge paired sensor logs cleanly using a portable fmax helper when one channel drops out.
python
import math
fmax_fn = getattr(
math,
"fmax",
lambda x, y: (
float(y) if math.isnan(x)
else (float(x) if math.isnan(y) or x >= y else float(y))
),
)
chan_a = [21.0, math.nan, 25.5]
chan_b = [19.5, 23.0, math.nan]
peaks = [fmax_fn(a, b) for a, b in zip(chan_a, chan_b)]
print("merged peaks:", peaks)
Output
merged peaks: [21.0, 23.0, 25.5]
How It Works
Fault-tolerant merge. Whenever one channel drops out as nan, the healthy reading survives automatically.
Clean float output. Every merged peak returns as a standard float.
Zero exceptions raised. Missing sensor readings do not poison the list.
Example 3 — Detect math.fmax() Support at Runtime
Show explicit output for Python versions with math.fmax() and the fallback branch.
python
import math
if hasattr(math, "fmax"):
print("math.fmax:", math.fmax(3, 7), math.fmax(math.nan, -4))
else:
a = -4.0 if math.isnan(math.nan) else math.nan
print("fallback:", 7.0, a)
Output
# On a Python version that has math.fmax:
math.fmax: 7.0 -4.0
# Fallback on older Python versions:
fallback: 7.0 -4.0
Lines starting with # label which branch ran; your Python interpreter prints only the matching line.
How It Works
Feature-guarded dispatch. Safely calls math.fmax() when present and runs portable logic otherwise.
Version check reminder. Run import sys; print(sys.version) to verify whether your interpreter includes math.fmax.
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, "fmax").
Built-in max(nan, 5.0) returns nan and preserves int types on ints. math.fmax() always returns a float and ignores a single nan, returning the valid number.
float("nan"), because there is no valid number to return.
Check math.isnan(x) and math.isnan(y) first, returning the non-NaN value as float, otherwise float(x) if x >= y else float(y).
No, it takes exactly two positional arguments (x and y), unlike built-in max() which accepts many arguments or an iterable.