Open In App

Precision Handling in Python

Python in its definition allows handling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. In this article, we will use high-precision calculations in Python with Decimal in Python.

Example



Input: x = 2.4
Output: Integral value of no = 2
Smallest integer value = 2
Greatest integer value = 3
Explanation: In this, we are handling precision values in python.

Python Ways to Precision Handling

we will explore the various functions that Python provides for managing precise data and decimal points.

Python Handle Precision using Rounding Method

Python provides for managing precise data and decimal points, the round() function is used to round a number to a specified number of decimal places. Alternatively, string formatting options, such as the f-string syntax or the format() method, allow you to format numbers with precise decimal places.






rounded = round(8.88888, 2)
print(rounded)
  
formatted = "{:.3f}".format(9.999)
print(formatted)

Output

8.89
9.999

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision using Getcontext()

In Python, we can handle precision values using getcontext(). The getcontext() function is used to access the current decimal context, which provides control over precision settings. The precision is used to set the desired number of decimal places for mathematical operations using the decimal class.




from decimal import Decimal, getcontext
  
getcontext().prec = 3
  
result = Decimal('3') / Decimal('9')
print(result)

Output

0.333

Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision with Math Module

In Python, we can handle precision values using Math Module. In this example, we will cover some Python math methods. such as trunc() method, ceil() method, and floor() method.




import math
  
a = 3.4536
  
print("The integral value of number is : ", end="")
print(math.trunc(a))
  
# using ceil() to print number after ceiling
print("The smallest integer greater than number is : ", end="")
print(math.ceil(a))
  
# using floor() to print number after flooring
print("The greatest integer smaller than number is : ", end="")
print(math.floor(a))

Output

The integral value of number is : 3
The smallest integer greater than number is : 4
The greatest integer smaller than number is : 3


Time Complexity: O(1)
Auxiliary Space: O(1)

Python Handle Precision with Decimal Module

In Python, we can handle precision values using Decimal Module. In this example, we will see How to Limit Float to Two Decimal Points in Python. In Python float precision to 2 floats in Python, and in Python float precision to 3. There are many ways to set the precision of the floating-point values. Some of them are discussed below.




a = 3.4536
  
print("The value of number till 2 decimal place(using %) is : ", end="")
print('%.2f' % a)
  
# using format() to print value till 3 decimal places
print("The value of number till 3 decimal place(using format()) is : ", end="")
print("{0:.3f}".format(a))
  
# using round() to print value till 2 decimal places
print("The value of number till 2 decimal place(using round()) is : ", end="")
print(round(a, 2))
  
# using f-string to print value till 2 decimal places
print("The value of number till 2 decimal place(using f-string) is : ", end="")
print(f"{a:0,.2f}")

Output

The value of number till 2 decimal place(using %) is : 3.45
The value of number till 3 decimal place(using format()) is : 3.454
The value of number till 2 decimal place(using round()) is : 3.45
The value of number till 2 decimal place(using f-string) is : 3.45

Time Complexity: O(1)
Auxiliary Space: O(1)


Article Tags :