Python 3 strange division

让人想犯罪 __ 提交于 2019-12-01 20:51:49

问题


About half an hour thinking "what am i doing wrong!?" on the 5-lines code.. because Python3 is somehow rounding big integers. Anyone know why there is a problem such:

Python2:

int(6366805760909027985741435139224001        # This is 7**40.
    / 7) == 909543680129861140820205019889143 # 7**39

Python3:

int(6366805760909027985741435139224001 
    / 7) == 909543680129861204865300750663680 # I have no idea what this is.

回答1:


Python 3 is not "rounding big integers". What it does is that it will return a float after division. Hence, in Python 2:

>>> 4/2
2

while in Python 3:

>>> 4/2
2.0

The reason for this is simple. In Python 2, / being integer division when you use integers have some surprising results:

>>> 5/2
2

Ooops. In Python 3 this is fixed:

>>> 5/2
2.5

This means that in Python 3, your division returns a float:

>>> 6366805760909027985741435139224001/7
9.095436801298612e+32

This float has less accuracy than the digits you need. You then convert this to an integer with int(), and you get a number you don't expect.

You should instead use integer division (in both Python 2 and Python 3):

>>> 6366805760909027985741435139224001//7
909543680129861140820205019889143L

(The trailing L means it's a long integer, in Python 3 the long and the normal integer is merged, so there is no trailing L).




回答2:


In Python 3 / is floating point division so it may not treat your arguments like integers. Use

// 

to do integer division in Python 3.




回答3:


You might be interested in the fractions module:

$ pythons 'import fractions; print("%.30f" % fractions.Fraction("1/9"))'
/usr/local/cpython-2.4/bin/python
   Traceback (most recent call last):
     File "<string>", line 1, in ?
   ImportError: No module named fractions
/usr/local/cpython-2.5/bin/python
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
   ImportError: No module named fractions
/usr/local/cpython-2.6/bin/python 0.111111111111111104943205418749
/usr/local/cpython-2.7/bin/python 0.111111111111111104943205418749
/usr/local/cpython-3.0/bin/python 0.111111111111111104943205418749
/usr/local/cpython-3.1/bin/python 0.111111111111111104943205418749
/usr/local/cpython-3.2/bin/python 0.111111111111111104943205418749
/usr/local/cpython-3.3/bin/python 0.111111111111111104943205418749
/usr/local/cpython-3.4/bin/python 0.111111111111111104943205418749
/usr/local/pypy-2.2/bin/pypy 0.111111111111111104943205418749
/usr/local/jython-2.7b1/bin/jython 0.111111111111111100000000000000


来源:https://stackoverflow.com/questions/20810729/python-3-strange-division

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!