MCPcopy
hub / github.com/ethereum/EIPs / Exp

Method Exp

assets/eip-7543/decimal_float.go:96–122  ·  view source on GitHub ↗

e^a

(_a *Decimal, precision, steps *int256, gas *uint64)

Source from the content-addressed store, hash-verified

94
95// e^a
96func (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
125func (out *Decimal) Ln(_a *Decimal, precision, steps *int256, gas *uint64) *Decimal {

Callers 6

TestDecExpFunction · 0.95
DecExpFunction · 0.80
ExpFunction · 0.80
TestDecNormalizeFunction · 0.80
RunMethod · 0.80
RunMethod · 0.80

Calls 9

MulMethod · 0.95
AddMethod · 0.95
InvMethod · 0.95
copyDecimalFunction · 0.85
SetFunction · 0.85
NewFunction · 0.85
CmpFunction · 0.85
AddFunction · 0.85
isZeroMethod · 0.80

Tested by 2

TestDecExpFunction · 0.76
TestDecNormalizeFunction · 0.64