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 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 TestHypot3D() { var hypot = DoubleArithmetic.Hypotenuse(3, 4, 12); Assert.AreEqual(13.0, hypot); }