39
loading...
This website collects cookies to deliver better user experience
"Computers must provide an arithmetic that works in the same way as the arithmetic that people learn at school."- excerpt from the decimal arithmetic specification.
>>> 1.1+3.2
4.300000000000001
>>> 1.001+2.002
3.0029999999999997
>>> 0.2+0.2+0.2-0.6
1.1102230246251565e-16
>>> 0.1+0.2+0.3-0.7
-0.09999999999999987
>>> from decimal import *
>>> Decimal(1.1)+Decimal(2.2)
Decimal('3.300000000000000266453525910')
>>> getcontext().prec=2
>>> Decimal(1.1)+Decimal(2.2)
Decimal('3.3')
>>> getcontext().prec=4
>>> Decimal(1.1)+Decimal(2.2)
Decimal('3.300')
>>> int(Decimal(1.1)+Decimal(2.2))
3
>>> float(Decimal(1.1)+Decimal(2.2))
3.3
>>> print(Decimal(1.1)+Decimal(2.2))
3.300
from decimal import *
Or we can simply import decimal
and type in decimal.Decimal
every time we want to use it. from decimal import *
a way by which we can use everything in the module. We do not require to import a specific function from the module, but we import the entire module itself. More about importing here.
getcontext().prec
This sets the value of the precision accordingly.Decimal(2.2)
we are creating a decimal object, with the value 2.2. One important thing to note here is that 2.2 is not of the type float, but of the type Decimal
The cool fact is that we can directly operate on them using our arithmetic operators. >>> import decimal
# You must import the entire module with a astrix sign
>>> Decimal(1.1)+Decimal(2.2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Decimal' is not defined
>>> from Decimal import *
#decimal module name is small case
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'Decimal'
>>> from decimal import *
>>> decimal(1.1)+decimal(1.2)
#`Decimal()` method is upper case
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
as_tuple()
, sqrt()
and the normalize()
methods from the official documentation. Out of them, the most interesting method is the as_tuple()
method. It is very useful when it comes to separating a decimal number into digits. The method returns a tuple containing sign, digits and the exponent value of the decimal. Note that in the sign field when the number is 0, it means the decimal is positive, when it is 1, it represents the negative number.