0

For my thesis, I have been looking for an equation that calculates an exp(x) with Vyper smart contract. I choose Vyper over Solidity for its ability to handle fixed-point numbers. However, I couldn't find an efficient to do this since Vyper doesn't allow me to do exponentiation with a decimal base.

What I was trying to do is e**x (e = constant Euler number = about 2.718281828. x is the variable of a decimal that can be negative or positive)

I tried looking for a Vyper math library that provides exponentiation function for decimals but with no luck. Someone somewhere suggested using look-up tables for exponential e to minimize computation time. However, I have no idea how can I implement that in Vyper.

I'm currently trying to develop an exponential function based on Taylor's series, https://en.wikipedia.org/wiki/Taylor_series.

Is this the only way to calculate this problem? I feel like there could be a better solution.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

What I have is a solution, but it is not perfect, and I haven't been able to get it to run reliably in Vyper yet. It might be an avenue that you can explore for your thesis though. What I had was based on this Stackoverflow answer:

https://ethereum.stackexchange.com/a/65854

My answer involves multiplying e by a scale factor and doing the exponentiation on that. The scale factor is the number of decimals that you want for precision. After the exponentiation is done on the scaled number, you then divide the result by a divisor scaled by an exponent. The numbers get large rather quickly, but this is the code I have (NOTE: code is in Python rather than Vyper):

import math

SCALE = 10
EXP = 3

eScaled = math.e * (10 ** SCALE)
eScaleDiv = (10 ** SCALE) ** EXP
ePowExpScaled = eScaled ** EXP

print(ePowExpScaled / eScaleDiv)
print(math.e ** EXP)