Skip to content
Advertisement

Python Convert any number to power of 10 (E-notation)

In my program big numbers is shown off and for this reason I want any number to be converted to power of 10 (E-notation). I tried an idea that came to my mind but I get an error that says:

OverflowError: cannot convert float infinity to integer

How I succeed that I want?

Advertisement

Answer

In Python floats are usually double-precision IEEE-754 floating point numbers; it has 11 exponent bits, which in practice means that it can represent a number ~2^1024 at maximum. If you’ve got a result greater than that: Python will convert it to IEEE 754 infinity instead:

>>> 1e308
1e+308
>>> 1e309
inf

Thus a number this big is not representable as a float.


A number greater than this is still representable in Python as a decimal or as an integer/long.

Advertisement