I try to pow(x, 1.0/3.0) and get -1.#IND00, why does it happens?
I tried to do pow(x, 1./3), pow(x, (1.0/3.0)), but nothing positive.
It doesn’t works only when x less than 0. And x is float.
Example:
x = -3.12;
y = pow(x, 1.0 / 3.0);
printf("%f", y);
The code above prints y as -1.#IND00
>Solution :
The pow function doesn’t compute roots of negative numbers.
Section 7.12.7.4p1-2 of the C standard regarding the pow functions states:
1.
double pow(double x, double y);2. The
powfunctions computexraised to the powery. A domain error occurs ifxis finite and negative andyis finite and
not an integer value
So the values you’re giving constitute a domain error, which is described in section 7.12.1p2:
For all functions, a domain error occurs if an input argument is outside the domain over
which the mathematical function is defined. … On a
domain error, the function returns an implementation-defined value
If you want to compute the cube root of a number, you should instead use cbrt which accepts negative values.