Pow () from math.h library - How to apply using functions

So, I am writing some code that should increase the value of the returned function to a certain power. I recently discovered that using the β€œ^” operator to exponentiate is useless, because in C ++ it is actually an XOR operator or something like that. Now here is the code I want to write:

int answer = pow(base, raisingTo(power)); 


Now can someone tell me if this is correct? I will explain the code. I declared an int-variable answer, as you all know, and initialized it with the value of any variable called the "base" raised to the return value of the raiseTo () function acting on any other variable called "power". When I do this (and I edit and compile my code in Visual C ++ 2010 Express Edition), a red line appears under the word "pow", and an error appears: "more than one instance of the overloaded function" pow "corresponds to the argument list"

Can anyone solve this problem for me? And could you guys also explain to me how this whole pow () function works, because frankly the links to www.cplusplus.com are a bit confusing, since I'm still just a beginner!

+4
source share
2 answers

The documentation states that it is explicitly explicit:

Overloading pow(int, int) no longer available. If you use this overload, the compiler may emit C2668 [ EDIT : this error] . To avoid this problem, enter the first parameter in double, float or long double.

In addition, to calculate the base power power you just write

 pow(base, power) 

And with the above hint:

 int result = (int) pow((double)base, power); 
+6
source

The pow () function returns either double or float , so the first step would be to change the answer to one of them. Secondly, return raisingTo() . If you are not doing something that is not obvious, you do not need it, but it should work anyway. In addition, both arguments must be doubles , according to this .

0
source

Source: https://habr.com/ru/post/1397235/


All Articles