Function pointers and numerical integration

Passing a function to a function, the trapezoidal rule it powers, and how a hand-typed π quietly caps the accuracy.

What you'll learn
  • Declare, pass and call a function pointer
  • Implement the trapezoidal rule and predict its error
  • Spot when a constant, not the method, limits a numerical result

integral.c computes

\[\int_0^{\pi/2} e^{-x}\sin x \, dx \;=\; \frac{1 - e^{-\pi/2}}{2} \;\approx\; 0.396060211824619\]

numerically. The integrator should work for any function, so it takes the function as an argument:

double integral(double a, double b, double (*f)(double), int n);

double (*f)(double) reads “f is a pointer to a function taking a double and returning a double”. Inside, f(x) calls whatever function was passed — here integral(a, b, &f, n), though plain f works too, because a function’s name decays to its address. Function pointers are how C does callbacks: qsort’s comparison function is the classic example.

The trapezoidal rule

Split \([a, b]\) into \(n\) slices of width \(h = (b - a)/n\) and replace the curve over each slice with a straight line:

double h = (b - a) / (double)n;
double sum = 0.5 * (f(a) + f(b));
for (i = 1; i < n; i++) {
    sum += f(a + i * h);
}
return h * sum;

Interior points are shared by two trapezoids, so they count once; the two ends count half.

The trapezoidal rule adding one slice at a time, with the running sum. Move n and compare the final error: doubling n cuts it by about four.

The error shrinks with \(h^2\). To leading order it is

\[E \;\approx\; \frac{h^2}{12}\,\big(f'(a) - f'(b)\big),\]

and here \(f’(0) = 1\), \(f’(\pi/2) = -e^{-\pi/2}\). For \(n = 1000\) this predicts \(2.48 \times 10^{-7}\), exactly the error measured. Each factor of 10 in \(n\) buys two more correct digits.

The π that stops the progress

The program defines its own π before including <math.h>:

#define M_PI 3.14159265

That value is wrong in the ninth decimal place, so the upper limit \(\pi/2\) is off by \(1.8 \times 10^{-9}\), and the integral by about \(f(\pi/2) \times 1.8 \times 10^{-9} \approx 3.7 \times 10^{-10}\) — a floor that no number of trapezoids can get under:

Correct digits against n. With full-precision π the rule gains two digits per decade all the way to n = 1,000,000. With the program's π, progress stops near 9.4 digits from n = 100,000 on.

Whether it bites depends on the build. With GCC’s default dialect, <math.h> defines M_PI again, silently replacing the program’s value (the redefinition warning is suppressed inside system headers), and the result is accurate to \(2.5 \times 10^{-13}\). Compile with -std=c11 and <math.h> leaves M_PI alone, so the truncated value is used: the error rises to \(3.7 \times 10^{-10}\), 1,500 times worse. Either way the printout, 0.396060 with %f’s six decimals, hides the difference — and n = 1,000 would already have printed the same six digits as the million used.

The robust fix is to not define M_PI at all: use acos(-1.0), or a full-precision constant under a name of your own.

Try it yourself. Print the result with %.15f and compile once with plain gcc and once with gcc -std=c11; explain the difference. Then add a second integrand, say double g(double x) { return x * x; }, and integrate it from 0 to 1 by passing g to the same integral. Why is the trapezoid error for \(x^2\) exactly \(h^2/6\) for every \(n\)?

View source on GitHub →