python large number division error

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

import decimal
A=100000000000000000000000
B=1
print(decimal.Decimal(A/B))

The output value at code execution is 999999999999991611392. I wonder what the error is.

python

2022-09-20 08:54

1 Answers

In Python3, the division is a floating point operation, unlike Python2. As for floating point constraints, https://docs.python.org/3/tutorial/floatingpoint.html Please refer to .

In [1]: 10/3
Out[1]: 3.3333333333333335

So the solution is to divide the whole number by Python3. If you use two //s as shown below, it is an integer division.

In [1]: import decimal
    ...: ...: A=100000000000000000000000
    ...: ...: B=1

In [1]: print(decimal.Decimal(A//B))
100000000000000000000000


2022-09-20 08:54

If you have any answers or tips


© 2022 pinfo. All rights reserved.