c++ - C++ pi 近似的不同输出

标签 c++

<分区>

Possible Duplicate:
Vastly different output C++ monte carlo approximation

在我的 64 位 ubuntu 计算机上,以下代码按预期工作,并使用两种算法返回 pi 的近似值。然而,在我必须演示代码的实验室机器上,一台 32 位 rhel 3 机器上,第二个算法总是返回 4,我不明白为什么。任何见解将不胜感激。

/*
 * RandomNumber.h
 *
 *  
 *      
 */

#ifndef RANDOMNUMBER_H_
#define RANDOMNUMBER_H_

class RandomNumber {
public:
RandomNumber() {
    x = time(NULL);
    m = pow(2, 31); //some constant value
    M = 65915 * 7915; //multiply of some simple numbers p and q
    method = 1;
}
RandomNumber(int seed) {
    x = ((seed > 0) ? seed : time(NULL));
    m = pow(2, 31); //some constant value
    method = 1; //method number
    M = 6543 * 7915; //multiply of some simple numbers p and q
}
void setSeed(long int seed) {
    x = seed; //set start value
}

void chooseMethod(int method) {
    this->method = ((method > 0 && method <= 2) ? method : 1); //choose one of two method
}

long int linearCongruential() { //first generator, that uses linear congruential method
    long int c = 0; // some constant
    long int a = 69069; //some constant
    x = (a * x + c) % m; //solution next value
    return x;
}

long int BBS() { //algorithm Blum - Blum - Shub
    x = (long int) (pow(x, 2)) % M;
    return x;
}
double nextPoint() { //return random number in range (-1;1)
    double point;
    if (method == 1) //use first method
        point = linearCongruential() / double(m);
    else
        point = BBS() / double(M);
    return point;
}
private:
long int x; //current value
long int m; // some range for first method
long int M; //some range for second method
int method; //method number
};

#endif /* RANDOMNUMBER_H_ */

和测试类:

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <iomanip>
#include "RandomNumber.h"
using namespace std;

int main() {
cout.setf(ios::fixed);
cout.precision(6);
RandomNumber random;
srand((unsigned) time(NULL));
cout << "---------------------------------" << endl;
cout << "   Monte Carlo Pi Approximation" << endl;
cout << "---------------------------------" << endl;
cout << " Enter number of points: ";
long int k1;
cin >> k1;
cout << "Select generator number: ";
int method;
cin >> method;
random.chooseMethod(method);
cout << "---------------------------------" << endl;
long int k2 = 0;
double sumX = 0;
double sumY = 0;
for (long int i = 0; i < k1; i++) {
    double x = pow(-1, int(random.nextPoint() * 10) % 2)
            * random.nextPoint();
    double y = pow(-1, int(random.nextPoint() * 10) % 2)
            * random.nextPoint();
    sumX += x;
    sumY += y;
    if ((pow(x, 2) + pow(y, 2)) <= 1)
        k2++;

}
double pi = 4 * (double(k2) / k1);
cout << "M(X)  = " << setw(10) << sumX / k1 << endl; //mathematical expectation of x
cout << "M(Y)  = " << setw(10) << sumY / k1 << endl; //mathematical expectation of y
cout << endl << "Pi = " << pi << endl << endl; //approximate Pi

return 0;
}

最佳答案

问题是 pow 返回一个 double,它在低端失去了精度。为 % 运算符转换为 long int 始终返回相同的结果,因此您的 RNG 输出常量 -60614748。

x = time(0)                 1354284781
pow(x, 2)                  1.83409e+18   0x1.973fdc9dc7787p+60
(long int) pow(x, 2)       -2147483648    0x80000000
(long int) pow(x, 2) % M     -60614748

修复方法是将 x = (long int) (pow(x, 2)) % M; 更改为 x = x * x % M,执行所有long int 中的算术运算。请注意,严格来说这仍然是不正确的,因为未定义有符号溢出;更正确的做法是使用 unsigned long

关于c++ - C++ pi 近似的不同输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13646493/

相关文章:

c++ - OpenCV C++ 中方差局部二进制模式 (VLBP) 的直方图计算

c++ - OpenGL 旋转 - 本地轴与全局轴

c++ - AF_UNIX 套接字发送线程安全吗?

c++ - my_thread_global_end 线程没有退出,错误?

c++ - 如果整个应用程序都是虚拟映射的,为什么 new 会进行系统调用?

c++ - 在同一类中强制使用 Getter/Setter (C++)

c++ - 寻找一种不使用memcpy来完成memcpy的方法

c++ - 需要将此代码更改为 do while 代码

c++ - 这些右值引用是什么意思?

c++ - 使用无屏幕创建 OpenGL 上下文