Numerical trade-offs between 1 / sqrt (x) and std :: exp (-0.5 * std :: log (x))

I came across some old code that calculates

double y = 1 / std::sqrt(x);

Using:

constexpr double base16 = 16.0;
double log_base16 = std::log(base16);
double y = std::pow(base16, -0.5 * std::log(x) / log_base16);

Which is essentially:

double y = std::exp(-0.5 * std::log(x));

Are there any rationales for quantitative advantages (e.g. accuracy or, most likely, prevention of overflow / overflow) between the methods? Perhaps the author of the original.

+4
source share
2 answers

The source code should be considered very mischievous, especially in modern C ++ standards and the IEEE754 floating point:

std :: sqrt is required by the IEEE standard. [So in the original.]

In addition, it std::powdoes not have such requirements.

1 / std::sqrt(x), , .

: http://en.cppreference.com/w/cpp/numeric/math/sqrt

+2

sqrt() pow() log(). , sqrt(), .

, , .

0

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


All Articles