python large number division error

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

import decimal

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


2022-09-20 08:54

1 Answers

In Python3, the division is a floating point operation, unlike Python2. As for floating point constraints, 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))

2022-09-20 08:54

If you have any answers or tips

© 2022 pinfo. All rights reserved.