Lin Ma - 1 year ago 189

Python Question

I am wondering how to define

`inf`

`-inf`

`int`

`inf`

`-inf`

`float`

`a = float('-inf') # works`

b = float('inf') # works

c = int('-inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'

d = int('inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'

Answer Source

To summarise what was said in the comments

There is no way to represent infinity as an integer in Python. This matches the behaviour of many other languages. However, due to Python's dynamic typing system, you can use `float('inf')`

in place of an integer, and it will behave as you would expect.

As far as creating a 'double' for infinity, in Python there is just one floating point type, called `float`

, unlike other languages such as Java which uses the term float and double for floating point numbers with different precision. In Python, floating point numbers usually use double-precision, so they act the same as doubles in Java.