Infinity generated in python code

谁都会走 提交于 2019-12-23 09:30:11

问题


I'm looking over some complex Python 2.6 code which is occasionally resulting in an infinity being generated (at least an Infinity being serialized by the json library -- which checks w/ math.isinf).

What is especially baffling is that Python (as far as I can tell) shouldn't be able to ever produce computation results set to infinity. Am I wrong with this assumption? I was aware you can only get infinities from constants:

k = float('inf')
k = 1e900

回答1:


Somewhere between 1e308 and 1e309 the floats run out of precision, so if you are computing results above that range you will see inf

>>> 1e308
1e+308
>>> 1e309
inf

>>> json.dumps(1e308,allow_nan=False)
'1e+308'
>>> json.dumps(1e309,allow_nan=False)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/json/__init__.py", line 237, in dumps
    **kw).encode(obj)
  File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
    chunks = list(self.iterencode(o))
  File "/usr/lib/python2.6/json/encoder.py", line 304, in _iterencode
    yield floatstr(o, self.allow_nan)
  File "/usr/lib/python2.6/json/encoder.py", line 47, in floatstr
    raise ValueError(msg)
ValueError: Out of range float values are not JSON compliant: inf
>>> 

Decimal can handle larger numbers, but obviously there is a performance penalty (and it can't be serialised with json)

>>> from decimal import Decimal
>>> Decimal('1e900')/10
Decimal("1E+899")

Here is an example of an addition that doesn't raise overflow exception

>>> a=1e308
>>> a+a
inf


来源:https://stackoverflow.com/questions/1824751/infinity-generated-in-python-code

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