math.fma(x, y, z) performs a fused multiply-add: it computes (x * y) + z as if with infinite intermediate precision and rounds only once at the end, returning a float.
Added in Python 3.13, single rounding rescues both catastrophic cancellation (when x * y nearly cancels z) and intermediate overflow (when x * y exceeds float max before z pulls it back down).
math.fma() is a function in Python's standard math module: import it with import math and call math.fma(...), or import the name directly with from math import fma. Added in Python 3.13.
Syntax
Import math and pass three real numbers (Python 3.13+).
python
import math
result = math.fma(x, y, z)
Call
What it does
math.fma(2, 3, 4)
Returns 10.0.
math.fma(1.0 + 2**-27, 1.0 - 2**-27, -1.0)
Returns -5.551115123125783e-17 (exact -(2**-54)).
math.fma(1e308, 2.0, -1e308)
Returns 1e+308 without intermediate overflow.
math.fma(1e308, 2.0, 1.0)
Raises OverflowError: final result overflows.
Requires Python 3.13 or newer. Guard scripts with hasattr(math, "fma") when supporting Python 3.12 or older.
Function signatures
python
math.fma(x, y, z, /)
# added in Python 3.13; computes (x * y) + z with single rounding
Parameters
Parameter
Description
x
Required positional first multiplicand (int or float).
y
Required positional second multiplicand (int or float).
z
Required positional addend (int or float).
Return Value
Return Value
Description
float
Single-rounded result of (x * y) + z.
Python Examples
Example 1 — Rescue Catastrophic Cancellation in (1+e)*(1-e) - 1 (Python 3.13+)
See naive (x * y) - 1.0 drop to 0.0 while math.fma(x, y, -1.0) recovers -(2**-54).
python
import math
eps = 2.0 ** -27
x = 1.0 + eps
y = 1.0 - eps
naive = (x * y) - 1.0
fused = math.fma(x, y, -1.0)
print("naive:", naive)
print("fma :", fused, fused == -(2.0 ** -54))
Output
naive: 0.0
fma : -5.551115123125783e-17 True
Requires Python 3.13 or newer.
How It Works
Naive product rounds to 1.0. Multiplying (1 + 2**-27)(1 - 2**-27) yields 1 - 2**-54, which rounds to 1.0 so subtracting 1.0 collapses to 0.0.
Single rounding preserves residual.math.fma() keeps full exact precision of x * y before adding -1.0.
Example 3 — See Intermediate Overflow Avoidance and Final OverflowError
Compare math.fma(1e308, 2.0, -1e308) with a fallback check on older Python.
python
import math
if hasattr(math, "fma"):
print("avoided intermediate overflow:", math.fma(1e308, 2.0, -1e308))
try:
math.fma(1e308, 2.0, 1.0)
except OverflowError:
print("final result still overflows")
else:
print("naive (1e308 * 2.0) - 1e308:", (1e308 * 2.0) - 1e308)
Output
# On Python 3.13+:
avoided intermediate overflow: 1e+308
final result still overflows
# On Python 3.12 and older:
naive (1e308 * 2.0) - 1e308: inf
Lines starting with # label which Python version prints each block; the script itself prints only the lines below the matching header.
How It Works
Intermediate overflow prevented. Although 2 * 1e308 exceeds binary64 max, subtracting 1e308 brings the fused result back to 1e+308.
True final overflow still raises. Adding only 1.0 leaves the result above max float, raising OverflowError.
Older Python fallback contrast. Without fma(), naive 1e308 * 2.0 overflows immediately to inf.
Frequently Asked Questions
math.fma() was introduced in Python 3.13.
x * y + z rounds twice (once after multiplying and once after adding), whereas math.fma(x, y, z) rounds only once after the entire expression.
It prevents intermediate overflow when x * y overflows but x * y + z fits in a float (like fma(1e308, 2.0, -1e308)). If the final result overflows, OverflowError is still raised.
Always a float (10.0).
Check hasattr(math, "fma") and fall back to float(x) * float(y) + float(z) when exact single-rounding is not critical.