Introducing infinity as an integer in Python 2.7

I am wondering how to define infand -infhow intin Python 2.7. I tried, and it seems that infthey -infwork only as 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'
+4
source share
1 answer

To summarize what was said in the comments

It is impossible to represent infinity as an integer in Python. This is consistent with the behavior of many other languages. However, due to the dynamic typing system of Python, you can use float('inf')instead of an integer, and it will behave as you expected.

"double" , Python , float, , Java, float double . Python , , Java.

+8

Source: https://habr.com/ru/post/1659966/


All Articles