Numerics, NaNs, IEEE 754 and C99
Tim Peters
tim.peters at gmail.com
Thu Jun 15 12:31:51 EDT 2006
[Nick Maclaren]
>>>> Firstly, a FAR more common assumption is that integers wrap in twos'
>>>> complement - Python does not do that.
[Grant Edwards]
>>> It used to
[Fredrik Lundh]
>> for integers ? what version was that ?
[Grant]
> Am I remebering incorrectly?
Mostly but not entirely.
> Didn't the old fixed-width integers operate modulo-wordsize?
Not in Python.
> I distinctly remember having to rewrite a bunch of checksum code when
> fixed-width integers went away.
Best guess is that you're working on a 32-bit box, and remember this
Python <= 2.2 behavior specific to the left shift operator:
>>> 1 << 31
-2147483648
>>> 1 << 32
0
On a 64-bit box (except Win64, which didn't exist at the time ;-)),
those returned 2**31 and 2**32 instead, while "1 << 64" wrapped to 0
(etc).
Python 2.3 starting warning about that:
>>> 1 << 31
__main__:1: FutureWarning: x<<y losing bits or changing sign will
return a long in Python 2.4 and up
-2147483648
and Python 2.4 started producing platform-independent results:
>>> 1 << 31
2147483648L
>>> 1 << 32
4294967296L
+ - * / on short ints always complained about overflow before int-long
unification, although IIRC very early Pythons missed the overflow
error in (on a 32-bit box) int(-(2L**31))/-1.
More information about the Python-list
mailing list