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)
Call
What 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
Parameter
Description
x
Required positional real number (int or float).
Return Value
Return Value
Description
float
Non-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}")