Strange things happen when i try to find the cube root of a number.
The following code returns me undefined. In cmd : -1.#IND
cout<<pow(( double )(20.0*(-3.2) + 30.0),( double )1/3)
While this one works perfectly fine. In cmd : 4.93242414866094
cout<<pow(( double )(20.0*4.5 + 30.0),( double )1/3)
From mathematical way it must work since we can have the cube root from a negative number.
Pow is from Visual C++ 2010 math.h library. Any ideas?
pow(x, y)from<cmath>does NOT work if x is negative and y is non-integral.This is a limitation of
std::pow, as documented in the C standard and on cppreference:There are a couple ways around this limitation:
Cube-rooting is the same as taking something to the 1/3 power, so you could do
std::pow(x, 1/3.).In C++11, you can use
std::cbrt. C++11 introduced both square-root and cube-root functions, but no generic n-th root function that overcomes the limitations ofstd::pow.