[Python-Dev] Memory size overflows
Gerald S. Williams
gsw@agere.com
Mon, 21 Oct 2002 09:33:52 -0400
Tim Peters wrote:
> Goodness no -- that kind of cleverness is usually buggy, and usually by
> failing to account for that the moral equivalent of -sys.maxint-1 is a fine
> product, but sys.maxint+1 isn't.
Agreed. Also, signed values needn't be 2's complement numbers,
and the behavior of signed overflow isn't guaranteed (it could
saturate, for instance). The use of doubles makes sense from a
portability standpoint. Or you could break out the signs and
use unsigned longs with preprocessor (and configure) checks,
being careful about performance impact. :-)
For example, you might need to do something equivalent to this:
#if HAVE_LIMITS_H
#define MAX_POSITIVE_LONG_VALUE (LONG_MAX)
#if -(LONG_MAX) == LONG_MIN
#define MAX_NEGATIVE_LONG_VALUE (LONG_MAX)
#elif (-(LONG_MAX) - 1) == LONG_MIN
#define MAX_NEGATIVE_LONG_VALUE ((LONG_MAX) + 1)
#else
#undef MAX_POSITIVE_LONG_VALUE
...
-Jerry