Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was thinking how I could program powers into my application. And I want to do this without using the default math libraries. I want to use only +,-,* and /. So I wondered what is the definition of a power. I came across this definition of $a^{b}$:

$$ a^b = e^{b \ln a} = \sum_{n=0}^\infty \frac{(b \ln a)^n}{n!} $$

The thing here, is that to calculate a power, you'd have to use a power. This seems kind of odd to me because that would make it impossible to program a power. So is there a way to calculate a power without using a power?

It's not as simple as $x*x*x$ since I want to calculate powers like $2^{-3,29032}$.

EDIT: I just finished to code, calculating it the long way and I came across the infamous x^0. What should I do now, just put in 1?

share|cite|improve this question
Note that the powers in the formula you linked to are all integer powers which you can calculate straightforwardly. Of course this still leaves you with the infinite sum and calculation of $\ln(a)$ to worry about. – Shard Jan 17 '13 at 18:32
And what is the definition of ln(x) then? – Ruben Jan 17 '13 at 18:40
Since it seems you are looking for a series expansion of a power and working with the above definition you gave in your question, you can express the natural log for small $x$ as $$ \ln(1+x)=\sum_{n=1}^\infty \frac{(-1)^{n+1}}{n} x^n = x - \frac{x^2}{2} + \frac{x^3}{3} - \cdots \quad{\rm for}\quad \left|x\right| \leq 1,\quad $$ I doubt this is how you numerically compute $a^b$ in practice though. – Hooked Jan 17 '13 at 18:47
No, $e^x$ is done from the rapidly converging power series. – Robert Israel Jan 17 '13 at 19:25
@5PM I think the last term you listed is supposed to be $2x^5/5$. – Hooked Jan 17 '13 at 19:26
up vote 1 down vote accepted

Given any $\alpha\in{\mathbb C}$ one has the binomial series, giving the value of $$(1+x)^\alpha:=\exp\bigl(\alpha\log(1+x)\bigr)$$ without taking recourse to any tables: $$(1+x)^\alpha=\sum_{k=0}^\infty {\alpha\choose k}\>x^k\qquad(-1<x<1)\ .$$ Here $${\alpha\choose k}:={\alpha(\alpha-1)\cdots(\alpha-k+1)\over k!}$$ is a generalized binomial coefficient.

If you want $y^\alpha$ for a given $y>0$ let $x:=1-y$ when $y<1$, and let $x:=-{y-1\over y}$ when $y>1$, then take the reciprocal of the result.

share|cite|improve this answer

As you have discovered, the definition for general powers are

$$ a^b = e^{b \ln a} = \sum_{n=0}^\infty \frac{(b \ln a)^n}{n!} $$

It is much easier to define for integral powers, $b \in \mathbb{Z}$, where

$$ \begin{alignat*}{4} a^0 & = 1, \\ a^b & = a \cdot a^{b-1} &\quad& \text{when } b > 0, \\ a^b & = \frac{1}{a^{|b|}} && \text{when } b < 0. \end{alignat*} $$

Since the definition for general powers only makes use of the definition of integral powers, which in turn only use multiplication, then you have reduced the problem to calculate a sum.

For quick calculation of integral powers, you might wish to refer to this page.

As for calculating the sum, since $n!$ grows so quickly, you can approximate it by

$$ a^b = e^{b \ln a} \approx \sum_{n=0}^{100} \frac{(b \ln a)^n}{n!} $$ since after $n=100$ the change is unlikely to be representable for computers.

Primary source: Wikipedia article on the exponential function.

share|cite|improve this answer

If you only want to do integer powers, you may use iterated multiplication ($x^3 = x * x * x$), and for negative powers $x^{-n}$, you may calculate $x^n$, then take the reciprocal $$\frac{1}{x^n}= x^{-n}$$This should be much easier than programming natural logarithms or infinite sums.

As an added bonus, you could use a little more memory and save some computation time by finding the powers $x, x^2, x^4, \ldots, x^{\log_2(n)}$, then use these as your building blocks to achieve exponentiation in $O(\log_2(n))$ time as opposed to $O(n)$.

share|cite|improve this answer
You can do even better than $\log_2(n)$ by using the shortest addition chain (assuming you know the chain in the first place!). – Hooked Jan 17 '13 at 19:04
But if you're using a power series, you'll probably need all the positive integer powers (up to whatever point you want to stop at), so there you might as well use $x^{n+1} = x x^n$. – Robert Israel Jan 17 '13 at 19:18
@RobertIsrael That's why I'm saying as long as the OP is using integral powers, he might as well not go through all that extra work. – andybenji Jan 17 '13 at 19:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.