
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decimal Fixed Point and Floating Point Arithmetic in Python
This article will explain how decimal fixed-point and floating-point arithmetic work in Python, which is useful for performing accurate calculations in various applications.
Numbers in Python can be stored in two ways: floating-point and decimal fixed-point. Floating-point numbers are fast but can sometimes be incorrect because of how computers store them.
On the other hand, Decimal fixed-point numbers are more accurate and useful when working with precise calculations.
By the end of this article, you will understand how both types of numbers work, when to use them, and how to write programs using them.
Floating-Point Arithmetic
The following example will use the float type to perform floating-point arithmetic in Python. Here we will add 0.1 and 0.2, which are floating-point numbers. And the output will show the result of this addition.
x = 0.1 + 0.2 print(x)
Output
This will create the following outcome -
0.30000000000000004
The above output is not what we expect because of how floating-point numbers are stored in computers. The result is slightly off due to the limitations of binary representation.
Decimal Fixed-Point Arithmetic
This is another way to check if a number is stored as a decimal fixed-point number. The Decimal class from the decimal module helps us to perform arithmetic operations accurately.
The output of this example will show the correct result of adding 0.1 and 0.2 using Decimal.
# Import the Decimal class from the decimal module from decimal import Decimal # Create Decimal objects and perform addition x = Decimal("0.1") + Decimal("0.2") # Print the result print(x)
Output
This will generate the following result -
0.3
Currency Calculation Using Decimal
The following example shows how to use the Decimal class for currency calculations. It sets a precision level and performs a multiplication operation to calculate the total price of items.
The output will show the total price calculated using Decimal.
# Import decimal and getcontext from decimal module from decimal import Decimal, getcontext # Set precision getcontext().prec = 4 # Define price and quantity as Decimal objects price = Decimal("19.99") qty = Decimal("3") # Calculate total and print it total = price * qty # Print the total print(total)
Output
This will produce the following result -
59.97
Difference between Floating-Point and Decimal
In this example, we compare floating-point and Decimal arithmetic in Python. The output shows that floating-point addition may have small precision errors, while Decimal addition provides an exact result.
# Import Decimal class from decimal module from decimal import Decimal # Floating-point addition float_result = 0.1 + 0.2 # Decimal addition decimal_result = Decimal("0.1") + Decimal("0.2") # Print both results print("Floating-point result:", float_result) print("Decimal result:", decimal_result)
Output
This will lead to the following outcome -
Floating-point result: 0.30000000000000004 Decimal result: 0.3
Floating-Point vs Decimal Fixed-Point
Here are the differences between floating-point and decimal fixed-point in tabular form -
Feature | Floating-Point | Decimal Fixed-Point |
---|---|---|
Speed | Floating-point arithmetic is faster and widely used in computations that need quick calculations. | Decimal fixed-point arithmetic is slower because it prioritizes accuracy over speed. |
Precision | Floating-point arithmetic can sometimes lead to small rounding errors because of binary representation. | Decimal fixed-point arithmetic provides high precision and avoids rounding errors, which makes it ideal for exact calculations. |
Financial Use | Floating-point arithmetic is not recommended for financial applications because of possible inaccuracies in calculations. | Decimal fixed-point arithmetic is the best choice for financial applications as it ensures precise calculations. |