Programming-Idioms

History of Idiom 86 > diff from v7 to v8

Edit summary for version 8 :
New Go implementation by user [Logiraptor]

Version 7

2016-02-07, 17:21:20

Version 8

2016-02-17, 04:20:32

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
}
Origin
http://grokbase.com/p/gg/golang-nuts/148wvnxk76/go-nuts-re-test-for-an-integer-overflow