Home > Back-end >  What is the fastest way to power real numbers?
What is the fastest way to power real numbers?

Time:10-01

Of course i know that there is a good pow() function in cmath(math.h), but not touching the background of the pow() what is the fastest way to power numbers with my own hands?

CodePudding user response:

You are asking two different questions:

  • What is the fastest way to power real numbers?
  • What is the fastest way to power numbers with my own hands?

These have different answers.

pow is fast. Standard library implementations are generally written by very smart people, reviewed by other smart people, and then refactored by even smarter people. Because of this, using a provided standard library implementation is almost always better than trying to re-implement the standard library yourself.

But, if you insist on creating your own implementation of pow, you should first implement exp and log using their Taylor series expansions. Then use the following property:

pow(base,power) = exp( power * log(base) )

Note that if base is negative, you should first compute pow(-base,power), then do a parity check on base to determine the sign of the result.

  • Related