Skip to content

Commit 383daa9

Browse files
author
dengsgo
committed
修复 pow
1 parent 7e163fb commit 383daa9

File tree

2 files changed

+4
-25
lines changed

2 files changed

+4
-25
lines changed

engine/util.go

+3-25
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,8 @@ func ErrPos(s string, pos int) string {
4444
}
4545

4646
// the integer power of a number
47-
func Pow(x float64, n int) float64 {
48-
if x == 0 {
49-
return 0
50-
}
51-
r := calPow(x, n)
52-
if n < 0 {
53-
r = 1 / r
54-
}
55-
return r
56-
}
57-
58-
func calPow(x float64, n int) float64 {
59-
if n == 0 {
60-
return 1
61-
}
62-
if n == 1 {
63-
return x
64-
}
65-
r := calPow(x, n>>1) // move right 1 byte
66-
r *= r
67-
if n&1 == 1 {
68-
r *= x
69-
}
70-
return r
47+
func Pow(x float64, n float64) float64 {
48+
return math.Pow(x, n)
7149
}
7250

7351
func expr2Radian(expr ExprAST) float64 {
@@ -135,7 +113,7 @@ func ExprASTResult(expr ExprAST) float64 {
135113
case "%":
136114
return float64(int(l) % int(r))
137115
case "^":
138-
return Pow(l, int(r))
116+
return Pow(l, r)
139117
default:
140118

141119
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func exec(exp string) {
5050
fmt.Println("ERROR: " + err.Error())
5151
return
5252
}
53+
5354
// []token -> AST Tree
5455
ast := engine.NewAST(toks, exp)
5556
if ast.Err != nil {

0 commit comments

Comments
 (0)