c++ - Why was std::pow(double, int) removed from C++11? -
while looking efficient way compute p^q (exponentiation), q integer , reviewing c++98 , c++11 standards noticed apparently std::pow(double, int) overload removed in c++11.
in c++98 26.5/6 has double pow(double, int); signature.
in c++11 26.8 find overloads taking pair of float, double, or long double, , explicit note in case of mixture of parameter types integral&double, pow(double, double) overload should picked.
is clarification of previous intention, incorrectly added in c++98, removed in c++11, or else?
obviously pow(double, int) version provides nice opportunity optimization seems odd removed. compiler still standards conforming provide such optimized overload?
double pow(double, int); hasn't been removed spec. has been reworded. lives in [c.math]/p11. how computed implementation detail. c++03 signature has changed is:
float pow(float, int); this returns double:
double pow(float, int); and this change done c compatibility.
clarification:
26.8 [cmath] / p11 says:
moreover, there shall additional overloads sufficient ensure:
if argument corresponding double parameter has type long double, arguments corresponding double parameters cast long double.
otherwise, if argument corresponding double parameter has type double or integer type, arguments corresponding double parameters cast double.
otherwise, arguments corresponding double parameters cast float.
this paragraph implies whole host of overloads, including:
double pow(double, int); double pow(double, unsigned); double pow(double, unsigned long long); etc.
these may actual overloads, or may implemented restricted templates. i've implemented both ways , favor restricted template implementation.
second update address optimization issues:
the implementation allowed optimize overload. recall optimization should only that. optimized version ought return same answer. experience implementors of functions pow time go trouble ensure implementation taking integral exponent gives same answer implementation taking floating point exponent, "optimization" slower.
as demonstration following program prints out pow(.1, 20) twice, once using std::pow, , second time using "optimized" algorithm taking advantage of integral exponent:
#include <cmath> #include <iostream> #include <iomanip> int main() { std::cout << std::setprecision(17) << std::pow(.1, 20) << '\n'; double x = .1; double x2 = x * x; double x4 = x2 * x2; double x8 = x4 * x4; double x16 = x8 * x8; double x20 = x16 * x4; std::cout << x20 << '\n'; } on system prints out:
1.0000000000000011e-20 1.0000000000000022e-20 or in hex notation:
0x1.79ca10c92422bp-67 0x1.79ca10c924232p-67 and yes, implementors of pow worry of bits down @ low end.
so while freedom there shuffle pow(double, int) off separate algorithm, implementors i'm aware of have given on strategy, possible exception of checking small integral exponents. , in event, advantageous put check in implementation floating point exponent biggest bang optimization buck.
Comments
Post a Comment