-
Notifications
You must be signed in to change notification settings - Fork 1
/
divide.go
33 lines (30 loc) · 689 Bytes
/
divide.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
31
32
33
package dividetwointegers
import "math"
func divide(dividend int, divisor int) int {
if divisor == 0 ||
(dividend == math.MinInt32 && divisor == -1) { // -INT_MIN = INT_MAX + 1
return math.MaxInt32
} else if dividend == 0 {
return 0
}
result := 0
for den, sor := abs(dividend), abs(divisor); den >= sor; {
temp, quadratics := sor, 1 // temp = abs(divisor)
for den >= (temp << 1) { // ensure den >= 0
temp <<= 1 // temp *= 2
quadratics <<= 1 // quadratics *=2
}
den -= temp
result += quadratics
}
if (dividend >= 0) != (divisor >= 0) {
return -result
}
return result
}
func abs(x int) int64 {
if x < 0 {
return int64(-x)
}
return int64(x)
}