Python math.fmax() Function

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

Definition

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)
CallWhat 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

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

Return Value

Return ValueDescription
floatLarger 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)))

How It Works

  1. Single NaN ignored. Regardless of which slot holds nan, safe_fmax returns valid float 5.0.
  2. Built-in max returns first NaN. Because nan > 5.0 is False, built-in max(nan, 5.0) keeps the initial nan.
  3. 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)

How It Works

  1. Fault-tolerant merge. Whenever one channel drops out as nan, the healthy reading survives automatically.
  2. Clean float output. Every merged peak returns as a standard float.
  3. 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)

How It Works

  1. Feature-guarded dispatch. Safely calls math.fmax() when present and runs portable logic otherwise.
  2. Float return enforced. Integer arguments 3, 7 yield float 7.0.
  3. 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.

Next: math.fmin()

Return the smaller of x and y as a float, ignoring a single NaN.

fmin() →

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.

14 people found this page helpful