Python math.ulp() – Least Significant Bit

Python math.ulp()

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

Syntax

The syntax to call ulp() function is

math.ulp(x)

where

Parameter Required Description
x Yes A float value.

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

Examples

1. Least significant bit of a float value

In the following program, we find the least significant bit of float x=3.1415.

Python Program

import math

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

Output

ulp(x) : 4.440892098500626e-16

2. Least significant bit of a negative float value

In the following program, we find the 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 Code

Output

ulp(x) : 4.440892098500626e-16

3. Least significant bit of infinity

In the following program, we find the 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 Code

Output

ulp(x) : inf

4. Least significant bit of nan

In the following program, we find the 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 Code

Output

ulp(x) : nan

5. Least significant bit of 0

In the following program, we find the 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 Code

Output

ulp(x) : 5e-324

Summary

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

Related Tutorials

Privacy Policy Terms of Use

SitemapContact Us