13

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'
Asclepius
  • 57,944
  • 17
  • 167
  • 143
Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 3
    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. – Jezzamon Nov 06 '16 at 04:17
  • @Jezzamon, using float point representation of inf/-inf to represent int? Could you show me how? `type(float)` !=` type(int)`, do you need a explicit type conversion to convert float into int? – Lin Ma Nov 06 '16 at 04:42
  • @Jezzamon, I tried it seems `double('inf')` and `double('-inf')` does not work either? – Lin Ma Nov 06 '16 at 04:43
  • 1
    http://stackoverflow.com/questions/7781260/how-can-i-represent-an-infinite-number-in-python might help. – boardrider Nov 06 '16 at 18:52
  • 1
    @boardrider, thanks and it is very helpful. Looks like no inf for double, only float, correct? – Lin Ma Nov 07 '16 at 01: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. https://docs.python.org/2/library/stdtypes.html#numeric-types-int-float-long-complex – Jezzamon Nov 07 '16 at 01: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')` – Jezzamon Nov 07 '16 at 01:36
  • @Jezzamon, sorry I have not made myself understood (I understand "there isn't a distinction between single or double precision floating point numbers"). For question on double, actually my question is simple, I mean it seems `double('inf')` cannot compile in Python 2.7? Do I need to import some library to make it compile? – Lin Ma Nov 07 '16 at 08:01
  • @Jezzamon, in my program, I just need integer, for example, I represent number of student in a district. Representing as `float('inf')` will make some confusions when others reading my code, so wondering if any `inf` for integer in Python 2.7? – Lin Ma Nov 07 '16 at 08:03
  • 1
    @LinMa Yes, there is no infinite integer in any version of Python (like most other programming languages). It is ok to use `float('inf')`, I don't think people will be confused because that's the only way to represent infinity. – Jezzamon Nov 07 '16 at 11:14
  • @Jezzamon, how about for `double`? I also cannot find a `inf` version for `double`, it seems `inf`only works for `float`? – Lin Ma Nov 08 '16 at 06:02
  • 1
    @LinMa `double` doesn't exist in python. What are you thinking it is or what are you hoping that to do? Maybe you are getting confused with Java, which has two different ways of representing floating point numbers, `float` and `double`. In python there is only `float`. – Jezzamon Nov 08 '16 at 22:07
  • @Jezzamon, thanks for the clarification, yes, I am from Java. I have a sense to use inf of int for int, inf of float for float and inf of double for double. If you could add a reply, I will mark it as answer to benefit other people. – Lin Ma Nov 09 '16 at 05:42

2 Answers2

21

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.

Jezzamon
  • 1,453
  • 1
  • 15
  • 27
  • 1
    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`. – Arthur Tacca Nov 22 '19 at 11:44
  • @ArthurTacca, type(math.inf) is float and not int. – Christian Jan 04 '22 at 22:10
  • 1
    @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. – Arthur Tacca Jan 06 '22 at 17:03
-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)