14

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' 
13
  • 4
    You can't represent infinity as an integer, but the floating point representation has a special case to allow it. You'll have to use the floating point representation. Commented Nov 6, 2016 at 4:17
  • 1
    stackoverflow.com/questions/7781260/… might help. Commented Nov 6, 2016 at 18:52
  • 1
    @boardrider, thanks and it is very helpful. Looks like no inf for double, only float, correct? Commented Nov 7, 2016 at 1:12
  • 1
    'float' in python refers to a floating point number. Unlike other languages, there isn't a distinction between single or double precision floating point numbers. According to the docs, the float type is usually represented as a double precision number behind the scenes. docs.python.org/2/library/… Commented Nov 7, 2016 at 1:33
  • 1
    Is there a particular reason you wanted to represent it as an integer? Usually in python you should be ok with having all other numbers integers and infinity as float('inf') Commented Nov 7, 2016 at 1:36

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

3 Comments

As of Python 3.5, you can write infinity as math.inf (having put import math in your imports), and negative infinity as -math.inf.
@ArthurTacca, type(math.inf) is float and not int.
@Christian You are right. math.inf is now better than writing float('inf') as suggested in this answer. However, it gives the same actual value, it doesn't answer the original question any better. I hadn't meant to suggest that it does, but I can see that my comment didn't make that clear.
-3

Something very big that is a integer (made in 3.x, not tested in 2.x):

0x40000 #will most likely crash the shell 

so if you wanted a infinite variable you would do:

Infinity = 0x40000 

You can do any Integer to Hexedecimal here:
https://www.binaryhexconverter.com/decimal-to-hex-converter
(make sure to add the 0x before the value it returns for python)

3 Comments

But 0x40001 is larger than 0x40000, so it is not a good idea to use 0x40000 as "infinity".
Also you don't need a website to convert between decimal and hex numbers, when you are using Python.
I still use it, I don't really want to use stuff that is to complex

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.