Python math.ulp() Function
In floating-point computation, understandingthe precision limits of floating-point numbersis very important. ULP (Unit in the Last Place) is the basic unit for measuring floating-point precision.
math.ulp() is a function introduced in Python 3.9 that returns thesmallest significant floating-point unit(ULP) of a floating-point number, that is, the distance from that number to the next representable floating-point number.
Word meaning: ulp is an abbreviation of "Unit in the Last Place", meaning "the unit of the last digit".
Basic syntax and parameters
Syntax format
import math
math.ulp(x)
Parameter Description
- x: a floating-point number
Return Value
Returns the ULP value of x, that is, the difference between x and the next representable floating-point number
Example
Example 1: basic usage
Example
import
math
(
"ULP of 1.0:"
,
math
.
ulp
(
1.0
)
)
(
"ULP of 2.0:"
,
math
.
ulp
(
2.0
)
)
(
"ULP of 100.0:"
,
math
.
ulp
(
100.0
)
)
(
"ULP of 0.0:"
,
math
.
ulp
(
0.0
)
)
(
"Smallest positive number:"
,
math
.
ulp
(
float
.
min
)
Result:
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 of different values
Example
import
math
values
=
[
0.1
,
0.5
,
1.0
,
1.5
,
2.0
,
10.0
,
100.0
,
1000.0
]
(
"ULP of different values:"
)
for
x
in
values:
(
f
" ULP({x}) = {math.ulp(x)}"
)
Result:
不同数值的 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
(
"Infinity:"
,
math
.
ulp
(
math
.
inf
)
)
(
"Largest finite number:"
,
math
.
ulp
(
sys
.
float_info
.
max
)
)
(
"Negative number:"
,
math
.
ulp
(
1.0
)
)
(
"Subnormal number:"
,
math
.
ulp
(
1e-310
)
)
Result:
无穷大: inf
最大有限数: inf
负数: 2.220446049250313e-16
次正规数: 5e-324
Example 4: floating-point precision detection
Example
import
math
def
check_precision
(
x
)
:
"""Check floating-point precision"""
ulp
=
math
.
ulp
(
x
)
relative_error
=
ulp / x
(
f
"x = {x}"
)
(
f
" ULP = {ulp}"
)
(
f
" Relative error is approximately {relative_error:.2e}"
)
(
f
" Significant digits is approximately {-math.log2(relative_error):.1f}"
)
(
)
check_precision
(
1.0
)
check_precision
(
1000000.0
)
Result:
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
Verification: 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
(
f
"x = {x}"
)
(
f
"nextafter(x, inf) - x = {direct_diff}"
)
(
f
"math.ulp(x) = {ulp_calc}"
)
(
f
"The two are equal: {direct_diff == ulp_calc}"
)
Result:
x = 1.0
nextafter(x, inf) - x = 2.220446049250313e-16
math.ulp(x) = 2.220 double 000001e-16
两者相等: True
Description: math.ulp(x) is equivalent to the absolute value of nextafter(x, inf) - x
Application scenarios
- Error analysis of numerical algorithms
- Floating-point precision detection
- Setting tolerances in scientific computing
- Determining a suitable epsilon when comparing floating-point numbers
Notes
- Only available in Python 3.9+
- The ULP value grows as the number grows
- The ULP of inf is also inf
Python math Module