Home > OS >  Why return -1.#IND00 for pow(x, 1.0/3.0) when x is negative?
Why return -1.#IND00 for pow(x, 1.0/3.0) when x is negative?

Time:12-29

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

CodePudding user response:

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 pow functions compute x raised to the power y. A domain error occurs if x is finite and negative and y is 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.

CodePudding user response:

The power function with negative base is defined in the real numbers only when the exponent is integer. The operation -3.12^0.3 is not defined in mathematics (in real numbers).*

This fact is described in the documentation of pow():

If base is finite and negative and exponent is finite and non-integer, a domain error occurs and a range error may occur.


* This is technically not quite correct but for most exponents the value of x^y when x is negative is a complex number, not a real number.

  •  Tags:  
  • c
  • Related