pow()问题 [英] pow() problem
问题描述
有谁能告诉我为什么pow(-8.0,1.0 / 3.0)(-8的立方根)返回
nan(在linux中)和负无穷大或者什么(在devcpp中>
windows),而不是-2?
问题似乎是pow无法处理负的立方根
数,我的意思是,计算器可以做到这一点,或者我在这里使用错误的
函数。我目前的解决方案是一个黑客,它检测到负面的输入并以不同方式处理它。
我怀疑它与表示功率的事实有关
作为浮点值。
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns
nan (in linux) and negative infinity or something (in devcpp in
windows), instead of -2?
The problem seems to be that pow can''t handle cubic root of negative
number, I mean, a calculator could do it or am I using the wrong
function here. My current solution is a hack which detects negative
input and handle it differently.
I suspect it is something to do with the fact the power is represented
as a floating point value.
推荐答案
Shaobo Hou写道:
Shaobo Hou wrote:
有谁能告诉我为什么pow(-8.0,1.0 / 3.0)(-8的立方根)返回
nan(在linux中)和负无穷大或某些东西(在devcpp中
窗口),而不是-2?
问题似乎是pow无法处理负数
数的立方根,我的意思是,计算器可以做到或者是我在这里使用了错误的
功能。我目前的解决方案是检测负面输入并以不同方式处理它的黑客。
我怀疑它与功率表示为浮点值的事实有关。
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns
nan (in linux) and negative infinity or something (in devcpp in
windows), instead of -2?
The problem seems to be that pow can''t handle cubic root of negative
number, I mean, a calculator could do it or am I using the wrong
function here. My current solution is a hack which detects negative
input and handle it differently.
I suspect it is something to do with the fact the power is represented
as a floating point value.
在某种程度上,是的:pow()的第二个参数不是
正好是三分之一,而是附近的值。你实际上并不是计算-8的立方根,而是但是-8升至
理性指数接近三分之一。那个指数值
是U / V形式的一小部分,其中U,V是整数,V是
几乎可以肯定是2的大功率。数学上
说话,
pow(-8,U / V)
== pow(pow(-8,1) / V),U)
== pow(sqrt(sqrt(...( - 8)...)),U)
(由于V是2的幂,并且这不能用实数算术来评估
。
一些建议:
- 最新的C99 Standard定义了一个cbrt()函数。
即使你无法访问一个完整的C99
实现,你可能会发现cbrt()存在。
- 如果cbrt()不可用,请尝试类似
(x> = 0.0)的内容? pow(x,1.0 / 3.0): - pop(-x,1.0 / 3.0)
- 如果您的系统有copysign(),请尝试将上述内容写成
copysign(pow(fabs(x),1.0 / 3.0),x)
-
Er ********* @ sun.com
In a way, yes: the second argument to pow() is not
exactly one-third, but a nearby value. You aren''t actually
calculating "the cube root of -8," but "-8 raised to a
rational exponent close to one-third." That exponent value
is a fraction of the form U/V where U,V are integers and V is
almost certainly a large power of two. Mathematically
speaking,
pow(-8, U/V)
== pow(pow(-8, 1/V), U)
== pow(sqrt(sqrt(...(-8)...)), U)
(since V is a power of two), and this can''t be evaluated
in real arithmetic.
Some suggestions:
- The latest "C99" Standard defines a cbrt() function.
Even if you don''t have access to a full-blown C99
implementation, you may find that cbrt() is present.
- If cbrt() isn''t available, try something like
(x >= 0.0) ? pow(x, 1.0/3.0) : -pow(-x, 1.0/3.0)
- If your system has copysign(), try writing the above as
copysign(pow(fabs(x), 1.0/3.0), x)
--
Er*********@sun.com
在星期二, 2005年2月22日15:12:57 +0000,邵伯侯对解析器说:
On Tue, 22 Feb 2005 15:12:57 +0000, Shaobo Hou said to the parser:
谁能告诉我为什么pow(-8.0,1.0 / 3.0)(立方根-8) )返回nan
(在linux中)和负无穷大或某些东西(在windows中的devcpp中),而不是-2?
问题似乎是pow可以'' t处理负数
数的立方根,我的意思是,计算器可以做到这一点,或者我在使用错误的函数
。我目前的解决方案是检测负输入并以不同方式处理它的黑客。
我怀疑它与功率表示为浮点值的事实有关。
Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns nan
(in linux) and negative infinity or something (in devcpp in windows),
instead of -2?
The problem seems to be that pow can''t handle cubic root of negative
number, I mean, a calculator could do it or am I using the wrong function
here. My current solution is a hack which detects negative input and
handle it differently.
I suspect it is something to do with the fact the power is represented as
a floating point value.
它与-8有更多关系...
下面演示了一种处理这个问题的方法,当你提到你当前的解决方案检测到负输入
并以不同的方式处理它时,这可能就是你b $ b所做的。
#include< stdio.h>
#include< stdlib.h>
#include< math.h>
int main (无效)
{
浮点数= -8;
浮动结果;
如果(num> = 0)
result = pow(num,1.0 / 3.0);
else
result = -pow(-num ,1.0 / 3.0);
printf("%f\ n",结果);
返回EXIT_SUCCESS;
}
It has more to do with the -8...
The below demonstrates one method for handling this, which may be what you
did already, when you mention your current solution detects negative input
and handles it differently.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
float num = -8;
float result;
if (num >= 0)
result = pow(num, 1.0/3.0);
else
result = -pow(-num, 1.0/3.0);
printf("%f\n", result);
return EXIT_SUCCESS;
}
文章< cv ********* @ wa pping.cs.man.ac.uk>,
Shaobo Hou< ho *** @ cs.man.ac.uk>写道:
:谁能告诉我为什么pow(-8.0,1.0 / 3.0)(-8的立方根)返回
:nan(在linux中)和负无穷大或某事(在
:windows中的devcpp中),而不是-2?
:我怀疑这是与权力代表的事实有关
:作为浮点值。
合理。 1/3不完全可以用二进制表示,所以是否
1/3出现奇数或偶数或偶数 (这将允许你首先平衡
x)将取决于你正在使用的精度。
-
这些是borogoves并且momerathsoutgrabe完全模仿。
In article <cv*********@wapping.cs.man.ac.uk>,
Shaobo Hou <ho***@cs.man.ac.uk> wrote:
:Can anyone tell me why pow(-8.0, 1.0 / 3.0) (cubic root of -8) returns
:nan (in linux) and negative infinity or something (in devcpp in
:windows), instead of -2?
:I suspect it is something to do with the fact the power is represented
:as a floating point value.
Plausibly. 1/3 is not exactly representable in binary, so whether
1/3 comes out "odd" or "even" (which would allow you to square the
x first) would be dependant on the precision you are working with.
--
Those were borogoves and the momerathsoutgrabe completely mimsy.
这篇关于pow()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!