public void ComparePowIntAndMathSqrtInAccuracy() { var pi6PowInt = DoubleArithmetic.PowInt(Math.PI, 6); var oneHalfTo4PowInt = DoubleArithmetic.PowInt(0.5, 4); var piSquaredPowInt = DoubleArithmetic.PowInt(Math.PI, 2); var pi6 = DoubleArithmetic.Pow6(Math.PI); var pi6Math = Math.Pow(Math.PI, 6.0); var oneHalfTo4Math = Math.Pow(0.5, 4.0); var piSquaredMath = Math.Pow(Math.PI, 2.0); const double oneHalfTo4 = 1.0 / 16.0; const double piSquared = Constants.PISquared; double[] powIntErrors = { DoubleArithmetic.RelativeError(pi6, pi6PowInt), DoubleArithmetic.RelativeError(oneHalfTo4, oneHalfTo4PowInt), DoubleArithmetic.RelativeError(piSquared, piSquaredPowInt) }; double[] mathErrors = { DoubleArithmetic.RelativeError(pi6, pi6Math), DoubleArithmetic.RelativeError(oneHalfTo4, oneHalfTo4Math), DoubleArithmetic.RelativeError(piSquared, piSquaredMath) }; for (var i = 0; i < powIntErrors.Length; i++) { Assert.IsTrue(mathErrors[i] >= powIntErrors[i]); } }
public void ComparePowIntAndMathSqrtInSpeed() { Math.PI.Pow(6); var sw = Stopwatch.StartNew(); for (var i = 0; i < 250000; i++) { DoubleArithmetic.PowInt(Math.PI, 6); } sw.Stop(); var arth = sw.ElapsedMilliseconds; sw.Restart(); for (var i = 0; i < 250000; i++) { Math.Pow(Math.PI, 6.0); } sw.Stop(); var math = sw.ElapsedMilliseconds; Assert.IsTrue(math > arth); }
public void ComparePowIntAndNaiveInSpeed() { DoubleArithmetic.Pow(Math.PI, 6); var sw = Stopwatch.StartNew(); for (var i = 0; i < 250000; i++) { DoubleArithmetic.PowInt(Math.PI, 6); } sw.Stop(); var arth = sw.ElapsedMilliseconds; sw.Restart(); for (var i = 0; i < 250000; i++) { NaivePower(Math.PI, 6); } sw.Stop(); var naive = sw.ElapsedMilliseconds; Assert.IsTrue(naive > arth); }
public void TestPowIntCorrectnessAndConsistency() { var pi6 = DoubleArithmetic.PowInt(Math.PI, 6); var pi18 = DoubleArithmetic.PowInt(Math.PI, 18); var pi17 = DoubleArithmetic.PowInt(Math.PI, 17); var oneHalfTo4 = DoubleArithmetic.PowInt(0.5, 4); var e218 = DoubleArithmetic.PowInt(Math.E, 218); var piToMinus2 = DoubleArithmetic.PowInt(Math.PI, -2); var pi6Expected = DoubleArithmetic.Pow6(Math.PI); Assert.AreEqual(32.0, DoubleArithmetic.PowInt(2.0, 5)); Assert.AreEqual(81.0, DoubleArithmetic.PowInt(3.0, 4)); Assert.AreEqual(pi6Expected, pi6, 1e-12, "on pi ^ 6, delta = {0}", Math.Abs(pi6 - pi6Expected)); Assert.AreEqual(pi18, pi17 * Math.PI, 1e-6, "on pi^18=p^17/pi, delta = {0}", Math.Abs(pi18 - pi17 * Math.PI)); Assert.AreEqual(1.0 / 16.0, oneHalfTo4, 0, "on 0.5^4=1/16, delta = {0}", Math.Abs(1.0 / 16.0 - oneHalfTo4)); Assert.AreEqual(218.0, Math.Log(e218), 0, "on ln(e^218)=218, delta = {0}", Math.Abs(218.0 - Math.Log(e218))); Assert.AreEqual(1.0 / Constants.PISquared, piToMinus2, 1e-16, "on 1/(pi^2), delta = {0}", Math.Abs(1.0 / Constants.PISquared - piToMinus2)); }
/// <summary> /// x * 2 ^ n /// </summary> /// <param name="x"></param> /// <param name="n"></param> /// <returns></returns> public static double TimesTwoTo(double x, int n) { return(x * DoubleArithmetic.PowInt(2.0, n)); }