Skip to content

Commit 98d6b4c

Browse files
math: correct Pow(..., x, 0.5) (#147)
Fixes #146
1 parent 2c3e3e1 commit 98d6b4c

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

math/math_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ package math_test
33
import (
44
"testing"
55

6+
"github.com/ericlagergren/decimal"
67
"github.com/ericlagergren/decimal/internal/test"
8+
"github.com/ericlagergren/decimal/math"
79
)
810

911
func TestExp(t *testing.T) { test.Exp.Test(t) }
1012
func TestLog(t *testing.T) { test.Log.Test(t) }
1113
func TestLog10(t *testing.T) { test.Log10.Test(t) }
1214
func TestPow(t *testing.T) { test.Pow.Test(t) }
1315
func TestSqrt(t *testing.T) { test.Sqrt.Test(t) }
16+
17+
func TestIssue146(t *testing.T) {
18+
one := decimal.New(1, 0)
19+
for i := int64(0); i < 10; i++ {
20+
n := decimal.New(i, 1)
21+
firstPow := math.Pow(new(decimal.Big), n, one)
22+
if n.Cmp(firstPow) != 0 {
23+
t.Errorf("%v^1 != %v", n, firstPow)
24+
} else {
25+
t.Logf("%v^1 == %v", n, firstPow)
26+
}
27+
}
28+
}

math/pow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func Pow(z, x, y *decimal.Big) *decimal.Big {
5454
return z.SetUint64(1)
5555
}
5656

57-
if x.Cmp(ptFive) == 0 {
57+
if y.Cmp(ptFive) == 0 {
5858
// x ** 0.5 = sqrt(x)
5959
return Sqrt(z, x)
6060
}

0 commit comments

Comments
 (0)