According to http://en.cppreference.com/w/cpp/numeric/math/pow , when std::pow
is used with integer parameters, the result is promoted to a double
.
My question is then the following:
How safe is to compare an integer type with the result of a std::pow(int1, int2)
?
For example, can the if
below evaluate to 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 the rhs can be something like 1023.99...9 so when converted to size_t becomes 1023?
My guess is that the response in a big NO, but would like to know for sure. I am using these kind of comparisons when checking for dimensions of matrices etc, and I wouldn't like to use a std::round
everywhere.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…