[Math] way to calculate decimal powers using only addition, subtraction, multiplication and division

exponentiationintegers

I am a programmer who is trying to build an arbitrary precision decimal class library for C# and was successful in implementing all the basic operations like addition, subtraction, multiplication and division but was stopped when trying to calculate powers to decimal (and negative decimal) exponents. Calculating Log and Ln was also not possible since the depend on calculating powers.

So here is what I CAN do (with arbitrary accuracy):

  • Calculate the result of ALL operators if both numbers are integers.

  • Calculate the result of addition, subtraction, multiplication and division if the numbers are mixed (decimals and integers).

  • Calculate the result of the power of a decimal if the exponent is integer.

Here is what I CAN'T do (and I hope to find the answer here):

  • Calculate the power of decimals or integers to fractional, negative or decimal exponents.

Some (programmers) might point out that I can use functions already in C# to calculate these but this would not work as I need to build the library to be of arbitrary precision so I have to do the calculation manually.

So, is there a way to do it with the current "tools" I have? Iterative methods are not a problem since I will not be calculating the powers by hand, the CPU will be doing the counting.

Best Answer

The simplest way would be to implement log and exp via Taylor series (using the Taylor remainder theorem in order to bound the error to the desired precision) and then just rewrite

$$a^b = \exp(b \log a)$$

However, this is probably not the ideal way of doing things. For instance, you could use the Sasaki-Kanada algorithm to obtain the logarithm.

A lot of this comes down to the kind of tradeoffs you're trying to make. Do you just want something that kind of functions, or do you want to be able to make guarantees about the accuracy of your calculations? Do you need speed, or can it take all the time it wants? Is there a solid reason not to use existing libraries for arbitrary-precision arithmetic, and if so what is it?

There's an entire field of mathematics studying these questions, namely numerical analysis, and many of these problems have been solved (or at least have best-practice solutions). But there's often no simple, one-size-fits-all answer in a lot of cases.

To maybe illustrate a bit of the complexity here: given numbers $a$ and $b$, we know that $a^b = e^{b \log a}$. But if you calculate $\log a$ to a particular precision, and then calculate the product with $b$ to the same precision, and finally exponentiate that to the same precision, then you will not have calculated $a^b$ to that precision -- every time you do an arithmetic calculation you lose a bit of accuracy, and this can compound dramatically.

Related Question