Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do power functions run in constant time?

When I use power functions like Math.Pow(double x, double y) in C# or the math.h pow-function in C++ do these run in constant time?

The reason I am asking is because I want to know if a "precalculated" bézier-function on the form (1-t)^n*p0 + ... + t^(n) * pN could run in linear time which could then be faster than an implementation of De Casteljaus algorithm taking control points and t as parameters.

like image 281
Mikael Högström Avatar asked Nov 03 '22 15:11

Mikael Högström


1 Answers

I think that these methods use iteration based processing to get the result, and only stop when the difference between the value of two iterations fall bellow a given error-constant.

There are iterative methods that converge very fastly to the result of a power operation... so I think they are near constant time.

This question has a lot of great explanations: How is Math.Pow() implemented in .NET Framework?

EDIT

I have found a lot of good meterial to work with in http://math.stackexchange.com.

  • Fastest way to calculate e^x upto arbitrary number of decimals?
  • How to calculate e^x with a standard calculator
  • How can I calculate non-integer exponents?
  • Numerically estimate a^b
  • Approximation of e^(−x)

This one is very interesting, as it explains a way of calculating exponentiation using human language:

  • How to Use a Fixed Point to Calculate an Exponentiation

Thoughts

I'm not a math genius, but as far as I can see, the time it takes does not depend a lot on the values you choose, but on the number of precise digits you want. What I'm trying to say is that it depends on the arguments, but there is a maximum.

Also, to support this theory, take a look at this algorithm (implemented by Sun): http://pastebin.com/LDjS5mAR. There is no loops, only ifs. I think that it is because the guys who implemented it, chose a fixed precision they wanted... and then expanded all the iterations needed to guarantee that precision.

For instance, a loop of invariant number of iterations can be easyly expanded like this:

for (int it = 0; it < 5; it++)
    a *= a;

Is the same as:

a *= a; a *= a; a *= a; a *= a; a *= a;
like image 152
Miguel Angelo Avatar answered Nov 14 '22 11:11

Miguel Angelo