Python math.ulp() Function

Python math 模块Python math Module


In floating-point calculations, understandingthe precision limitations of floating-point numbersis very important. ULP (Unit in the Last Place) is the basic unit for measuring the precision of floating-point numbers.

math.ulp()is a function introduced in Python 3.9, used to return thesmallest significant floating-point unit(ULP), that is, the distance from that number to the next representable floating-point number.

Word Definition: ulpis the abbreviation of "Unit in the Last Place", meaning "the unit of the last place".


Basic Syntax and Parameters

Syntax Format

import math

math.ulp(x)

Parameter Description

  • x: floating-point number

Return Value

Returns the ULP value of x, that is, the difference between x and the next representable floating-point number


Examples

Example 1: Basic Usage

Example

import math

print("ULP of 1.0:", math.ulp(1.0))
print("ULP of 2.0:", math.ulp(2.0))
print("ULP of 100.0:", math.ulp(100.0))
print("ULP of 0.0:", math.ulp(0.0))
print("Smallest positive number:", math.ulp(float.min)

Output:

1.0 的 ULP: 2.220446049250313e-16
2.0 的 ULP: 4.440892098500626e-16
100.0 的 ULP: 2.8421709430404007e-14
0.0 的 ULP: 5e-324

Example 2: ULP for Different Values

Example

import math

values = [0.1, 0.5, 1.0, 1.5, 2.0, 10.0, 100.0, 1000.0]
print("ULP for different values:")
for x in values:
    print(f"  ULP({x}) = {math.ulp(x)}")

Output:

不同数值的 ULP:
  ULP(0.1) = 1.3877787807814457e-17
  ULP(0.5) = 1.3877787807814457e-16
  ULP(1.0) = 2.220446049250313e-16
  ULP(1.5) = 2.220446049250313e-16
  ULP(2.0) = 4.440892098500626e-16
  U浮点 10.0: 2.220446049250313e-15
  ULP(100.0) = 2.8421709430404007e-14
  ULP(1000.0) = 2.2737367544323206e-13

Example 3: Special Values

Example

import math

print("Infinity:", math.ulp(math.inf))
print("Largest finite number:", math.ulp(sys.float_info.max))
print("Negative number:", math.ulp(-1.0))
print("Subnormal number:", math.ulp(1e-310))

Output:

无穷大: inf
最大有限数: inf
负数: 2.220446049250313e-16
次正规数: 5e-324

Example 4: Floating-Point Precision Detection

Example

import math

def check_precision(x):
    """Detect floating-point precision"""
    ulp = math.ulp(x)
    relative_error = ulp / x
    print(f"x = {x}")
    print(f"  ULP = {ulp}")
    print(f" Relative error ≈ {relative_error:.2e}")
    print(f" Significant digits ≈ {-math.log2(relative_error):.1f}")
    print()

check_precision(1.0)
check_precision(1000000.0)

Output:

x = 1.0
  ULP = 2.220446049250313e-16
  相对误差 ≈ 2.22e-16
  有效位数 ≈ 52.0

x = 1000000.0
  ULP = 1.907344663e-13
  相对误差 ≈ 1.91e-13
  有效位数 ≈ 52.0

Example 5: Combining with nextafter

Example

import math

# Verify: nextafter(x, inf) - x = ULP(x)
x = 1.0
next_val = math.nextafter(x, math.inf)
ulp_calc = math.ulp(x)
direct_diff = next_val - x

print(f"x = {x}")
print(f"nextafter(x, inf) - x = {direct_diff}")
print(f"math.ulp(x) = {ulp_calc}")
print(f"Equal: {direct_diff == ulp_calc}")

Output:

x = 1.0
nextafter(x, inf) - x = 2.220446049250313e-16
math.ulp(x) = 2.220 double 000001e-16
两者相等: True

Note: math.ulp(x) is equivalent to |nextafter(x, inf) - x|


Application Scenarios

  • Error analysis of numerical algorithms
  • Floating-point precision detection
  • Tolerance setting in scientific computing
  • Determining an appropriate epsilon when comparing floating-point numbers

Notes

  • This function is only available in Python 3.9+
  • The ULP value increases as the number increases
  • The ULP of inf is also inf

Python math 模块Python math Module

Other Extensions