python - decimal.InvalidOperation,除法对于非常大的数字来说是不可能的

标签 python python-3.x

使用Python 3.5.2

>>> from decimal import Decimal
>>> Decimal('12') % Decimal('0.01')
Decimal('0.00')
>>> Decimal('234567') % Decimal('0.01')
Decimal('0.00')

按预期工作。但是...

>>> Decimal('7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450') % Decimal('0.01')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]

编辑:这是我发现的可能导致此错误的最小数字:

>>> Decimal(10**26) % Decimal('0.01') 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]

为什么 Decimal(very_large_int) % Decimal('0.01') 会出现此错误?我认为 Decimal 能够处理非常大的数字?

最佳答案

作为 L3viathan answered ,问题在于结果(不是结果 - 这是我在评论中提到的“隐藏部分”)超出了可用精度。

如果我们使用Python2,隐藏的部分会更加明显:

Traceback (most recent call last):
  File "/tmp/d.py", line 24, in <module>
    print(big % Decimal('0.01'))
  File "/usr/local/lib/python2.7/decimal.py", line 1460, in __mod__
    remainder = self._divide(other, context)[1]
  File "/usr/local/lib/python2.7/decimal.py", line 1381, in _divide
    'quotient too large in //, % or divmod')
  File "/usr/local/lib/python2.7/decimal.py", line 3873, in _raise_error
    raise error(explanation)
InvalidOperation: quotient too large in //, % or divmod

本质上,a % b 是通过同时进行除法和求模来实现的(类似于 Knuth 第 2 卷中的算法 D;对于仅限于两个全字的 C 实现,请参阅 qdivrem.c code I wrote in the early 2000s )。因此,库代码需要两位额外的数字(Decimal('0.01') 中小数点右侧的位数 - 计算实际所需的位数并不像 >big 下面,因为我们必须查看指数)来计算中间商。

十进制库直接用 C 语言为 Python3 重新实现,隐藏了细节,但两者的解决方法是相同的:扩展精度。下面是一个示例源例程,它捕获异常并重试,但使用了魔术常量 2。

from __future__ import print_function
import decimal
Decimal = decimal.Decimal
import traceback
big = Decimal(
    '731671765313306249192251196744265747423553491949349698352031277'
    '4506326239578318016984801869478851843858615607891129494954595017379'
    '5833195285320880551112540698747158523863050715693290963295227443043'
    '5576689664895044524452316173185640309871112172238311362229893423380'
    '3081353362766142828064444866452387493035890729629049156044077239071'
    '3810515859307960866701724271218839987979087922749219016997208880937'
    '7665727333001053367881220235421809751254540594752243525849077116705'
    '5601360483958644670632441572215539753697817977846174064955149290862'
    '5693219784686224828397224137565705605749026140797296865241453510047'
    '4821663704844031998900088952434506585412275886668811642717147992444'
    '2928230863465674813919123162824586178664583591245665294765456828489'
    '1288314260769004224219022671055626321111109370544217506941658960408'
    '0719840385096245544436298123098787992724428490918884580156166097919'
    '1338754992005240636899125607176060588611646710940507754100225698315'
    '520005593572972571636269561882670428252483600823257530420752963450')
try:
    print(big % Decimal('0.01'))
except decimal.DecimalException:
    traceback.print_exc()
    print('')
    ctx = decimal.getcontext()
    print('failed because precision was', ctx.prec, 'and big is',
        len(big.as_tuple().digits), 'digits long')
    print('trying again with 2 more digits')
    with decimal.localcontext() as ctx:
        ctx.prec = len(big.as_tuple().digits) + 2
        try:
            print(big % Decimal('0.01'))
        except decimal.DecimalException:
            traceback.print_exc()

使用Python2:

$ python2 /tmp/d.py
Traceback (most recent call last):
  File "/tmp/d.py", line 24, in <module>
    print(big % Decimal('0.01'))
  File "/usr/local/lib/python2.7/decimal.py", line 1460, in __mod__
    remainder = self._divide(other, context)[1]
  File "/usr/local/lib/python2.7/decimal.py", line 1381, in _divide
    'quotient too large in //, % or divmod')
  File "/usr/local/lib/python2.7/decimal.py", line 3873, in _raise_error
    raise error(explanation)
InvalidOperation: quotient too large in //, % or divmod

failed because precision was 28 and big is 1000 digits long
trying again with 2 more digits
0.00

使用Python3:

$ python3 /tmp/d.py
Traceback (most recent call last):
  File "/tmp/d.py", line 24, in <module>
    print(big % Decimal('0.01'))
decimal.InvalidOperation: [<class 'decimal.DivisionImpossible'>]

failed because precision was 28 and big is 1000 digits long
trying again with 2 more digits
0.00

请注意,除以一个非常大的数字实际上更容易:除以 0.01 会导致这里出现问题。如果除数的指数至少为 1000 - 28(1e972 或更大),我们就不会有问题。

关于python - decimal.InvalidOperation,除法对于非常大的数字来说是不可能的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868278/

相关文章:

python - Pyserial 什么时候应该使用 flush?

python - 如何创建多维列表

Python: "FOR"循环打印基于行的列字段匹配

python - xlwt3 python中的formula.py错误

python - 删除s3文件也会删除文件夹python代码

python - Python 3.5.3 中相同的字符串怎么可能是不同的对象?

python - 使用 Numpy 更快地提取多维数组的下三角?

python - 在python中将类似字符串的对象转换为字符串

Python:在无限循环函数中只运行一次代码段..?

python - python列表中通过其值的子字符串获取元素