...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package expr
18
19 import (
20 `fmt`
21 )
22
23 func idiv(v int64, d int64) (int64, error) {
24 if d != 0 {
25 return v / d, nil
26 } else {
27 return 0, newRuntimeError("division by zero")
28 }
29 }
30
31 func imod(v int64, d int64) (int64, error) {
32 if d != 0 {
33 return v % d, nil
34 } else {
35 return 0, newRuntimeError("division by zero")
36 }
37 }
38
39 func ipow(v int64, e int64) (int64, error) {
40 mul := v
41 ret := int64(1)
42
43
44 if v < 0 {
45 return 0, newRuntimeError(fmt.Sprintf("negative base value: %d", v))
46 }
47
48
49 if e < 0 {
50 return 0, newRuntimeError(fmt.Sprintf("negative exponent: %d", e))
51 }
52
53
54 if (e & 1) != 0 {
55 ret *= mul
56 }
57
58
59 for e >>= 1; e != 0; e >>= 1 {
60 if mul *= mul; (e & 1) != 0 {
61 ret *= mul
62 }
63 }
64
65
66 return ret, nil
67 }
68
View as plain text