Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
466 views
in Technique[技术] by (71.8m points)

c - How to avoid -nan(ind) error when using the pow function. Is it possible to use pow with negative bases with non-integral exponents

I get an error when using the pow function -nan(ind) prints to the screen. Wondering if theres a way to use pow with numbers that have negetive bases and non integer exponents.

Currently the pow function is pow(-12.4112021858, 0.2). And gives me -nan(ind) error. If i change the base to just 12.41 it seems to calculate perfectly fine.

Edit - I had exponent set as a different number to 0.2

int main() {



    double a = 1.83;
    double v = 1.25;
    double r = 0;

    double sum = 0;
 
    double exponent = 0.2;

    double result = 1;
    while (true)
    {
    printf("Enter Radius 
");
    scanf_s("%lf", &r);
    sum = 1 - r/ a;
    printf("%lf
", sum);
    sum = sum * v;
    printf("%lf
", sum);
    sum=  pow(sum, exponent);
    printf("%lf
", sum);

}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Wondering if theres a way to use pow with numbers that have negetive bases and non integer exponents.

First, check the documentation. C 2018 7.12.7.4 specifies powf, pow, and powl:

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…

Your x, approximately ?12.4112021858, is finite and negative, and your y, approximately .2, is finite and not an integer value. So a domain error occurs.

This means you cannot expect to get a result unless the pow you are using specifically provides support for additional cases beyond what the C standard requires, even if .2 is exactly represented in double.

(When there is a domain-error, an implementation-defined result is returned. This could be a NaN, a valid mathematical result, such as ?2 for pow(-32, .2) if decimal-based floating-point is used, or something else. The implementation may also report an error via errno or a floating-point exception. See C 2018 7.12.1 for more information.)

Second, .2 is not representable in the double format of most C implementations. C implementations commonly use the IEEE-754 binary64 format. In this format, the closest representable value to .2 is 0.200000000000000011102230246251565404236316680908203125. For the source code pow(-12.4112021858, .2), the numerals are first converted to double, and then pow is called with arguments -12.41120218580000056363132898695766925811767578125 and 0.200000000000000011102230246251565404236316680908203125. So you are not requesting an operation that has a real-number result.

If your C implementation used a decimal-based floating-point, .2 would be representable, and it would be reasonable for pow(-12.4112021858, .2) to return the fifth root of x, as the fifth root is a negative real number. (This would be an extension to the standard specification of pow, as described above.)

If you know y is supposed to be one-fifth or a rational number p/q where q is odd, you can calculate the desired result as pow(fabs(x), y) if p is even and copysign(pow(fabs(x), y), x) if p is odd.

One of the comments suggesting using cpow, but that will not produce the result you want. cpow(-12.4112021858, .2) will return approximately 1.3388 + .9727 i. (The complex power “function” is multi-valued, but cpow is defined to produce that result.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...