public void Solve_2_1() { Test(new[] { 0.0 }, 0, 0); Test(new[] { -1.0 }, 0, 1); Test(new[] { 2.0 }, 0, -8); Test(new[] { 0.0 }, 1, 0); Test(new[] { -2.0 }, 2, 12); Test(new[] { 1.0 }, 1, -2); Test(new[] { -3.0, 0.0, 3.0 }, -9, 0); Test(new[] { -3.0 }, -5, 12); Test(new[] { 5.0 }, -7, -90); Test(new[] { -4.0, 2.0 }, -12, 16); Test(new[] { -1.5, 3.0 }, -6.75, -6.75); Test(new[] { -3.0, 1.0, 2.0 }, -7, 6); Test(new[] { -7.0, -5.0, 12.0 }, -109, -420); void Test(double[] expected, double c, double d) { var actual = target2(c, d); CollectionAssert.AreEqual(expected, actual); var f = CubicEquation1.CreateFunction(c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } }
public void Solve_1() { void Test(double a, double b, double c) { var actual = target1(a, b, c); var det = b * b - 4 * a * c; // Errors can be occurred. Assert.AreEqual(det < 0 ? 0 : det == 0 ? 1 : 2, actual.Length); var f = QuadraticEquation1.CreateFunction(a, b, c); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x), -9); } } // A case with error for c - b * b / 4. Test(100, 80, 16); for (int a = -50; a <= 50; a++) { if (a != 0) { for (int b = -50; b <= 50; b++) { for (int c = -50; c <= 50; c++) { Test(a, b, c); } } } } }
public void Solve_2() { void Test(double c) { var actual = target2(c); var det = -c; Assert.AreEqual(det < 0 ? 0 : det == 0 ? 1 : 2, actual.Length); var f = QuadraticEquation1.CreateFunction(c); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } for (int c = -1000; c <= 1000; c++) { Test(c); } for (int c1 = 1; c1 <= 100; c1++) { for (double c2 = -100; c2 <= 100; c2++) { Test(c2 / c1); } } }
public void Solve_2_2() { Test(0, 2); Test(0, -100); Test(3, 0); Test(1, 1); Test(10, -10); Test(-7, 0); Test(-10, 127); Test(-1, -10); Test(-100, 90); Test(-15, -4); Test(-1.5, Math.Sqrt(2) / 2); Test(-6 * Math.Pow(2, 1 / 3.0), -8); void Test(double c, double d) { var actual = target2(c, d); var det = (-4 * c * c * c - 27 * d * d).RoundAlmost(); Assert.AreEqual(c == 0 & d == 0 || det < 0 ? 1 : det == 0 ? 2 : 3, actual.Length); var f = CubicEquation1.CreateFunction(c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } }
public void Pow_Double() { Assert.AreEqual(Math.Pow(1.1, 7), Numbers.Pow(1.1, 7)); Assert2.AreNearlyEqual(Math.Pow(1.08, 20), Numbers.Pow(1.08, 20)); // e Assert2.AreNearlyEqual(Math.Pow(1.000001, 1000000), Numbers.Pow(1.000001, 1000000), -10); }
public void Pow_Decimal() { Assert.AreEqual((decimal)Math.Pow(1.2, 4), Numbers.Pow(1.2m, 4)); Assert2.AreNearlyEqual((decimal)Math.Pow(1.1, 7), Numbers.Pow(1.1m, 7)); Assert2.AreNearlyEqual((decimal)Math.Pow(1.08, 20), Numbers.Pow(1.08m, 20)); // e Assert2.AreNearlyEqual((decimal)Math.Pow(1.000001, 1000000), Numbers.Pow(1.000001m, 1000000), -9); }
public void Last_Sqrt() { Assert2.AreNearlyEqual(10, BinarySearch.Last(0.0, 100, x => x * x < 100), -9); Assert2.AreNearlyEqual(5, BinarySearch.Last(0.0, 5, x => x * x < 100), -9); Assert.AreEqual(15, BinarySearch.Last(15.0, 20, x => x * x < 100)); Assert2.AreNearlyEqual(Math.Sqrt(3), BinarySearch.Last(0.0, 10, x => x * x <= 3), -9); }
public void Pow() { void Test(double x, double p) => Assert2.AreNearlyEqual(Math.Pow(x, p), ElementaryFunctions.Pow(x, p)); Test(2.0, 3.0); Test(2.0, 0.5); Test(Math.PI, Math.E); }
public void Sqrt() { void Test(double x) => Assert2.AreNearlyEqual(Math.Sqrt(x), ElementaryFunctions2.Sqrt(x)); Test(1.0); Test(2.0); Test(3.0); Test(127.0); }
public void Cbrt() { void Test(double x) => Assert2.AreNearlyEqual(Math.Pow(x, 1.0 / 3), ElementaryFunctions2.Cbrt(x)); Test(1.0); Test(2.0); Test(3.0); Test(127.0); }
public void Log() { void Test(double x) => Assert2.AreNearlyEqual(Math.Log(x), ElementaryFunctions2.Log(x)); Test(1.0); Test(2.0); Test(3.0); Test(0.5); Test(Math.E); }
public void Exp() { void Test(double x) => Assert2.AreNearlyEqual(Math.Exp(x), ElementaryFunctions2.Exp(x)); Test(1.0); Test(-1.0); Test(0.5); Test(2.0); Test(Math.PI); }
public void Log() { void Test(double x) => Assert2.AreNearlyEqual(Math.Log(x), ElementaryFunctions.Log(x)); // x=1 から離れるほど誤差が大きくなります。 Test(1.0); Test(2.0); Test(3.0); Test(0.5); Test(Math.E); }
public void Exp() { void Test(double x) => Assert2.AreNearlyEqual(Math.Exp(x), ElementaryFunctions.Exp(x)); // x=0 から離れるほど誤差が大きくなります。 Test(1.0); Test(-1.0); Test(0.5); Test(2.0); Test(Math.PI); }
public void Calculations() { var v1 = new Vector(2, 2); var v2 = new Vector(3, 4); Assert2.AreNearlyEqual(2 * Math.Sqrt(2), v1.Norm); Assert.AreEqual(5, v2.Norm); Assert2.AreNearlyEqual(Math.PI / 4, v1.Angle); Assert2.AreNearlyEqual(4.0 / 3, Math.Tan(v2.Angle)); Assert.AreEqual(1, Vector.Area(v1, v2)); }
public void AreNearlyEqual_Special() { var pi = Math.Sqrt(12) * Enumerable.Range(0, 40).Sum(i => 1 / ((2 * i + 1) * Math.Pow(-3, i))); // They are exactly equal in the decimal. Assert2.AreNearlyEqual(Math.PI, pi, -30); var e = Math.Pow(1.00000001, 100000000); Assert2.AreNearlyEqual(Math.E, e, -7); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(Math.E, e, -8)).Message); }
public void Log_b() { void Test(double x, double b) => Assert2.AreNearlyEqual(Math.Log(x, b), ElementaryFunctions.Log(x, b), -9); Test(1.0, 2); Test(2.0, 2); Test(0.5, 2); Test(1.0, 10); Test(2.0, 10); Test(0.5, 10); Test(Math.E, Math.E); }
public void Tan() { void Test(double expected, double x) => Assert2.AreNearlyEqual(expected, ElementaryFunctions.Tan(x)); Test(Math.Sqrt(3), Math.PI / 3); Test(1.0, Math.PI / 4); Test(1 / Math.Sqrt(3), Math.PI / 6); Test(0.0, 0); Test(-1 / Math.Sqrt(3), -Math.PI / 6); Test(-1.0, -Math.PI / 4); Test(-Math.Sqrt(3), -Math.PI / 3); Test(2 - Math.Sqrt(3), Math.PI / 12); }
public void Inverse() { void Test(double x) => Assert2.AreNearlyEqual(1 / x, ElementaryFunctions.Inverse(x)); Test(1.0); Test(0.001); Test(0.00081); Test(0.15); Test(0.2); Test(3.0); Test(7.0); Test(256.0); Test(12345); }
public void Calculations() { // IL で default(T) と new T() は同一になるようです。 Assert.AreEqual(new VectorInit(0, 0), default); Assert.AreEqual(new VectorInit(0, 0), new VectorInit()); var v1 = new VectorInit(2, 2); var v2 = new VectorInit(3, 4); Assert2.AreNearlyEqual(2 * Math.Sqrt(2), v1.Norm); Assert.AreEqual(5, v2.Norm); Assert2.AreNearlyEqual(Math.PI / 4, v1.Angle); Assert2.AreNearlyEqual(4.0 / 3, Math.Tan(v2.Angle)); }
public void AreNearlyEqual_Generic() { Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <Uri>(null, null)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual("", null)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(123, 123)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <object>(123F, 123D)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <object>(123M, 123D)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <object>(new[] { 123 }, new[] { 123D })).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <object>(null, 0)).Message); Assert2.AreNearlyEqual(12.34F, 12.3F, -1); Assert2.AreNearlyEqual(123, 123D); Assert2.AreNearlyEqual(123M, 123M); Assert2.AreNearlyEqual <object>(Math.PI, 3.14, -2); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual <object>(Math.PI, 3.14, -3)).Message); }
public void Cos() { void Test(double expected, double x) => Assert2.AreNearlyEqual(expected, ElementaryFunctions.Cos(x)); Test(1.0, 0); Test(Math.Sqrt(3) / 2, Math.PI / 6); Test(Math.Sqrt(2) / 2, Math.PI / 4); Test(0.5, Math.PI / 3); Test(0.0, Math.PI / 2); Test(-0.5, Math.PI * 2 / 3); Test(-Math.Sqrt(2) / 2, Math.PI * 3 / 4); Test(-Math.Sqrt(3) / 2, Math.PI * 5 / 6); Test(-1.0, Math.PI); Test((Math.Sqrt(6) + Math.Sqrt(2)) / 4, Math.PI / 12); }
public void Solve_1_2() { Test(3, 3, 3); Test(1, 0, 1); Test(1, -1, 0); Test(3, -2, -6); void Test(double b, double c, double d) { var actual = target1(b, c, d); var f = CubicEquation1.CreateFunction(1, b, c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } }
public void AreNearlyEqual_Double_9() { // A calculation with error. double D9(int digits) => Enumerable.Range(1, digits).Sum(i => Math.Pow(0.1, i)); // 1/9 = 0.1111... var expected = 1.0 / 9; var actual = D9(20); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert.AreEqual(expected, actual)).Message); Assert2.AreNearlyEqual(expected, actual); // They are exactly equal in the decimal. Assert2.AreNearlyEqual(expected, actual, -30); var actual12 = D9(12); Assert2.AreNearlyEqual(expected, actual12); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(expected, actual12, -13)).Message); }
public void AreNearlyEqual_Double() { // The test can be for other value types by casting. var target = 43.21; var values6 = new[] { 43.2099989, 43.209999, 43.21, 43.210001, 43.2100011 }; var values1 = new[] { 43.1099, 43.11, 43.215, 43.31, 43.3101 }; var values0 = new[] { 42.2099, 42.21, 43.215, 44.21, 44.2101 }; BoundaryTest(values6, -6); // 10^-6 BoundaryTest(values1, -1); // 10^-1 BoundaryTest(values0, 0); // 10^0 void BoundaryTest(double[] values, int digits) { Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(target, values[0], digits)).Message); Assert2.AreNearlyEqual(target, values[1], digits); Assert2.AreNearlyEqual(target, values[2], digits); Assert2.AreNearlyEqual(target, values[3], digits); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(target, values[4], digits)).Message); } }
public void Solve_1_1() { Test(new[] { 0.0 }, 1, 1, 0); Test(new[] { -2.0 }, 3, 4, 4); Test(new[] { -2.0 }, 1, -1, 2); Test(new[] { -3.0, 1.0 }, 1, -5, 3); void Test(double[] expected, double b, double c, double d) { var actual = target1(b, c, d); actual = Array.ConvertAll(actual, x => x.RoundAlmost()); CollectionAssert.AreEqual(expected, actual); var f = CubicEquation1.CreateFunction(1, b, c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } }
public void Solve_2() { void Test(double c, double d) { var actual = target2(c, d); var det = -4 * c * c * c - 27 * d * d; Assert.AreEqual(c == 0 & d == 0 || det < 0 ? 1 : det == 0 ? 2 : 3, actual.Length); var f = CubicEquation1.CreateFunction(c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x)); } } for (int c = -100; c <= 100; c++) { for (int d = 0; d <= 100; d++) { Test(c, d); } } for (int c1 = 1; c1 <= 20; c1++) { for (double c2 = -20; c2 <= 20; c2++) { for (int d1 = 1; d1 <= 20; d1++) { for (double d2 = -20; d2 <= 20; d2++) { Test(c2 / c1, d2 / d1); } } } } }
public void Solve_1() { void Test(double a, double b, double c, double d) { var actual = target1(a, b, c, d); var f = CubicEquation1.CreateFunction(a, b, c, d); foreach (var x in actual) { Assert2.AreNearlyEqual(0, f(x), -9); } } // A case with error for the determinant. Test(20, 8, 0, 0); Test(6, 47, 0, 0); Test(5, 33, 0, 0); Test(2, 49, 0, 0); Test(5, 50, -6, 5); for (int a = -15; a <= 15; a++) { if (a != 0) { for (int b = -15; b <= 15; b++) { for (int c = -15; c <= 15; c++) { for (int d = -15; d <= 15; d++) { Test(a, b, c, d); } } } } } }
public void AreNearlyEqual_Double_NaN() { var values = new[] { double.NaN, double.NegativeInfinity, double.PositiveInfinity }; foreach (var v in values) { Console.WriteLine(Assert.ThrowsException <ArgumentOutOfRangeException>(() => Assert2.AreNearlyEqual(v, 0)).Message); Console.WriteLine(Assert.ThrowsException <AssertFailedException>(() => Assert2.AreNearlyEqual(0, v)).Message); Console.WriteLine(); } }
public void GetE() => Assert2.AreNearlyEqual(Math.E, ElementaryFunctions.GetE());