#include <stdio.h>

typedef double (* MFP)(double);

enum { DIVN = 30 };

double numInt0(MFP fp, double a, double b) {
    double res = 0.0, step = (b - a) / DIVN;

    int i;
    for (i = 0; i < DIVN; ++i)
        res += fp(a + i * step) * step;

    return res;
}

double numInt(MFP fp, double a, double b) {
    long i, divn = 10;
    double step, dif, res0, 
           res = (fp(b) + fp(a)) * (b - a) / 2;

    for (dif = 1.0; dif > 1E-6 || dif < -1E-6; divn *= 2) {
        res0 = res;
        step = (b - a) / divn;
        for (res = 0.0, i = 0; i < divn; ++i)
            res += fp(a + i * step) * step;
        dif = res - res0;
    }

    return res;
}

