Why is pow(int, int) so slow?

pow() works with real floating-point numbers and uses under the hood the formula pow(x,y) = e^(y log(x)) to calculate x^y. The int are converted to double before calling pow. (log is the natural logarithm, e-based) x^2 using pow() is therefore slower than x*x. Edit based on relevant comments Using pow even with integer exponents may … Read more

GCC C++ pow accuracy

The function pow operates on two floating-point values, and can raise one to the other. This is done through approximating algorithm, as it is required to be able to handle values from the smallest to the largest. As this is an approximating algorithm, it sometimes gets the value a little bit wrong. In most cases, … Read more

Is std::abs(0u) ill-formed?

Looks like libstdc++ is correct, this is not ill-formed, although we will see there are some doubts expressed over whether this is a defect in LWG active issue 2192. The draft C++11 standard section 26.8 [c.math] paragraph 11 says: Moreover, there shall be additional overloads sufficient to ensure: and includes the following item: Otherwise, if … Read more

Sin and Cos give unexpected results for well-known angles

C/C++ provides sin(a), cos(a), tan(a), etc. functions that require a parameter with radian units rather than degrees. double DegreesToRadians(d) performs a conversion that is close but an approximate as the conversion results are rounded. Also machine M_PI is close, but not the same value as the the mathematical irrational π. OP’s code with 180 passed … Read more