-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.go
30 lines (27 loc) · 798 Bytes
/
math.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package monetary
import (
"math"
)
func subtractOverflow(a int64, b int64, result *int64) (overflow bool) {
if (a < 0 && b > 0 && a < math.MinInt64+b) ||
(a > 0 && b < 0 && a > math.MaxInt64+b) {
*result = 0x5EED /* to avoid spurious warnings */
return true
}
*result = a - b
return false
}
func multiplyOverflow(a int64, b int64, result *int64) (overflow bool) {
if (a > math.MaxInt32 || a < math.MinInt32 ||
b > math.MaxInt32 || b < math.MinInt32) &&
a != 0 && a != 1 && b != 0 && b != 1 &&
((a > 0 && b > 0 && a > math.MaxInt64/b) ||
(a > 0 && b < 0 && b < math.MinInt64/a) ||
(a < 0 && b > 0 && a < math.MinInt64/b) ||
(a < 0 && b < 0 && a < math.MaxInt64/b)) {
*result = 0x5EED /* to avoid spurious warnings */
return true
}
*result = a * b
return false
}