#include <stdio.h>
#include <math.h>
double gauss(double (*f)(), double x1, double x2){
double xs[] = {-0.906179845938664, -0.538469310105683, .0, 0.538469310105683, 0.906179845938664};
double ws[] = {0.236926885056189, 0.478628670499366, 0.568888888888889, 0.478628670499366, 0.236926885056189};
int n = sizeof(xs)/sizeof(xs[0]);
double res = 0;
int i;
for(i=0; i<n; ++i){
res += ws[i] * (*f)((x2+x1)/2. + (x2-x1)/2.*xs[i]);
}
res *= (x2-x1)/2.;
return res;
}
double f(double x){
return 1/(1+x*x);
}
int main(void){
double x1 = 0.33, x2 = x1+1;
printf("gauss: %.10f\n", gauss
(f
, x1
, x2
)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxtYXRoLmg+Cgpkb3VibGUgZ2F1c3MoZG91YmxlICgqZikoKSwgZG91YmxlIHgxLCBkb3VibGUgeDIpewogICAgZG91YmxlIHhzW10gPSB7LTAuOTA2MTc5ODQ1OTM4NjY0LCAtMC41Mzg0NjkzMTAxMDU2ODMsIC4wLCAwLjUzODQ2OTMxMDEwNTY4MywgMC45MDYxNzk4NDU5Mzg2NjR9OwogICAgZG91YmxlIHdzW10gPSB7MC4yMzY5MjY4ODUwNTYxODksIDAuNDc4NjI4NjcwNDk5MzY2LCAwLjU2ODg4ODg4ODg4ODg4OSwgMC40Nzg2Mjg2NzA0OTkzNjYsIDAuMjM2OTI2ODg1MDU2MTg5fTsKICAgIGludCBuID0gc2l6ZW9mKHhzKS9zaXplb2YoeHNbMF0pOwogICAgZG91YmxlIHJlcyA9IDA7CiAgICBpbnQgaTsKICAgIGZvcihpPTA7IGk8bjsgKytpKXsKICAgICAgICByZXMgKz0gd3NbaV0gKiAoKmYpKCh4Mit4MSkvMi4gKyAoeDIteDEpLzIuKnhzW2ldKTsKICAgIH0KICAgIHJlcyAqPSAoeDIteDEpLzIuOwogICAgcmV0dXJuIHJlczsKfQoKZG91YmxlIGYoZG91YmxlIHgpewogICAgcmV0dXJuIDEvKDEreCp4KTsKfQppbnQgbWFpbih2b2lkKXsKICAgIGRvdWJsZSB4MSA9IDAuMzMsIHgyID0geDErMTsKICAgIHByaW50ZigiZ2F1c3M6ICUuMTBmXG4iLCBnYXVzcyhmLCB4MSwgeDIpKTsKICAgIHByaW50ZigiYXJ0YW46ICUuMTBmXG4iLCBhdGFuKHgyKSAtIGF0YW4oeDEpKTsKICAgIHJldHVybiAwOwp9Cg==