Ulp() function of Python math module

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:

# 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:

Number:100000000000000000.000000

ulp:16.000000

Next floating point number:100000000000000016.000000

 


Copyright 2024 © pythontic.com