std::pow with int exponent

It seems that an argument for the exponent is cast to double in this function. Does that mean it will do a binomial expansion as might be expected for an irrational double?

Or does it just do multiplication when the exponent is integral?

Edit:

I am hoping it does the latter, as that would be much more efficient in that particular case.

I will have a go at testing this with code - only asked because I thought someone might know straight away.

Cheers
Last edited on
> It seems that an argument for the exponent is cast to double in this function.

If the first argument has type long double, the second integer argument is cast to long double and the result is of type long double.

Otherwise, both arguments are (effectively) cast to double and the result is of type double.


> Does that mean it will do a binomial expansion as might be expected for an irrational double?
> Or does it just do multiplication when the exponent is integral?

The standard does not specify the algorithm; that is left to the implementation.
Hi & thanks for your reply.

I guess I could go with the idea that whatever is implemented in the std library, is going to be better than what anyone else might typically implement, however I am curious, and I have the time :+)

I wrote my own very simple function that does multiplication, so if that was shown to be faster, then I guess it's pretty easy to just do that.

I am using some code of yours from Nov 4 2014 utilising std::steady_clock to see if it is worth employing my simple function.

Regards


> whatever is implemented in the std library, is going to be better than what anyone else might typically implement

This need not necessarily be true. std::pow has to take care of all edge-cases as required by the standard ( Error handling: http://en.cppreference.com/w/cpp/numeric/math/pow ), the code may be written with the idea of portability (it may not exploit every feature in every processor architecture), and so on.

For instance: https://software.intel.com/en-us/intel-mkl/benchmarks#VML exp


> utilising std::steady_clock to see if it is worth employing my simple function.

std::chrono::steady_clock is a is a monotonic clock that measures wall clock time.

std::clock() measures approximate processor time used by the process.

See the discussion in this thread: http://www.cplusplus.com/forum/beginner/146620/


Performance of std::pow would typically depend on the options used for compiling the code: whether SSE is enabled, should the compiler generate intrinsics etc.
Last edited on
All right, thanks again for your always outstanding answers :+D
Topic archived. No new replies allowed.