/// <summary>Asserts that two floating point values are almost equal</summary> /// <param name="expected">Expected value</param> /// <param name="actual">Actual value</param> private void assertAlmostEqual(float expected, float actual) { if (!FloatHelper.AreAlmostEqual(expected, actual, 1)) { Assert.AreEqual(expected, actual); } }
private bool arrayContains(float[] array, float toCheck) { for (int index = 0; index < array.Length; ++index) { bool almostEqual = FloatHelper.AreAlmostEqual( array[index], toCheck, Specifications.MaximumDeviation ); if (almostEqual) { return(true); } } return(false); }
/// <summary>Asserts that two vectors are equal within the accepted deviation</summary> /// <param name="expected">Value the vector is expected to have</param> /// <param name="actual">Actual value that the vector has</param> /// <param name="message">Message to describe the assertion</param> public static void AssertAreEqual(Vector3 expected, Vector3 actual, string message) { // Determine whether the values are within a reasonable range to the expected results bool almostEqual = FloatHelper.AreAlmostEqual(expected.X, actual.X, 1) && FloatHelper.AreAlmostEqual(expected.Y, actual.Y, 1) && FloatHelper.AreAlmostEqual(expected.Z, actual.Z, 1); // If the values are off, let NUnit do its (zero-tolerance) own check which we // already know will fail, but which saves us from writing our own NUnit assertion if (!almostEqual) { Assert.AreEqual(expected, actual, message); } }
public void TestRandomPointWithin() { Disc2 testDisc = new Disc2(new Vector2(123.4f, 567.8f), 9.0f); DefaultRandom randomNumberGenerator = new DefaultRandom(); for (int index = 0; index < Specifications.ProbabilisticFunctionSamples; ++index) { Vector2 randomPoint = testDisc.RandomPointWithin(randomNumberGenerator); float randomPointDistance = (testDisc.Center - randomPoint).Length(); if (!FloatHelper.AreAlmostEqual(randomPointDistance, testDisc.Radius, 36)) { Assert.LessOrEqual(randomPointDistance, testDisc.Radius); } } }
/// <summary>Ensures that two vectors are equal</summary> /// <param name="expected">Expected vector</param> /// <param name="actual">Actual vector</param> /// <param name="deltaUlps">Allowed deviation in representable floating point values</param> /// <param name="message">Message to display when the vectors are not equal</param> public static void AreAlmostEqual( Vector3 expected, Vector3 actual, int deltaUlps, string message ) { bool almostEqual = FloatHelper.AreAlmostEqual(expected.X, actual.X, deltaUlps) && FloatHelper.AreAlmostEqual(expected.Y, actual.Y, deltaUlps) && FloatHelper.AreAlmostEqual(expected.Z, actual.Z, deltaUlps); if (almostEqual) { return; } // Now we already know that the two vectors are not equal even within the allowed // deviation (delta argument). In order to force NUnit to output a good error // message, we now let NUnit do the job again fully well knowing that it will // fail. This allows for deltas and good NUnit integration at the same time. Assert.AreEqual(expected, actual, message); }
/// <summary>Determines whether two unified scalar values are nearly equal</summary> /// <param name="left">Unified scalar value to compare on the left side</param> /// <param name="right">Unified scalar value to compare on the right side</param> /// <param name="deltaUlps"> /// Allowed deviation in representable floating point values for each of the /// unified vector's fields /// </param> /// <returns>True if the provided unified scalar values are nearly equal</returns> private static bool areAlmostEqual(UniScalar left, UniScalar right, int deltaUlps) { return (FloatHelper.AreAlmostEqual(left.Fraction, right.Fraction, deltaUlps) && FloatHelper.AreAlmostEqual(left.Offset, right.Offset, deltaUlps)); }