Timothée Tardy
Timothée Tardy

Reputation: 25

technical problem on python with infinite float

I am using Python, and I have a problem, I want to do a program tha can count from 1 to infinite, to know how much is the infinite. Here is my code :

a=0
for i in range(1, 10e+99):
  a += 1
  print (a)

but it says " 'float' object cannot be interpreted as an integer "

whereas 10e+99 is not a float help me please

Upvotes: 2

Views: 553

Answers (5)

Eric Postpischil
Eric Postpischil

Reputation: 222437

  1. Per the Python 2 documentation and Python 3 documentation, range requires integer arguments.

  2. In IEEE-754 32-bit binary floating-point, the largest representable finite number is about 3.4028e38. When converting numerals, such as 1e99 in source code, to this format, any number greater than or equal to 2128−2104 (340,282,377,062,143,265,289,209,819,405,393,854,464) will be converted to infinity, assuming the common round-to-nearest-ties-to-even method is used. Because of this, 10e+99 (which stands for 10•1099 and hence 10100) would act like infinity. However, Python implementations more typically use IEEE-754 64-bit binary floating-point, in which the largest representable finite number is 21024−2971, and 10e99 acts as a finite number.1 Thus, to get infinity, you would need around 1e309.

  3. It is not humanly possible to test whether a loop incrementing by 1 from 1 to 10e99 will produce infinity because the total computing power available to humans is only around 1030 additions per year (for a loose sense of “around”, some orders of magnitude). This is insufficient to count to the limit of 32-bit floating-point finite numbers, let alone that of the 64-bit floating-point numbers.

  4. If the arithmetic were done in a floating-point format, it would never reach infinity even with unlimited computing power because, once the sum reached 253 in IEEE-754 64-bit binary, adding 1 would not change the number; 253 would be produced in each iteration. This is because IEEE-754 64-bit binary has only 53 bits available for the significand, so 253+1 is not representable. The nearest representable values are 253 and 253+2. When arithmetic is performed, the exact real-number result is by default rounded to the nearest representable value, with ties rounded to the number with the even low bit in its significand. When 1 is added to 253 the real-number result 253+1 is rounded to 253, and the sum thus stays at 253 for all future iterations.

Footnote

1 The representable value nearest 10100 is 10,000,000,000,000,000,159,028,911,097,599,180,468,360,808,563,945,281,389,781,327,557,747,838,772,170,381,060,813,469,985,856,815,104.

Upvotes: 1

aka.nice
aka.nice

Reputation: 9372

Here is what is going to happen if you correct and execute your program:

a=0
for i in range(1, 10**100):
  a += 1
  print (a)

Suppose you have a super efficient python virtual machine (everyone knows how efficient they are...).

Suppose you have a very efficient implementation of (unbounded) large integers.

Suppose each loop takes a few machine cycles to print those numbers in decimal form (say only 1000 which is well under reality).

Suppose each cycle takes approximately 1.0e-10 s (10GHz) which means having an implementation of print taking advantage of parallelism.

With those irrealistic hypothesis, that's already 10^93 s necessary for the program to complete.

The age of universe is estimated to be less than 10^18 s. Whaouh! It gonna be long.

Now let's compute the energy it's gonna take on a base of 400W computer. Assuming that all Sun matter (2e30 kg) can be converted into electrical power for your computer (thru E=m c^2), you are going to consume about 2 10^48 equivalent of Sun to perform this computation.

Before you hit return, I kindly ask you: think twice! Save the universe!

Upvotes: 0

Sohel Reza
Sohel Reza

Reputation: 321

With is code you can check you variable is infinity or not.

import math

infinity = float('inf')
a = 99999999999999999999999999999999
if a > infinity:
 print('Your number is an infinity number')
else:
 print('Your number is not an infinity number')

#or you can check with math.isinf

print('Your number is Infinity: ',math.isinf(infinity ))

# Also infinity can be both positive and negative 

Note: infinity is infinity that has no end, whatever your value or number you enter it will always return false.

Upvotes: 0

rlucatoor
rlucatoor

Reputation: 131

The problem arises because the range() function takes an int, whereas 10e+99 is indeed a float. While 10e+99 is of course not infinity, and therefore you shouldn't expect infinity to pop up anywhere during the execution of your program, if you really wanted to get your for loop to work as it is you could simply do

a=0
for i in range(1, int(10e+99)):
    a += 1
    print (a)

As other users have pointed out, I would however rethink your strategy entirely: using a range-based for loop to "find out" the value of infinity just doesn't work. Infinity is not a number.

Upvotes: 0

quamrana
quamrana

Reputation: 39354

Perhaps you meant your program to go on forever:

a = 0
while True:
  a += 1
  print(a)

In my head when I see while True: I replace it with 'forever'.

Upvotes: 0

Related Questions