I'm trying to implement the exponentiation of numbers in my Scheme interpreter. I'm wondering how one Scheme implementation (Kawa) implemented this equation:


Kawa give this output:


My implementation (same as few others) return:


I was checking wolfram Alpha and it gives the same output. How to calculate this value to not get floating pointer number? Is there a special case that need to be handled? How to calculate the value to get such even number?

This is the equation I'm using for calculating of exponents taken from Wikipedia (de Moivre's formula):

$ z^{n}=(r(\cos \varphi + i\sin \varphi ))^n = r^n (\cos n\varphi + i \sin n \varphi) $

The problem is that $\varphi$ and $ r $ are real numbers the same as a result of $cos$ and $sin$. There has to be a different equation to evaluate the complex number to the power of an integer.

I was searching the Kawa source code but was not able to find anything. So hoping someone here will know the answer.

What is the equation that will give me an integer value like this? And when it should be used (what rules are required)?

EDIT: I think that this happens when both imaginary and real parts are the same integer.

  • 1
    $\begingroup$ you can use the exponentiation trick used in the powermod algorithm; here, with $z = a+bi$ and $n>0$ an integer, find $z^n$ with successive steps, at current $w$ either take $w^2$ or take $zw.$ Involves writing $n$ in binary and reversing the digits... en.wikipedia.org/wiki/Modular_exponentiation $\endgroup$
    – Will Jagy
    Commented Feb 11 at 22:40
  • $\begingroup$ Matlab computes $(10+10i)^{10}=3.2\cdot10^{11}i$. So does WolframAlpha. $\endgroup$ Commented Feb 13 at 16:46
  • $\begingroup$ @CyeWaldman and? This is the same as $+320000000000i$ $\endgroup$
    – jcubic
    Commented Feb 13 at 17:58
  • $\begingroup$ @jcubic I thought that you were looking for verification of that, that's all. $\endgroup$ Commented Feb 14 at 1:07
  • $\begingroup$ @CyeWaldman now I was looking, how to get this result. $\endgroup$
    – jcubic
    Commented Feb 14 at 14:53

1 Answer 1


I recently encountered the same issue in my exponentiation, and solved it through recursion. If you have (a+bi)^c, where c is some integer greater than 1, and you don't want to run into float precision errors, simply restate it as (a+bi)(a+bi)^(c-1), and do it recursively until the exponent is 1. Your multiplication code should not have issues with precision.

  • $\begingroup$ Thanks that is a really good idea. And I don't even need to use recursion I can use a loop. $\endgroup$
    – jcubic
    Commented Feb 11 at 22:50
  • $\begingroup$ I'm glad I could help! Also you're right, and a loop would be more versatile here. I might want to change mine in fact $\endgroup$ Commented Feb 11 at 23:01
  • 1
    $\begingroup$ If your integer exponent is large, use Will Jagy's suggestion which will reduce the number of operations from n to log_2(n). $\endgroup$ Commented Feb 11 at 23:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .