modernc.org/ccgo/v3@v3.16.14/lib/testdata/CompCert-3.6/test/c/integr.c (about)

     1  #include <stdlib.h>
     2  #include <stdio.h>
     3  
     4  static double square(double x)
     5  {
     6    return x * x;
     7  }
     8  
     9  static double integr(double (*f)(double), double low, double high, int n)
    10  {
    11    double h, x, s;
    12    int i;
    13  
    14    h = (high - low) / n;
    15    s = 0;
    16    for (i = n, x = low; i > 0; i--, x += h) s += f(x);
    17    return s * h;
    18  }
    19  
    20  double test(int n)
    21  {
    22    return integr(square, 0.0, 1.0, n);
    23  }
    24  
    25  int main(int argc, char ** argv)
    26  {
    27    int n; double r;
    28    if (argc >= 2) n = atoi(argv[1]); else n = 10000000;
    29    r = test(n);
    30    printf("integr(square, 0.0, 1.0, %d) = %g\n", n, r);
    31    return 0;
    32  }