// 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 MyMath { private const double LOG2 = 0.69314718055994530941; public static double Log(double x) { if(x > 1) { return LOG2 + Log(x / 2); } else if(x < 0.5) { return Log(2 * x) - LOG2; } else { double res = 0; double q = 1; for(int i = 1; i <= 50; i++) { q *= (x - 1); res += (i % 2 == 1 ? 1 : - 1) * q / i; } return res; } } } public class Program { public static void Main(string[] args) { Random rng = new Random(); for(int i = 0; i < 10; i++) { double x = Math.Pow(10, i - 4) * rng.NextDouble(); Console.WriteLine(x); Console.WriteLine(Math.Log(x)); Console.WriteLine(MyMath.Log(x)); } } } }