Exponential Notation and integers

Timothy Fitz firemoth at gmail.com
Fri Nov 19 03:02:46 CET 2004


My comment is more of a linguistic complaint. The differences between
an integer (type int or long) and a float in practice are large,
shouldn't 1e100 be of an integer type, since it -is- an integer? The
default practice here of making it a float just seems wrong.

[Peter Hansen]
> Maybe something like this would help the OP:
> 
> >>> def number(x):
> ...    return int(float(x))
> ...
> >>> MYCONSTANT = number('1.344e3')
> >>> print MYCONSTANT
> 1344

Yes, this would work in some cases, but not where it matters. Floats
have a significant amount of error when you are talking about very
large numbers, which is the only time you would want integer
exponential literals anyway, examples of errors:

>>> long(1e100)
10000000000000000159028911097599180468360808563945281389781327557747838772170381060813469985856815104L
>>> 1e20 == 10 ** 20 + 8192
True
>>> long(1e23) == 10 ** 23
False

The worst part is that because python will implicitly convert between
floats and integers, this can all happen silently and come back and
bite you where you least expect it.



More information about the Python-list mailing list