Python – math.ulp(x) function
ULP stands for “Unit in the Last Place”. math.ulp() is introduced in python 3.9.0 version, it returns the value of the least significant bit of the float x. In numerical analysis and computer science, unit of least precision (ULP) or unit in the last place is the spacing between floating-point numbers.
- If the argument is a NaN (not a number), output is NaN.
- If input x is negative, output is ulp(-x).
- If input x is positive infinity, output is inf.
- If the input is zero, the smallest positive denormalized representable float is the output(smaller than the minimum positive normalized float, sys.float_info.min).
- If input value x is the largest positive representable float, the value of the least significant bit of x is the output, such that the first float smaller than x is x – ulp(x).
- Otherwise, if input value x is a positive finite number, the value of the least significant bit of x is the output, such that the first float bigger than x is x + ulp(x).
x: float whose ulp is returned
Return the value of the least significant bit of the float x.
Example: To show the working of math.ulp(x) method.
nan inf inf 5e-324 1.99584030953472e+292 8.881784197001252e-16 8.881784197001252e-16