-5

据我了解,python 可以处理任意长度的整数。它可以很好地处理加法和乘法,但似乎会随着除法而分崩离析。

>>> from decimal import Decimal, getcontext
>>> getcontext().prec=2**5 
>>> ORIG_NUM=476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833
>>> print(Decimal(ORIG_NUM)/7)
6.8134241248373119570790782479582E+88
>>> print("%.6f" % (Decimal(ORIG_NUM)/7))
68134241248373123068520468160885027682039120236463560535175757714209021725053511199096832.000000

我不想要 e 符号因此 getcontext().prec 尝试。另外,对 %f 去掉小数点感到困惑?

>>> getcontext().prec=2**7
>>>QUOTIENT= (Decimal(476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833)/7)

>>>print(QUOTIENT)
68134241248373119570790782479581551134271725146826890290284253689229460799396205095551690.428571428571428571428571428571428571429
>>>QUOTIENT*7
4.7693968873861185e+89
>>> Decimal(QUOTIENT)*7
Decimal('476939688738611861479643277126195193774273841655244923746230303999463152075374578393677824')
>>> print(Decimal(QUOTIENT)*7)
476939688738611861479643277126195193774273841655244923746230303999463152075374578393677824
>>> print(Decimal(QUOTIENT)*7)==ORIG_NUM
476939688738611861479643277126195193774273841655244923746230303999463152075374578393677824
False

第二个问题是数字不匹配。

有关该主题的任何信息?

希望这样更好...

4

2 回答 2

10
  1. print(Decimal(QUOTIENT)*7)==ORIG_NUM失败,因为print函数返回None
  2. 如果您想要精确的划分,请使用Fraction该类。

    >>> from fractions import Fraction
    >>> x = 476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833
    >>> f = Fraction(x, 7)
    >>> print(f)
    476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833/7
    >>> f * 7 == x
    True
    
于 2012-11-10T10:22:10.637 回答
0
>>> (Decimal(QUOTIENT)*7)-476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833
Decimal('24484107799769124335834371765627456691714240528174856926479601142724815991')

疯狂的巨大错误。

>>> 68134241248373119570790782479581551134271725146826890290284253689229460799396205095551690*7
476939688738611836995535477357070857939902076027788232031989775824606225595773435668861830
>>> MODD=(Decimal(476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833)%7)
3.000000
>>>TRUNCD_QUO=int((Decimal(476939688738611836995535477357070857939902076027788232031989775824606225595773435668861833)/7))
>>>((TRUNCD_QUO*7)+MODD)==ORIG_NUM
True
于 2012-11-10T10:26:17.140 回答