使用pow函数时如何避免-nan(ind)错误.是否可以使用带有非整数指数的负底数的 pow [英] How to avoid -nan(ind) error when using the pow function. Is it possible to use pow with negative bases with non-integral exponents

查看:53
本文介绍了使用pow函数时如何避免-nan(ind)错误.是否可以使用带有非整数指数的负底数的 pow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用电源功能 -nan(ind)打印到屏幕时出现错误.想知道是否有一种方法可以将pow与具有可取底数和非整数指数的数字一起使用.

当前pow函数为pow(-12.4112021858,0.2).并给我-nan(ind)错误.如果我将基数更改为仅12.41,则似乎可以很好地计算出结果.

编辑 - 我将指数设置为与 0.2 不同的数字

  int main(){双a = 1.83;双v = 1.25;双r = 0;双和= 0;双指数= 0.2;双重结果= 1;一会儿(true){printf("Enter Radius \ n");scanf_s(%lf",& r);和= 1-r/a;printf(%lf \ n",总和);求和=求和* v;printf(%lf \ n",总和);总和= pow(总和,指数);printf(%lf \ n",总和);}} 

解决方案

想知道是否有一种方法可以将pow用于具有可取底数和非整数指数的数字.

首先,检查文档.C 2018 7.12.7.4指定 powf pow powl :

pow 函数计算提升到幂 y x .如果 x 是有限的并且是负数,而 y 是有限的而不是整数值,则会发生域错误.

您的 x 约为-12.4112021858,是有限的且为负数,而您的 y 约为0.2则是有限的,不是整数.因此发生域错误.

这意味着除非使用的 pow 特别为C标准要求的其他情况提供了支持,即使.2精确地表示为 double,也不能期望得到结果..

(当出现域错误时,将返回实现定义的结果.这可能是NaN,有效的数学结果,例如 pow(-32,.2)如果使用基于十进制的浮点数或其他方式.该实现可能还会通过 errno 或浮点异常报告错误.有关更多信息,请参见C 2018 7.12.1./p>

第二,.2不能代表大多数C实现的 double 格式.C实现通常使用IEEE-754 binary64格式.在此格式下,最接近.2的可表示值是0.200000000000000011102230246251565404236316680908203125.对于源代码 pow(-12.4112021858,.2),首先将数字转换为 double ,然后使用参数调用 pow -12.41120218580000056363132898695766925811767578125和0.200000000000000011102230246251565404236316680908203125.因此,您不需要的是具有实数结果的操作.

如果您的C实现使用基于十进制的浮点数,则.2是可表示的,并且 pow(-12.4112021858,.2)返回 x ,因为第五个根是负实数.(如上所述,这将是对 pow 的标准规范的扩展.)

如果您知道 y 应该是五分之一或有理数 p / q 其中 q 是奇数,如果 p 是偶数而 copysign(pow(fabs(x),y),x),如果 p 是奇数.

其中一项建议使用 cpow 的注释,但这不会产生您想要的结果. cpow(-12.4112021858,.2)将返回大约1.3388 + .9727 i .(复杂的幂函数"是多值的,但是定义了 cpow 才能产生该结果.)

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 \n");
    scanf_s("%lf", &r);
    sum = 1 - r/ a;
    printf("%lf\n", sum);
    sum = sum * v;
    printf("%lf\n", sum);
    sum=  pow(sum, exponent);
    printf("%lf\n", sum);

}
}

解决方案

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.)

这篇关于使用pow函数时如何避免-nan(ind)错误.是否可以使用带有非整数指数的负底数的 pow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆