Programming-Idioms

History of Idiom 86 > diff from v10 to v11

Edit summary for version 11 :
[Go] Comments: unsigned, multiplication performed.

Version 10

2016-02-21, 01:35:54

Version 11

2016-02-21, 17:17:55

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Idiom #86 Check if integer multiplication will overflow

Write boolean function multiplyWillOverflow which takes two integers x, y and return true if (x*y) overflows.

Code
func mulOverflows(a, b uint64) bool {
   if a <= 1 || b <= 1 {
     return false
   }
   c := a * b
   return c/b != a
}
Code
func mulOverflows(a, b uint64) bool {
   if a <= 1 || b <= 1 {
     return false
   }
   c := a * b
   return c/b != a
}
Comments bubble
This holds for uint64, not for signed integers.
Note that the multiplication is performed, then its result is checked.
Origin
http://grokbase.com/p/gg/golang-nuts/148wvnxk76/go-nuts-re-test-for-an-integer-overflow
Origin
http://grokbase.com/p/gg/golang-nuts/148wvnxk76/go-nuts-re-test-for-an-integer-overflow