Python math.ulp() Function

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

Definition

math.ulp(x) returns the value of the least significant bit (Unit in the Last Place) of the float x.

Added in Python 3.9, it reveals the exact spacing between adjacent floats at scale x: tiny 5e-324 at zero, 2.22e-16 at 1.0, and jumping all the way to 2.0 around 1e16.

math.ulp() is a function in Python's standard math module: import it with import math and call math.ulp(...), or import the name directly with from math import ulp. Added in Python 3.9.

Syntax

Import math and pass any real number (Python 3.9+).

python
import math

spacing = math.ulp(x)
CallWhat it does
math.ulp(1.0)Returns 2.220446049250313e-16 (2**-52).
math.ulp(0.0)Returns 5e-324 (smallest positive subnormal).
math.ulp(1e16)Returns 2.0 (integers differ by 2 here!).
math.ulp(float("-inf"))Returns inf (always non-negative).

Notice math.ulp(x) is always non-negative (ulp(-x) == ulp(x)) and increases in powers of two across exponent bands. Pairs directly with math.nextafter().

Function signatures
python
math.ulp(x, /)
# added in Python 3.9; returns float value of least significant bit

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
floatNon-negative spacing at x; inf for Β±inf, nan for nan, 5e-324 for zero and subnormals.

Python Examples

Example 1 — Watch Float Spacing Grow From 5e-324 to 2.0

Inspect math.ulp(x) across scales from zero up to 1e16 (Python 3.9+).

python
import math

for x in [0.0, 1.0, 1e6, 1e16]:
    gap = math.ulp(x)
    print(f"x={x!r:>5} -> ulp={gap}")

How It Works

  1. Minimum subnormal quantum. At zero, adjacent floats sit 5e-324 (2**-1074) apart.
  2. Unit scale precision. At 1.0, float spacing equals machine epsilon 2**-52.
  3. Integers skipped at 1e16. Once magnitude crosses 2**53, spacing hits 2.0 so odd numbers like 1e16 + 1.0 cannot be stored.

Example 2 — Explain Integer Rounding Past 2**53 With math.ulp()

Verify 1.0 + ulp(1.0) == nextafter(1.0, 2.0) and inspect ulp(2**53).

python
import math

print(1.0 + math.ulp(1.0) == math.nextafter(1.0, 2.0))
big = float(2 ** 53)
print("ulp(2**53):", math.ulp(big))
print("2**53 + 1 == 2**53:", big + 1.0 == big)

How It Works

  1. Exact next representable float. Adding ulp(1.0) to 1.0 steps cleanly to the neighbor returned by math.nextafter().
  2. Gap hits two at 2**53. Because binary64 has 53 bits of significand, spacing doubles from 1.0 to 2.0 right at 2**53.
  3. Why +1.0 disappears. Adding 1.0 is halfway between representable numbers and rounds back to even 2**53.

Example 3 — Check Sign Symmetry, Negative Infinity, and NaN in ulp()

Verify ulp(-5.0) == ulp(5.0), positive inf on -inf, and NaN propagation.

python
import math

print(math.ulp(-5.0) == math.ulp(5.0))
print(math.ulp(float("-inf")))
print(math.isnan(math.ulp(float("nan"))))

How It Works

  1. Sign does not matter. Spacing depends only on magnitude, so positive and negative numbers share identical ULP values.
  2. Positive infinity output. Even passing negative infinity returns positive inf.
  3. NaN propagates. Taking ULP of NaN yields NaN.

Frequently Asked Questions

Python 3.9.
Unit in the Last Place (or Unit of Least Precision) β€” the numerical distance represented by the lowest bit of a floating-point number.
5e-324 (2**-1074), the smallest positive subnormal IEEE 754 float.
ULP measures spacing magnitude, so it is always non-negative (or nan for NaN).
At 2**53 (~9e15), math.ulp(float(2**53)) reaches 2.0, proving that consecutive odd integers can no longer be represented as distinct floats.

Back to the function index

See all 62 Python math functions grouped by task.

Function Index →

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.

15 people found this page helpful