MCPcopy
hub / github.com/shopspring/decimal / Pow

Method Pow

decimal.go:698–764  ·  view source on GitHub ↗

Pow returns d to the power of d2. When exponent is negative the returned decimal will have maximum precision of PowPrecisionNegativeExponent places after decimal point. Pow returns 0 (zero-value of Decimal) instead of error for power operation edge cases, to handle those edge cases use PowWithPreci

(d2 Decimal)

Source from the content-addressed store, hash-verified

696// res2 := d3.Pow(d4)
697// res2.String() // output: "10118.08037125"
698func (d Decimal) Pow(d2 Decimal) Decimal {
699 baseSign := d.Sign()
700 expSign := d2.Sign()
701
702 if baseSign == 0 {
703 if expSign == 0 {
704 return Decimal{}
705 }
706 if expSign == 1 {
707 return Decimal{zeroInt, 0}
708 }
709 if expSign == -1 {
710 return Decimal{}
711 }
712 }
713
714 if expSign == 0 {
715 return Decimal{oneInt, 0}
716 }
717
718 // TODO: optimize extraction of fractional part
719 one := Decimal{oneInt, 0}
720 expIntPart, expFracPart := d2.QuoRem(one, 0)
721
722 if baseSign == -1 && !expFracPart.IsZero() {
723 return Decimal{}
724 }
725
726 intPartPow, _ := d.PowBigInt(expIntPart.value)
727
728 // if exponent is an integer we don't need to calculate d1**frac(d2)
729 if expFracPart.value.Sign() == 0 {
730 return intPartPow
731 }
732
733 // TODO: optimize NumDigits for more performant precision adjustment
734 digitsBase := d.NumDigits()
735 digitsExponent := d2.NumDigits()
736
737 precision := digitsBase
738
739 if digitsExponent > precision {
740 precision += digitsExponent
741 }
742
743 precision += 6
744
745 // Calculate x ** frac(y), where
746 // x ** frac(y) = exp(ln(x ** frac(y)) = exp(ln(x) * frac(y))
747 fracPartPow, err := d.Abs().Ln(-d.exp + int32(precision))
748 if err != nil {
749 return Decimal{}
750 }
751
752 fracPartPow = fracPartPow.Mul(expFracPart)
753
754 fracPartPow, err = fracPartPow.ExpTaylor(-d.exp + int32(precision))
755 if err != nil {

Callers 2

BenchmarkDecimal_PowFunction · 0.80
TestDecimal_PowFunction · 0.80

Calls 9

SignMethod · 0.95
PowBigIntMethod · 0.95
NumDigitsMethod · 0.95
AbsMethod · 0.95
QuoRemMethod · 0.80
IsZeroMethod · 0.80
LnMethod · 0.80
MulMethod · 0.80
ExpTaylorMethod · 0.80

Tested by 2

BenchmarkDecimal_PowFunction · 0.64
TestDecimal_PowFunction · 0.64