Overflow-less integer arithmetic?

Andrew Dalke dalke at acm.org
Mon Aug 20 03:40:45 CEST 2001


Tim:
>Do you really find such OverflowErrors more useful than not?  I'm asking
>because PEP 237 seeks to eradicate them:
>
>    http://python.sf.net/peps/pep-0237.html

Despite what I said yesterday, I have used and needed lont ints
a few times in real code.  There are two cases:
  1) I expect to get large values (>2**31) frequently
  2) I expect to get them infrequently

Case #1 is the more common, and is solved by using an 'L' in the
appropriate initialization.  There have been many times when the
omission of the L was not caught until the testing phase, which
means automatic conversion would have been prefered.

Case #2 has only occured once, where >98% of the times the
value could fit in a regular integer and I wanted the
faster math performance.  In that case I did
  try:
    operation
  except OverflowError:
    redo the operation with longs

So I have have no problems with the current 237 PEP, where
int operations convert to long rather than raising an exception.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list