ulp() function of Python math module

The ulp() function returns the difference between a given floating point number and the next floating point number.

Math Updated 22 Jun 2022 1 min read

Overview

  • For a given floating point number, the function ulp(num) returns the distance between the number and the next floating point number. This is applicable to signed floating point numbers as well.
  • As the magnitude of a floating point number increases the precision with which it can be represented decreases. The ulp also differs as the value range of the floating point number changes.

Example

python
# Example Python program that prints the

            # unit in the last place for a given

            # real number

            import math

# A big magnitude in floating point representation

            num = 100000000000000000.0

            ulp = math.ulp(num)

            print("Number:%f"%num)

            print("ulp:%f"%ulp)

            print("Next floating point number:%f"%(num+ulp))

Output

Output
Number:100000000000000000.000000

ulp:16.000000

Next floating point number:100000000000000016.000000