I think a problem occured when i used long()

Dan Bishop danb_83 at yahoo.com
Thu Sep 2 03:41:58 CEST 2004


alikakakhel3 at hotmail.com (Ali) wrote in message news:<8f17f4bc.0409011101.5ace996 at posting.google.com>...
> I did the folloing in python shell:
> 
> >>> x = 5.07e-25
> >>> x = long(x)
> >>> print x
> 0L
> 
> Um... I was under the impresion that long numbers had very very long
> precision. But, it seems that in this case it rounded it to zero :(

"long" numbers are indeed unbounded, but they're only for integers;
fractions get truncated.

>>> long(1.001)
1L
>>> long(0.999)
0L

5.07e-25 is between 0 and 1, so it gets rounded to zero.

Perhaps you meant 5.07e+25, which doesn't.

>>> long(5.07e+25)
50699999999999999203082240L

(The "noise" digits at the end are there because the original float
value is stored with only 53 significant bits.)

Or perhaps you really do want high-precision fractional values.  You
*can* use long for this, by scaling your data so it's an integer.  For
example, instead of storing an amount as $1.99, store it as 199 cents
(and remember the scaling by 100).  For greater fractional precision,
use higher scaling factors.

The "decimal" class in Python 2.4 will take care of this behind the
scenes.



More information about the Python-list mailing list