Python math.ulp() – Least Significant Bit

Python math.ulp()

math.ulp(x) function least significant bit of the float value x.

Syntax

The syntax to call ulp() function is

math.ulp(x)

where

ParameterRequiredDescription
xYesA float value.

math.ulp() function is new in Python version 3.9.

Examples

Least significant bit of float x=3.1415.

Python Program

import math

x = 3.1415
result = math.ulp(x)
print('ulp(x) :', result)
Run

Output

ulp(x) : 4.440892098500626e-16

Least significant bit of negative float x=-3.1415. If x is negative value, then math.ulp() returns ulp(-x).

Python Program

import math

x = -3.1415
result = math.ulp(x)
print('ulp(x) :', result)
Run

Output

ulp(x) : 4.440892098500626e-16

Least significant bit of x=infinity. math.ulp() returns infinity.

Python Program

import math

x = math.inf
result = math.ulp(x)
print('ulp(x) :', result)
Run

Output

ulp(x) : inf

Least significant bit of x=NaN. math.ulp() returns nan.

Python Program

import math

x = math.nan
result = math.ulp(x)
print('ulp(x) :', result)
Run

Output

ulp(x) : nan

Least significant bit of float x=0. math.ulp() returns smallest positive denormalised representable float.

Python Program

import math

x = 0
result = math.ulp(x)
print('ulp(x) :', result)
Run

Output

ulp(x) : 5e-324

Summary

In this Python Examples tutorial, we learned the syntax of, and examples for math.trunc() function.