2

Its available for me only log(base "e"), sin, tan and sqrt (only square root) functions and the basic arithmetical operators (+ - * / mod). I have also the "e" constant.

I'm experimenting several problems with Deluge (zoho.com) for these restrictions. I must implement exponentiation of rational (fraction) bases and exponents.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Alex
  • 3,325
  • 11
  • 52
  • 80
  • Your own answer to your other question was "e^(Log(number)/index)". So what's wrong with "e^(Log(number)*exponent)"? – Henrik Dec 14 '10 at 13:59
  • possible duplicate of [Floating exponent exponentiation algorithm](http://stackoverflow.com/questions/4432075/floating-exponent-exponentiation-algorithm) – Dr. belisarius Dec 14 '10 at 14:20
  • The prbolem is that I've discovered that I'm not able to use e^, because Deluge doesn't has a exp (e^) function. So, I cant' perform the algorithm. – Alex Dec 14 '10 at 15:05
  • You can use the Taylor series to compute `e^x`, though your fractions may explode in size. – starblue Dec 15 '10 at 19:46

2 Answers2

3

Say you want to calculate pow(A, B)

Consider the representation of B in base 2:

B = b[n]   * pow(2, n    ) +
    b[n-1] * pow(2, n - 1) +
    ...
    b[2]   * pow(2, 2    ) +
    b[1]   * pow(2, 1    ) +
    b[0]   * pow(2, 0    ) +
    b[-1]  * pow(2, -1   ) +
    b[-2]  * pow(2, -2   ) +
    ...

 = sum(b[i] * pow(2, i))

where b[x] can be 0 or 1 and pow(2, y) is an integer power of two (i.e., 1, 2, 4, 1/2, 1/4, 1/8).

Then,

pow(A, B) = pow(A, sum(b[i] * pow(2, i)) = mul(pow(A, b[i] * pow(2, i)))

And so pow(A, B) can be calculated using only multiplications and square root operations

salva
  • 9,943
  • 4
  • 29
  • 57
  • This assumes B is a diadic rational, or that its approximation using diadic rationals is accurate enough. If B is represented as an ordered pair of integers (e.g., (1,3) for 1/3), this may not be appropriate. – Josephine Dec 14 '10 at 14:28
  • 2
    @Josephine: IMO, in the context of programming, it is an acceptable assumption as it is the way computers represent numbers internally. If the OP wants to support other kind of numbers he should estate it explicitly. – salva Dec 14 '10 at 14:46
1

If you have a function F() that does e^x, where e is the constant, and x is any number, then you can do this: (a is base, b is exponent, ln is log-e)

a^b = F(b * ln(a))

If you don't have F() that does e^x, then it gets trickier. If your exponent (b) is rational, then you should be able to find integers m and n so that b = m/n, using a loop of some sort. Once you have m and n, you make another loop which multiples a by itself m times to get a^m, then multiples a by itself n times to get a^n, then divide a^m/a^n to get a^(m/n), which is a^b.

Mr. TA
  • 5,230
  • 1
  • 28
  • 35
  • 3
    a^m/a^n = a^(m-n), not a^(m/n). – Henrik Dec 14 '10 at 14:12
  • You're right. I haven't a F() function (like exp()) to do the hard work for me, unfortunately. If so, I should solve the problem wiht the nth root formula. Would you give me an example of the tricky way to do the solution? I'm not shure if I've understood. Supose 5^(0,5): its equivalent 5^(1/2) => 5/25 => 0.2. But the correct answer is 2.2360. Thanks! – Alex Dec 14 '10 at 17:39