作者VictorTom (鬼翼&娃娃鱼)
看板C_and_CPP
标题Re: [问题] 有关 pow() 函数
时间Wed Jul 22 12:03:31 2009
※ 引述《alai (none)》之铭言:
: 推 hilorrk:我想应该是double的问题 只要有double型态出现就有可能会 07/22 11:30
: → hilorrk:不精准...还记得强制cast的运作方式吗? 而用math.h和cmath 07/22 11:31
: → hilorrk:的差别应该是math.h的pow都是double型态 cmath不是吧... 07/22 11:31
: 推 VictorTom:他的问题不是这个, 这问题很怪是在dev-C++跑出来.... 07/22 11:44
: → VictorTom:double N=5.0; pow(N, 2); 拿来当loop终止条件的结果. 07/22 11:44
: → VictorTom:与 pow(5.0, 2) 当终止条件跑出来的结果不一样.... 07/22 11:45
: → VictorTom:虽然你把两个 pow() 直接print出来都是25.0没错~_~ 07/22 11:45
我贴一下我的code吧, 看有没有大大可以解释, 小弟是已经猜是bug了....XD
==
#include <iostream>
#include <cstdlib>
#include <math.h> /* 用 cmath 的话做不出问题 */
using namespace std;
int main(void)
{
double N = 5.0;
double M = pow(N,2.0);
for(int i=0; i<=M; i++)
/* 这行用 for(int i=0; i<=pow(N,2.0); i++) 取代,
在 Dev-C++ 4.9.9.2 跑出的结果会不一样,
不过在 Visual C++ 2005 跑出来的结果是一致的. */
{
cout << i << " ";
}
cout << endl;
system("PAUSE");
return 0;
}
==
虽然以前有遇过因为浮点reg有80 bits的关系比var只有64精确....
所以可能遇到最佳化後的code可能会有精确度不同的问题发生....
不过这个例子看起来不是这麽一回事; 要研究就disasm去看吧Orz
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.134.41.4