/* *   exp 定積分 その2 */ #include #include #include #define RAND() ((double)rand() / (1.0 + RAND_MAX)) int main(void) { int seed; int j; int ntrial; double y0; double s = 0.0; double sreal, serror; printf("Input: Number of trials:\n"); scanf("%u", &ntrial); printf("Input: seed\n"); scanf("%u", &seed); srand(seed); for (j = 1; j <= ntrial; j ++) { y0 = exp(-RAND()); s = s + y0; } s = s / ntrial; sreal=1.0-exp(-1.0); serror=s-sreal; printf("s = %1.10f \n", s); printf("sreal = %1.10f \n", sreal); printf("serror = %1.10f \n", serror); }