Why doesn’t this code use the C++ power function directly? Can someone help me with the power function in this code? [closed]

This function uses a technique called exponentation by squaring.

It’s a particularly efficient way of evaluating the power for integral type arguments. The standard C function uses floating point arguments, and the C standard doesn’t require an exact result even if the floating point arguments represent whole numbers.

In C++ though you can probably rely on one of the overloads of std::pow that takes integral type arguments, and cast the result, subject to your making the necessary size checks. But again even the C++ standard does not require that the best possible result is returned (cf. std::sqrt under IEEE754), although one could reasonably regard a std::pow function that does not return the correct result for integral arguments to be defective.

Leave a Comment