Exploring Python numerical types
Python provides basic numerical types such as arbitrarily sized integers and floating-point numbers (double precision) as standard, but it also provides several additional types that are useful in specific applications where precision is especially important. Python also provides (built-in) support for complex numbers, which is useful for some more advanced mathematical applications. Let’s take a look at some of these different numerical types, starting with the Decimal
type.
Decimal type
For applications that require decimal digits with accurate arithmetic operations, use the Decimal
type from the decimal
module in the Python Standard Library:
from decimal import Decimal num1 = Decimal('1.1') num2 = Decimal('1.563') num1 + num2 # Decimal('2.663')
Performing this calculation with float objects gives the result 2.6630000000000003, which includes a small error arising from the fact that certain...