21

What's the best way to do integer math in 32- and 64-bit, so that overflow happens like it does in C?

e.g. (65536*65536+1)*(65536*65536+1) should be 0x0000000200000001 in 64-bit math, and not its exact value (non-overflowing) 0x10000000200000001.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • 3
    Signed overflow causes undefined behaviour in C, so strictly speaking the question is meaningless. – Cairnarvon May 25 '13 at 01:22
  • ^ yet another reason C isn't helpful for certain types of numerical computation. :-( – Jason S May 25 '13 at 04:32
  • Similar, but not a duplicate. The 32-bit question (or 16-bit, which I didn't ask about) is different from the int -> long int behavior. – Jason S Jul 27 '15 at 13:42

2 Answers2

33

Just & the result with the appropriate 32- or 64-bit mask (0xffffffff or 0xffffffffffffffff).

martineau
  • 119,623
  • 25
  • 170
  • 301
  • With signed int you have to use modulo e.g. `% 0x100000000` or `% 0x10000000000000000`. Unfortunately this is not really efficient. Neither is the bitmask when compared to any language that supports native processor sized integers. NumPy exposes them so its probably the best solution still... – Gregory Morse Mar 16 '20 at 12:16
16

Use NumPy with the appropriate integer size and the overflow is more C like:

32 bit:

>>> np.uint32(2**32-3) + np.uint32(5)
__main__:1: RuntimeWarning: overflow encountered in uint_scalars
2

64 bit:

>>> i64=np.uint64(65536*65536+1)
>>> hex(i64*i64)
'0x200000001L'

Compare with Python's native int:

>>> hex((65536*65536+1)*(65536*65536+1))
'0x10000000200000001L'

You can see that NumPy is doing as you desire.

Jens
  • 8,423
  • 9
  • 58
  • 78
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Can NumPy omit the warnings though? This is probably the best solution as it would be the fastest and deals with signed/unsigned rather than hacking it with bit masks or modulo over in the Python native bigint side of things. – Gregory Morse Mar 16 '20 at 12:18
  • Yes, you can catch or suppress the errors and warnings. You can do that with the [warnings module](https://docs.python.org/2/library/warnings.html#warnings.filterwarnings) or with multiple numpy options. – dawg Mar 16 '20 at 15:16