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 Test_Divide_1() { var run_count = 1000000; Func <Tuple <ulong, ulong, ulong, ulong>, bool> predicate = (a) => { var m0 = a.Item1; var m1 = a.Item2; var n0 = a.Item3; var n1 = a.Item4; if (0 == n0 && 0 == n1) { try { var d0 = DoubleArithmetic.Divide(m0, m1, n0, n1, out UInt64 d1); return(false); } catch (DivideByZeroException) { return(true); } } else { var d0 = DoubleArithmetic.Divide(m0, m1, n0, n1, out UInt64 d1); var m = UInt64ArrayToBigIntegerUnsigned(m0, m1); var n = UInt64ArrayToBigIntegerUnsigned(n0, n1); var p = m / n; var d = UInt64ArrayToBigIntegerUnsigned(d0, d1); return(p == d); } }; FsCheck.Prop.ForAll(predicate).Check(new Configuration() { MaxNbOfTest = run_count }); return; }
public void TestThatNaiveHypotFailsWhenHypotWorks() { // test with large values const double m = 1e308; var x = 0.3 * m; var y = 0.4 * m; var naive = Math.Sqrt(x * x + y * y); var hypot = DoubleArithmetic.Hypotenuse(x, y); // assert that naive method blows up Assert.IsTrue(double.IsInfinity(naive)); // assert that our method gives the correct answer Assert.AreEqual(0.5 * m, hypot); // test with small values const double eps = 1e-324; x = 3 * eps; y = 4 * eps; naive = Math.Sqrt(x * x + y * y); hypot = DoubleArithmetic.Hypotenuse(x, y); // assert that naive method vanishes Assert.AreEqual(naive, 0); // assert that our method gives the correct answer Assert.AreEqual(5 * eps, hypot); }
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 static bool Multiply(ulong first, ulong second, out ulong result) { ulong t; result = DoubleArithmetic.BigMul(first, second, out t); return(0 != t); }
public bool Test_BigRem_1(Tuple <ulong, ulong, ulong> a) { var p0 = a.Item1; var p1 = a.Item2; var d0 = a.Item3; if (0 == d0) { return(true); } if (p1 >= d0) { return(true); } var p = UInt64ArrayToBigIntegerUnsigned(p0, p1); var d = UInt64ArrayToBigIntegerUnsigned(d0); var r = p % d; var r0 = DoubleArithmetic.BigRemNoThrow(p0, p1, d0); if (r == UInt64ArrayToBigIntegerUnsigned(r0)) { return(true); } return(false); }
public bool Test_BigRem_0(Tuple <ulong, ulong, ulong, ulong, ulong, ulong> a) { var p0 = a.Item1; var p1 = a.Item2; var p2 = a.Item3; var p3 = a.Item4; var d0 = a.Item5; var d1 = a.Item6; if (0 == d0 && 0 == d1) { return(true); } if (DoubleArithmetic.GreaterThanOrEqual(p2, p3, d0, d1)) { return(true); } var p = UInt64ArrayToBigIntegerUnsigned(p0, p1, p2, p3); var d = UInt64ArrayToBigIntegerUnsigned(d0, d1); var r = p % d; var s = (System.Numerics.BigInteger?)null; try { var r0 = DoubleArithmetic.BigRemNoThrow(p0, p1, p2, p3, d0, d1, out var r1); s = UInt64ArrayToBigIntegerUnsigned(r0, r1); } catch (OverflowException) { return(false); } if (r == s) { return(true); } return(false); }
public bool Test_Divide_1(Tuple <ulong, ulong, ulong, ulong> a) { var m0 = a.Item1; var m1 = a.Item2; var n0 = a.Item3; var n1 = a.Item4; if (0 == n0 && 0 == n1) { try { var d0 = DoubleArithmetic.Divide(m0, m1, n0, n1, out UInt64 d1); return(false); } catch (DivideByZeroException) { return(true); } } else { var d0 = DoubleArithmetic.Divide(m0, m1, n0, n1, out UInt64 d1); var m = UInt64ArrayToBigIntegerUnsigned(m0, m1); var n = UInt64ArrayToBigIntegerUnsigned(n0, n1); var p = m / n; var d = UInt64ArrayToBigIntegerUnsigned(d0, d1); return(p == d); } }
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 TestHypotWithSomeKnownValues() { Assert.AreEqual(13.0, DoubleArithmetic.Hypotenuse(5.0, 12.0)); Assert.AreEqual(25.0, DoubleArithmetic.Hypotenuse(7.0, 24.0)); Assert.AreEqual(641.0, DoubleArithmetic.Hypotenuse(200.0, 609.0)); Assert.AreEqual(661.0, DoubleArithmetic.Hypotenuse(300.0, 589.0)); }
public bool Test_BigSquare_2(ulong m0, ulong m1) { var mm = UInt64ArrayToBigIntegerUnsigned(m0, m1); var p0p = mm * mm; var p10 = DoubleArithmetic.BigSquare(m0, m1, out UInt64 p11, out UInt64 p12, out UInt64 p13); var p1p = UInt64ArrayToBigIntegerUnsigned(p10, p11, p12, p13); return(p0p == p1p); }
public bool Test_BigMul_2(ulong m0, ulong n0) { var mm = UInt64ArrayToBigIntegerUnsigned(m0); var nn = UInt64ArrayToBigIntegerUnsigned(n0); var p0p = mm * nn; var p10 = DoubleArithmetic.BigMul(m0, n0, out UInt64 p11); var p1p = UInt64ArrayToBigIntegerUnsigned(p10, p11); return(p0p == p1p); }
public bool Test_BigMul_3(ulong m0, ulong n0) { var mm = UInt64ArrayToBigInteger(m0); var nn = UInt64ArrayToBigInteger(n0); var p0p = mm * nn; var p10 = DoubleArithmetic.BigMul(unchecked ((Int64)m0), unchecked ((Int64)n0), out Int64 p11); var p1p = UInt64ArrayToBigInteger(p10, unchecked ((UInt64)p11)); return(p0p == p1p); }
public bool Test_BigMul_1(ulong m0, ulong m1, ulong n0, ulong n1) { var mm = UInt64ArrayToBigInteger(m0, m1); var nn = UInt64ArrayToBigInteger(n0, n1); var p0p = mm * nn; var p10 = DoubleArithmetic.BigMul(m0, unchecked ((Int64)m1), n0, unchecked ((Int64)n1), out UInt64 p11, out UInt64 p12, out Int64 p13); var p1p = UInt64ArrayToBigInteger(p10, p11, p12, unchecked ((UInt64)p13)); return(p0p == p1p); }
public static bool Multiply(long first, long second, out long result) { long t; var r = DoubleArithmetic.BigMul(first, second, out t); result = unchecked((long)r); if (0 > (first ^ second)) { if ((-1 == t && 0 > r) || (0 == t && 0 == r)) { return false; } } else { if (0 == t && r <= (ulong)long.MaxValue) return false; } return true; }
public void Test_BigMul_1() { var run_count = 1000000; Func <Tuple <ulong, ulong, ulong, ulong>, bool> predicate = (a) => { var m0 = a.Item1; var m1 = a.Item2; var n0 = a.Item3; var n1 = a.Item4; var d0 = DoubleArithmetic.BigMul(m0, m1, n0, n1, out UInt64 d1, out UInt64 d2, out UInt64 d3); var m = UInt64ArrayToBigIntegerUnsigned(m0, m1); var n = UInt64ArrayToBigIntegerUnsigned(n0, n1); var p = m * n; var d = UInt64ArrayToBigIntegerUnsigned(d0, d1, d2, d3); return(p == d); };
public bool Test_BigRem_NoThrow_0(Tuple <ulong, ulong, ulong, ulong, ulong, ulong> a) { var p0 = a.Item1; var p1 = a.Item2; var p2 = a.Item3; var p3 = a.Item4; var d0 = a.Item5; var d1 = a.Item6; if (0 == d0 && 0 == d1) { return(true); } if (DoubleArithmetic.GreaterThanOrEqual(p2, p3, d0, d1)) { var r0 = DoubleArithmetic.BigRemNoThrow(p0, p1, p2, p3, d0, d1, out var r1); } return(true); }
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)); }
public void TestHypot3D() { var hypot = DoubleArithmetic.Hypotenuse(3, 4, 12); Assert.AreEqual(13.0, hypot); }