Python math.ulp() Method



The Python math.ulp() method is used to calculate the "unit in the last place" (ULP) of a floating-point number. The ULP represents the difference between the given floating-point number and the next larger floating-point number of the same precision.

For example, if "x" is represented in binary floating-point format, the ulp(x) method returns the smallest positive floating-point number that, when added to "x", produces a different floating-point number.

This smallest positive difference is often referred to as the "unit in the last place", indicating the minimum increment by which a floating-point number can change in its least significant digit.

Syntax

Following is the basic syntax of the Python math.ulp() method −

math.ulp(x)

Parameters

This method accepts a numeric value representing the floating-point number as a parameter for which you want to calculate the ulp.

Return Value

The method returns a float, which represents the distance between x and the next larger representable floating-point number.

Example 1

In the following example, we are calculating the least significant bit of the floating-point number "1.0" using the math.ulp() method −

import math
result = math.ulp(1.0)
print("The result obtained is:",result)         

Output

The output obtained is as follows −

The result obtained is: 2.220446049250313e-16

Example 2

Here, we are calculating the least significant bit of the negative floating-point number "-2.5" −

import math
result = math.ulp(-2.5)
print("The result obtained is:",result)     

Output

Following is the output of the above code −

The result obtained is: 4.440892098500626e-16

Example 3

Now, we calculate the least significant bit of the floating-point number "0.0" using the math.ulp() method −

import math
result = math.ulp(0.0)
print("The result obtained is:",result)  

Output

We get the output as shown below −

The result obtained is: 5e-324

Example 4

In this example, we use a variable "x" to store the floating-point number "3.14159". We then calculate the least significant bit of "x" −

import math
x = 3.14159
result = math.ulp(x)
print("The result obtained is:",result) 

Output

The result produced is as shown below −

The result obtained is: 4.440892098500626e-16
python_maths.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy