Skip to content

Commit

Permalink
math: correct Pow(..., x, 0.5) (#147)
Browse files Browse the repository at this point in the history
Fixes #146
  • Loading branch information
ericlagergren authored Oct 18, 2019
1 parent 2c3e3e1 commit 98d6b4c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions math/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,26 @@ package math_test
import (
"testing"

"github.com/ericlagergren/decimal"
"github.com/ericlagergren/decimal/internal/test"
"github.com/ericlagergren/decimal/math"
)

func TestExp(t *testing.T) { test.Exp.Test(t) }
func TestLog(t *testing.T) { test.Log.Test(t) }
func TestLog10(t *testing.T) { test.Log10.Test(t) }
func TestPow(t *testing.T) { test.Pow.Test(t) }
func TestSqrt(t *testing.T) { test.Sqrt.Test(t) }

func TestIssue146(t *testing.T) {
one := decimal.New(1, 0)
for i := int64(0); i < 10; i++ {
n := decimal.New(i, 1)
firstPow := math.Pow(new(decimal.Big), n, one)
if n.Cmp(firstPow) != 0 {
t.Errorf("%v^1 != %v", n, firstPow)
} else {
t.Logf("%v^1 == %v", n, firstPow)
}
}
}
2 changes: 1 addition & 1 deletion math/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Pow(z, x, y *decimal.Big) *decimal.Big {
return z.SetUint64(1)
}

if x.Cmp(ptFive) == 0 {
if y.Cmp(ptFive) == 0 {
// x ** 0.5 = sqrt(x)
return Sqrt(z, x)
}
Expand Down

0 comments on commit 98d6b4c

Please sign in to comment.