e^a
(_a *Decimal, precision, steps *int256, gas *uint64)
| 94 | |
| 95 | // e^a |
| 96 | func (out *Decimal) Exp(_a *Decimal, precision, steps *int256, gas *uint64) *Decimal { |
| 97 | a := copyDecimal(_a, gas) // in case out == _a |
| 98 | |
| 99 | // out = 1 |
| 100 | Set(ONE_INT256, &out.c, gas) |
| 101 | Set(ZERO_INT256, &out.q, gas) |
| 102 | |
| 103 | if a.isZero(gas) { |
| 104 | return out |
| 105 | } |
| 106 | |
| 107 | var factorial_inv Decimal |
| 108 | a_power := copyDecimal(ONE_DECIMAL, gas) |
| 109 | factorial := copyDecimal(ONE_DECIMAL, gas) |
| 110 | factorial_next := copyDecimal(ZERO_DECIMAL, gas) |
| 111 | |
| 112 | for i := New(1, gas); Cmp(steps, i, gas) == -1; Add(i, ONE_INT256, i, gas) { // step 0 skipped as out set to 1 |
| 113 | a_power.Mul(a_power, a, precision, gas) // a^i |
| 114 | factorial_next.Add(factorial_next, ONE_DECIMAL, precision, gas) // i++ |
| 115 | factorial.Mul(factorial, factorial_next, precision, gas) // i! |
| 116 | factorial_inv.Inv(factorial, precision, gas) // 1/i! |
| 117 | factorial_inv.Mul(&factorial_inv, a_power, precision, gas) // store a^i/i! in factorial_inv as not needed anymore |
| 118 | out.Add(out, &factorial_inv, precision, gas) // out += a^i/i! |
| 119 | } |
| 120 | |
| 121 | return out |
| 122 | } |
| 123 | |
| 124 | // 0 < _a |
| 125 | func (out *Decimal) Ln(_a *Decimal, precision, steps *int256, gas *uint64) *Decimal { |