C++ 计算pow(double,2)会得到一个巨大的数字(C)
我正在尝试编写一个使用多个进程进行Pi计算的模拟代码。 我有一个函数,它生成从1到-1的随机双x,y数, 当我试图计算x^2+y^2 这是错误的。发生的事情是,你在做一个整数除法,然后乘以4,然后把它转换成一个双精度。所以你有地板行为 在进行除法之前,您需要将C++ 计算pow(double,2)会得到一个巨大的数字(C),c++,montecarlo,C++,Montecarlo,我正在尝试编写一个使用多个进程进行Pi计算的模拟代码。 我有一个函数,它生成从1到-1的随机双x,y数, 当我试图计算x^2+y^2 这是错误的。发生的事情是,你在做一个整数除法,然后乘以4,然后把它转换成一个双精度。所以你有地板行为 在进行除法之前,您需要将pointswith或pointsOutside转换为双精度 (这也是一个错误的公式,但那是另一回事。)我错误地使用了MPI_Send和MPI_Recv,现在它正确地计算了功率。这还不足以帮助您。有很多可能的问题会导致这种症状,你可能有很多
pointswith
或pointsOutside
转换为双精度
(这也是一个错误的公式,但那是另一回事。)我错误地使用了MPI_Send和MPI_Recv,现在它正确地计算了功率。这还不足以帮助您。有很多可能的问题会导致这种症状,你可能有很多方式误解了你的结果。向我们展示你的代码,告诉我们你在使用什么语言,告诉我们你观察到的具体行为(观察,而不是解释),并告诉我们你预期会发生什么不同。对不起,我刚刚编辑了这个问题,希望它足够了什么是
RANDOM\u MAX
和RANDOM\u MIN
?你能给我们看一段你的代码吗?足够让我们运行它并看到问题,但仍然尽可能多地删除与问题无关的代码吗?首先,你有RANDOM_MAX和RAND_MAX。其次,RAND()对蒙特卡罗模拟市民来说是可怕的。第三,您是否绝对确定randomPoint实际返回正确范围内的点?double pi=(pointsWithin/pointsOutside)*4
将导致0
,因为这两个变量都是int
,所以它是一个整数除法。将其更改为:double pi=((double)pointsWithin/(double)TOTAL_NUM_OF_POINTS)*4.0;但我认为这不是问题所在。当我计算x^2+y^2建议double pi=4.0*pointsWithin/pointsOutside时,我得到了错误的数字代码>(避免强制转换)
#include <mpi.h>
#include <iostream>
#include "main_header.h"
#include "dynamic.h"
using namespace std;
int main(int argc,char *argv[])
{
int numprocs, myid, i, root = 0, pointsInCircle = 0, pointsOutOfCircle = 0;
boolean pointInside = TRUE;
double point[DIMENSION], scatterTable[NUMBER_OF_SLAVES][DIMENSION], pi, t1, t2;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Status status;
srand(time(NULL));
t1 = MPI_Wtime();
printf("Trying to scatter..\n");
fflush(stdout);
MPI_Scatter(&scatterTable[0][0], DIMENSION, MPI_DOUBLE, // Master process sends the first tasks to the slaves.
&point[0], DIMENSION, MPI_DOUBLE, root, MPI_COMM_WORLD);
printf("Scatter successful\n");
fflush(stdout);
if (myid == 0) {
for (i = 0; i < TOTAL_NUM_OF_POINTS; i++) {
MPI_Recv(&pointInside, 1, MPI_INT, MPI_ANY_SOURCE, 0, MPI_COMM_WORLD, &status);
if (pointInside) {
pointsInCircle++;
printf("point inside\n");
fflush(stdout);
}
else {
pointsOutOfCircle++;
printf("point outside\n");
fflush(stdout);
}
point[0] = randomPoint(); // X component
point[1] = randomPoint(); // Y component
printf("x = %f, y = %f\n", point[0], point[1]);
fflush(stdout);
MPI_Send(&point[DIMENSION], 2, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD);
}
point[0] = point[1] = END_OF_PI_CALC;
for (i = 0; i < NUMBER_OF_SLAVES; i++)
MPI_Send(&point[DIMENSION], 2, MPI_DOUBLE, status.MPI_SOURCE, 0, MPI_COMM_WORLD);
t2 = MPI_Wtime();
pi = calculatePi(pointsInCircle, pointsOutOfCircle);
printf("Pi = %f.\n Time to calculate: %f", pi, (t2-t1));
}
else {
while (point[0] != END_OF_PI_CALC || point[1] != END_OF_PI_CALC) {
pointInside = withinCircle(point[0], point[1]);
MPI_Send(&pointInside, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
MPI_Recv(&point[DIMENSION], 2, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD, &status);
}
}
if (myid == 0)
MPI_Finalize();
return 0;
}
#include "dynamic.h"
#include <stdio.h>;
#include <math.h>
boolean withinCircle(double x, double y) {
double result = (pow(x,(double)2) + pow(y, (double)2));
printf("result = %f", result);
fflush(stdout);
if (result <= 1)
return TRUE;
return FALSE;
}
double randomPoint() {
double range = (RANDOM_MAX - RANDOM_MIN);
double div = RAND_MAX / range;
return RANDOM_MIN + (rand() / div);
}
double calculatePi(int pointsWithin, int pointsOutside) {
double pi = (pointsWithin / pointsOutside) * 4;
return pi;
}
#ifndef DYNAMIC_H
#define DYNAMIC_H
#include "main_header.h"
#define TOTAL_NUM_OF_POINTS 20000
#define NUM_OF_POINTS_TO_SEND 1
#define RANDOM_MAX 1
#define RANDOM_MIN -1
#define NUMBER_OF_SLAVES 3
#define DIMENSION 2
#define END_OF_PI_CALC 2
boolean withinCircle(double x, double y);
double randomPoint();
double calculatePi(int pointsWithin, int pointsOutside);
#endif DYNAMIC_H
#ifndef MAIN_H
#define MAIN_H
#include <time.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int boolean; // Defines a boolean datatype.
#endif MAIN_H
x = -0.944151, y = 0.389386
result = 17134570711043240115048918967982918960341742262641887592820622432866394633762913354831186160392586118427086670840176838705152.000000
point outside
double calculatePi(int pointsWithin, int pointsOutside) {
double pi = (pointsWithin / pointsOutside) * 4;
return pi;
}