mirror of
https://github.com/shopspring/decimal.git
synced 2024-11-22 20:40:48 +01:00
add Pow()
This commit is contained in:
parent
d6f52241f3
commit
8a97b04143
2 changed files with 25 additions and 0 deletions
16
decimal.go
16
decimal.go
|
@ -301,6 +301,22 @@ func (d Decimal) Mod(d2 Decimal) Decimal {
|
|||
return d.Sub(d2.Mul(quo))
|
||||
}
|
||||
|
||||
// Pow returns d to the power d2
|
||||
func (d Decimal) Pow(d2 Decimal) Decimal {
|
||||
var temp Decimal
|
||||
if d2.IntPart() == 0 {
|
||||
return NewFromFloat(1)
|
||||
}
|
||||
temp = d.Pow(d2.Div(NewFromFloat(2)))
|
||||
if d2.IntPart()%2 == 0 {
|
||||
return temp.Mul(temp)
|
||||
}
|
||||
if d2.IntPart() > 0 {
|
||||
return temp.Mul(temp).Mul(d)
|
||||
}
|
||||
return temp.Mul(temp).Div(d)
|
||||
}
|
||||
|
||||
// Cmp compares the numbers represented by d and d2 and returns:
|
||||
//
|
||||
// -1 if d < d2
|
||||
|
|
|
@ -1025,6 +1025,15 @@ func TestDecimal_Cmp2(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestPow(t *testing.T) {
|
||||
a := New(4, 0)
|
||||
b := New(2, 0)
|
||||
x := a.Pow(b)
|
||||
if x.String() != "16" {
|
||||
t.Errorf("Error, saw %s", x.String())
|
||||
}
|
||||
}
|
||||
|
||||
func didPanic(f func()) bool {
|
||||
ret := false
|
||||
func() {
|
||||
|
|
Loading…
Reference in a new issue