Home > Software engineering >  -2.6 to the power of 0.2 outputs #IND00
-2.6 to the power of 0.2 outputs #IND00

Time:06-11

currently i'm doing my practice for C language and i've found one question about function pow() in C\C .

#include <stdio.h>
#include <math.h>
int main(){
    double k = 0.2;
    printf("2.6^k = %f\n", pow(2.6, k));
    printf("-2.6^k = %f\n", pow(-2.6, k));

}

OUTPUT:

2.6^k = 1.210583
-2.6^k = -1.#IND00

In this example -2.6 to the power of 0.2 i̶s̶ ̶n̶o̶t̶ ̶e̶v̶e̶n̶ ̶a̶ ̶c̶o̶m̶p̶l̶e̶x̶ ̶n̶u̶m̶b̶e̶r̶(Edit: it is),but output says(as i think) that number is indeterminable.

And in my practice there is the following: image

I implemented this like that:

/* e = 2.1783; x = -2.6 */
result = pow(cos(pow(x,0.2) - pow(e,-x   sqrt(3)))   1.61,2);

But due to (-x sqrt(3)) being negative number it outputs:

-1.#IND00

CodePudding user response:

The value 0.2 cannot be represented exactly in binary floating point. So what you have is not actually 0.2 but a value slightly more than that. This yields a complex result so pow returns NaN.

Reading into this further, section 7.12.7.4 of the C standard regarding the pow function states:

double pow(double x, double y);

A domain error occurs if x is finite and negative and y is finite and not an integer value.

In the event of a domain error, an implementation-defined value is returned. While MSVC doesn't seem to document what it does in this case, it apparently returns NaN. In the case of Linux, the man pages explicitly state that NaN is returned in this case.

  • Related