计算t = pow(-11.5,.333) [英] computing t = pow(-11.5, .333)

查看:79
本文介绍了计算t = pow(-11.5,.333)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C程序中,我需要进行求幂,其中基数为
为负且指数为分数。在标准C中,这将是
类似于t = pow(-11.5,.333),但是使用这个组合

的参数存在域错误,结果是a

渗透NaN。


我通过将标准函数放入

包装器解决了这个问题:


双xpow(双b,双x)

{

双p;


如果(b< 0)

{

p = pow(-b,x);

p * = -1.0;

}

其他p = pow(b,x);


返回p;

}




标准库函数中有这种限制的特殊原因吗?有没有其他人有这个问题,并找到了更好的解决方案?


JS

In a C program I need to do exponentiation where the base is
negative and the exponent is a fraction. In standard C this would
be something like t = pow(-11.5, .333), but with this combination
of arguments there is a domain error and the result is a
percolating NaN.

I worked around the problem by putting the standard function in a
wrapper:

double xpow(double b, double x)
{
double p;

if(b < 0)
{
p = pow(-b, x);
p *= -1.0;
}
else p = pow(b, x);

return p;
}

Is there any particular reason for this limitation in the
standard library function? Has anyone else had a problem with
this and found a better solution?

JS

推荐答案

John Smith写道:
John Smith wrote:

在C程序中,我需要进行求幂,其中基数为负,指数为分数。在标准C中,这将是t = pow(-11.5,.333)之类的东西,但是这个参数的组合存在域错误,结果是渗透NaN。

我通过将标准函数放入
包装器解决了这个问题:

双xpow(双b,双x)
{
double p;

if(b< 0)
{
p = pow(-b,x);
p * = -1.0;
}
其他p = pow(b,x);

返回p;
}

这个限制是否有任何特殊原因在
标准库函数中?有没有其他人有这个问题,并找到了更好的解决方案?

In a C program I need to do exponentiation where the base is
negative and the exponent is a fraction. In standard C this would
be something like t = pow(-11.5, .333), but with this combination
of arguments there is a domain error and the result is a
percolating NaN.

I worked around the problem by putting the standard function in a
wrapper:

double xpow(double b, double x)
{
double p;

if(b < 0)
{
p = pow(-b, x);
p *= -1.0;
}
else p = pow(b, x);

return p;
}

Is there any particular reason for this limitation in the
standard library function? Has anyone else had a problem with
this and found a better solution?




-11.5有第三个根是否定的,

如果那是你真正想要找的东西。


但是,如果你认为

a负数增加到一个偶数电源,是积极的,



a负数提升到一个奇整数幂,是负的,

然后它似乎很自然

将负数提升为非整数的符号,

应该是未定义的。


-

pete



-11.5 has a third root which is negative,
if that''s what you''re really trying to find.

However, if you consider that
a negative number raised to an even integer power, is positive,
and that
a negative number raised to an odd integer power, is negative,
then it just seems natural that
the sign of a negative number raised to a non integer,
should be undefined.

--
pete


John Smith写道:
John Smith wrote:
在C程序中,我需要进行求幂,其中基数是负的,指数是一个分数。在标准C中,这将是t = pow(-11.5,.333)之类的东西,但是这个参数的组合存在域错误,结果是渗透NaN。

我通过将标准函数放入
包装器解决了这个问题:

双xpow(双b,双x)
{
double p;

if(b< 0)
{
p = pow(-b,x);
p * = -1.0;
}
其他p = pow(b,x);

返回p;
}

这个限制是否有任何特殊原因在
标准库函数中?有没有其他人有这个问题,并找到了更好的解决方案?
In a C program I need to do exponentiation where the base is
negative and the exponent is a fraction. In standard C this would
be something like t = pow(-11.5, .333), but with this combination
of arguments there is a domain error and the result is a
percolating NaN.

I worked around the problem by putting the standard function in a
wrapper:

double xpow(double b, double x)
{
double p;

if(b < 0)
{
p = pow(-b, x);
p *= -1.0;
}
else p = pow(b, x);

return p;
}

Is there any particular reason for this limitation in the
standard library function? Has anyone else had a problem with
this and found a better solution?




我不知道为什么pow功能是这样定义的那里

对理由的了解很少。如果您的

实现提供它,您可以使用C99中的立方根函数获得

pow(-11.5,.333)的预期结果:cbrt(-11.5) ;。


Robert Gamble



I don''t know why the pow function is defined this way and there is
little insight in the rationale. You can obtain the expected result of
pow(-11.5, .333) by using the cube root function from C99 if your
implementation provide it: cbrt(-11.5);.

Robert Gamble


Robert Gamble写道:
Robert Gamble wrote:
John Smith写道:
John Smith wrote:
在C程序中,我需要进行求幂,其中基数为负,指数为分数。在标准C中,这将是t = pow(-11.5,.333)之类的东西,但是这个参数的组合存在域错误,结果是渗透NaN。

我通过将标准函数放入
包装器解决了这个问题:

双xpow(双b,双x)
{
double p;

if(b< 0)
{
p = pow(-b,x);
p * = -1.0;
}
其他p = pow(b,x);

返回p;
}

这个限制是否有任何特殊原因在
标准库函数中?有没有其他人有这个问题,并找到了更好的解决方案?
我不知道为什么pow功能是这样定义的,而且理由上没有什么洞察力。
In a C program I need to do exponentiation where the base is
negative and the exponent is a fraction. In standard C this would
be something like t = pow(-11.5, .333), but with this combination
of arguments there is a domain error and the result is a
percolating NaN.

I worked around the problem by putting the standard function in a
wrapper:

double xpow(double b, double x)
{
double p;

if(b < 0)
{
p = pow(-b, x);
p *= -1.0;
}
else p = pow(b, x);

return p;
}

Is there any particular reason for this limitation in the
standard library function? Has anyone else had a problem with
this and found a better solution?
I don''t know why the pow function is defined this way and there is
little insight in the rationale.




我显然没有给予足够的考虑。任何负数

上升到一个分数幂,而不是奇数

数(1/3,1 / 5,1 / 7等)的倒数一个复杂的结果。 C99为复数提供了

支持,你可以用cpow函数系列来执行这样的计算。

你可以获得预期的结果
pow(-11.5,.333)使用C99中的立方根函数,如果你的
实现提供它:cbrt(-11.5);.



I obviously didn''t give enough thought to that. Any negative number
raised to a fractional power that is not the reciprocal of an odd
number (1/3, 1/5, 1/7, etc) will have a complex result. C99 provides
support for complex numbers and you can perform such calculations with
the cpow family of functions.
You can obtain the expected result of
pow(-11.5, .333) by using the cube root function from C99 if your
implementation provide it: cbrt(-11.5);.




Robert Gamble



Robert Gamble


这篇关于计算t = pow(-11.5,.333)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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