Std :: pow with integer parameters compared to integer type

According to http://en.cppreference.com/w/cpp/numeric/math/pow , when std::powused with integer parameters, the result is raised to double.

My question is this:

How safe is it to compare an integer type with a result std::pow(int1, int2)? For example, can ifevaluate true?

std::size_t n = 1024;
if(n != std::pow(2, 10))
    cout << "Roundoff issues..." << endl;

That is, is it possible that the result on rhs can be approximately the same as 1023.99 ... 9, so when converting to size_t it becomes 1023?

I guess the answer is big NO, but I would like to know for sure. I use such comparisons when checking matrix sizes, etc., and I would not want to use it std::roundeverywhere.

+4
source share
4 answers

It’s funny to ask because fooobar.com/questions/261304 / ... there was a question caused by the fact that pow, applied to small integers, did not calculate the obvious result on their platform (see also my writeup ).

, , pow , . exp , , pow ULP. pow, pow(10, 2) 100, pow(2, N), , , , .

+4

pow , , . , . ( Linux, , , ), . SO, pow , .

+3

false, , int, .

, , , . - .

0

, , .

, const, :

std::pow(2, 10)

, , gcc, clang , , , - . godbolt, gcc :

movl    $1024, %esi     

, , , C: pow (10,2) pow (10, j), j = 2;.

0

All Articles