Python math.fma() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.13+

Definition

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

ParameterDescription
xRequired positional first multiplicand (int or float).
yRequired positional second multiplicand (int or float).
zRequired positional addend (int or float).

Return Value

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

How It Works

  1. 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.
  2. Single rounding preserves residual. math.fma() keeps full exact precision of x * y before adding -1.0.
  3. Bit-exact verification. Output matches exact algebraic answer -(2.0 ** -54).

Example 2 — Apply Linear Scale-and-Bias Safely Across Versions

Use hasattr(math, "fma") and fall back to float(x) * float(y) + float(z) on older Python.

python
import math


def mul_add(x, y, z):
    if hasattr(math, "fma"):
        return math.fma(x, y, z)
    return float(x) * float(y) + float(z)


for sensor in [0.0, 2.5, 5.0]:
    calibrated = mul_add(sensor, 2.0, 10.0)
    print(f"raw={sensor} -> calibrated={calibrated}")

How It Works

  1. Runtime capability check. Uses math.fma() natively on Python 3.13+ and standard float arithmetic on older releases.
  2. Explicit float return. Wrapping inputs in float() keeps the fallback return type consistent with math.fma().
  3. Clean calibration curve. Exact binary fractions ensure both branches output identical numbers.

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)

How It Works

  1. Intermediate overflow prevented. Although 2 * 1e308 exceeds binary64 max, subtracting 1e308 brings the fused result back to 1e+308.
  2. True final overflow still raises. Adding only 1.0 leaves the result above max float, raising OverflowError.
  3. 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.

Next: math.fmax()

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

fmax() →

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.

13 people found this page helpful