// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class Calculus { private const int N = 1000000; public delegate double Func(double x); public static double Integrate(Func f, double start, double finish) { double eps = (finish - start) / N; double sum = 0; for(int i = 0; i < N; i++) { sum += eps*f(start + eps * (0.5 + i)); } return sum; } } public class Program { public static double StandardNormal(double x) { return Math.Exp(-x*x/2)/Math.Sqrt(2*Math.PI); } public static void Main(string[] args) { //Console.WriteLine(Math.PI / 2); //Console.WriteLine(Calculus.Integrate((double x) => Math.Sqrt(1 - x*x), -1, 1)); Console.WriteLine(Calculus.Integrate(StandardNormal, -10, 1.345067)); } } }