| 914 | } |
| 915 | |
| 916 | func (d Decimal) powBigIntWithPrecision(exp *big.Int, precision int32) (Decimal, error) { |
| 917 | if d.IsZero() && exp.Sign() == 0 { |
| 918 | return Decimal{}, fmt.Errorf("cannot represent undefined value of 0**0") |
| 919 | } |
| 920 | |
| 921 | tmpExp := new(big.Int).Set(exp) |
| 922 | isExpNeg := exp.Sign() < 0 |
| 923 | |
| 924 | if isExpNeg { |
| 925 | tmpExp.Abs(tmpExp) |
| 926 | } |
| 927 | |
| 928 | n, result := d, New(1, 0) |
| 929 | |
| 930 | for tmpExp.Sign() > 0 { |
| 931 | if tmpExp.Bit(0) == 1 { |
| 932 | result = result.Mul(n) |
| 933 | } |
| 934 | tmpExp.Rsh(tmpExp, 1) |
| 935 | |
| 936 | if tmpExp.Sign() > 0 { |
| 937 | n = n.Mul(n) |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if isExpNeg { |
| 942 | return New(1, 0).DivRound(result, precision), nil |
| 943 | } |
| 944 | |
| 945 | return result, nil |
| 946 | } |
| 947 | |
| 948 | // ExpHullAbrham calculates the natural exponent of decimal (e to the power of d) using Hull-Abraham algorithm. |
| 949 | // OverallPrecision argument specifies the overall precision of the result (integer part + decimal part). |