所以,当我编译代码(使用这里找到梅森倍捻机:http://www-personal.umich.edu/~wagnerr/MersenneTwister.html):为什么我的程序给人一种完全不同的输出,当我编译MinGW相比,G ++
#include <iostream>
#include <cmath>
#include "mtrand.h"
using namespace std;
double pythag(double x, double y) {
double derp=0;
derp=(x*x)+(y*y);
derp=sqrt(derp);
}
int main() {
double x=0;
double y=0;
double pi=0;
double hold1=0;
double hold2=0;
double hits=0;
MTRand mt;
mt.seed();
// cout.precision(10);
for(long i=1; i<=100000000000l; i++) {
x=abs(mt.rand());
y=abs(mt.rand());
if(pythag(x,y)<=1) {
hits++;
}
if(i%100000l==0) {
pi=(4*hits)/i;
cout << "\r" << i << " " << pi ;
}
}
cout <<"\n";
return 42;
}
使用G ++(“G ++ pi.cc -o pi“) 并运行生成的应用程序,我得到我想要的输出,使用Monte Carlo方法计算出的pi的运行计数。 但是,当我用mingw g ++编译(“i686-pc-mingw32-g ++ -static-libstdC++ -static-libgcc pi.cc -o pi.exe”) 我总是得到一个0. 任何帮助是不胜感激。
未定义的行为是你在找什么。向上转动警告级别“-Wall”并修复编译警告。警告是您的代码中真正的逻辑错误。 – 2011-12-15 05:57:42
`pythag`表示它返回一个double,但没有返回语句。 – Hugh 2011-12-15 05:59:08