Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Represent infinity as an integer in Python 2.7

I am wondering how to define inf and -inf as an int in Python 2.7. I tried and it seems inf and -inf only work as a 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'
like image 703
Lin Ma Avatar asked Nov 06 '16 04:11

Lin Ma


People also ask

How do you write infinity as an integer in python?

Representing infinity as an Integer in python One can use float('inf') as an integer to represent it as infinity.

How do you represent infinity in python?

Using float to represent infinity in Python Since Infinite numbers are both positive and negative, Therefore, in Python, they can be represented using float('inf') and float('-inf') .

Is infinity an integer?

Integers. Integers are the consists of Whole Numbers including negative values of the Natural Numbers. Fraction numbers are not included in integers, hence they cannot be represented in p/q form. The range of Integers is from the Infinity at the Negative end and Infinity at the Positive end, including zero.

What is float ('- INF ')?

float('inf') As stated in answer above, float('inf') is used for setting a variable with an infinitely large value. In simple words, it sets the value as +ve infinty.


1 Answers

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 in most situations 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.

like image 67
Jezzamon Avatar answered Sep 21 '22 06:09

Jezzamon