Python Glossary Русский

Python 3.15+mathfunction

math.fmax()

comparison

The larger of two floats, with NaN treated as missing: fmax(nan, 1.0) gives 1.0, whereas max() with NaN behaves unpredictably.

Syntax

math.fmax(x, y)

The built-in max() compares with <, and any comparison with NaN is false, so the result depends on order: max(nan, 1.0) is nan while max(1.0, nan) is 1.0. fmax, following the C standard, ignores NaN when the other value is a number and returns NaN only when both are NaN. For zeros the sign of the result is not guaranteed: fmax(0.0, -0.0) may return either. It takes exactly two arguments and always returns a float.

Examples

import math
nan = float('nan')
print(math.fmax(nan, 1.0), math.fmax(1.0, nan))  # → 1.0 1.0
print(max(nan, 1.0), max(1.0, nan))  # → nan 1.0 — max зависит от порядка

See also

Open in the full glossarydocs.python.orgReport an inaccuracy