1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 19:28:48 +00:00

Fix precision errors by specifying delta 1e-6

This commit is contained in:
Oliver Booth 2022-04-30 11:07:57 +01:00
parent 7577fc1c99
commit 3ae2827fd5
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 70 additions and 79 deletions

View File

@ -10,23 +10,23 @@ public class DoubleTests
[TestMethod]
public void DegreesToRadians_ShouldBeCorrect()
{
Assert.AreEqual(System.Math.PI, 180.0.DegreesToRadians());
Assert.AreEqual(System.Math.PI * 1.5, 270.0.DegreesToRadians());
Assert.AreEqual(0.0, 0.0.DegreesToRadians());
Assert.AreEqual(0.017453292519943295, 1.0.DegreesToRadians());
Assert.AreEqual(0.10471975511965978, 6.0.DegreesToRadians());
Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians());
Assert.AreEqual(System.Math.PI, 180.0.DegreesToRadians(), 1e-6);
Assert.AreEqual(System.Math.PI * 1.5, 270.0.DegreesToRadians(), 1e-6);
Assert.AreEqual(0.0, 0.0.DegreesToRadians(), 1e-6);
Assert.AreEqual(0.017453292519943295, 1.0.DegreesToRadians(), 1e-6);
Assert.AreEqual(0.10471975511965978, 6.0.DegreesToRadians(), 1e-6);
Assert.AreEqual(0.20943951023931956, 12.0.DegreesToRadians(), 1e-6);
}
[TestMethod]
public void RadiansToDegrees_ShouldBeCorrect()
{
Assert.AreEqual(180.0, System.Math.PI.RadiansToDegrees());
Assert.AreEqual(360.0, (2.0 * System.Math.PI).RadiansToDegrees());
Assert.AreEqual(0.0, 0.0.RadiansToDegrees());
Assert.AreEqual(1.0, 0.017453292519943295.RadiansToDegrees());
Assert.AreEqual(6.000000000000001, 0.10471975511965978.RadiansToDegrees()); // rounding errors are fun
Assert.AreEqual(12.0, 0.20943951023931953.RadiansToDegrees());
Assert.AreEqual(180.0, System.Math.PI.RadiansToDegrees(), 1e-6);
Assert.AreEqual(360.0, (2.0 * System.Math.PI).RadiansToDegrees(), 1e-6);
Assert.AreEqual(0.0, 0.0.RadiansToDegrees(), 1e-6);
Assert.AreEqual(1.0, 0.017453292519943295.RadiansToDegrees(), 1e-6);
Assert.AreEqual(6.000000000000001, 0.10471975511965978.RadiansToDegrees(), 1e-6); // rounding errors are fun
Assert.AreEqual(12.0, 0.20943951023931953.RadiansToDegrees(), 1e-6);
}
[TestMethod]
@ -102,17 +102,17 @@ public class DoubleTests
[TestMethod]
public void Round_ShouldRoundToNearestInteger()
{
Assert.AreEqual(4.0, 3.5.Round());
Assert.AreEqual(7.0, 6.8.Round());
Assert.AreEqual(7.0, 7.2.Round());
Assert.AreEqual(4.0, 3.5.Round(), 1e-6);
Assert.AreEqual(7.0, 6.8.Round(), 1e-6);
Assert.AreEqual(7.0, 7.2.Round(), 1e-6);
}
[TestMethod]
public void Round_ShouldRoundToNearestMultiple()
{
Assert.AreEqual(5.0, 3.5.Round(5));
Assert.AreEqual(5.0, 7.0.Round(5));
Assert.AreEqual(10.0, 7.5.Round(5));
Assert.AreEqual(5.0, 3.5.Round(5), 1e-6);
Assert.AreEqual(5.0, 7.0.Round(5), 1e-6);
Assert.AreEqual(10.0, 7.5.Round(5), 1e-6);
}
[TestMethod]
@ -140,11 +140,11 @@ public class DoubleTests
[TestMethod]
public void Sqrt_ShouldBeCorrect_GivenValue()
{
Assert.AreEqual(0.0, 0.0.Sqrt());
Assert.AreEqual(1.414213562373095, 2.0.Sqrt());
Assert.AreEqual(3.0, 9.0.Sqrt());
Assert.AreEqual(4.0, 16.0.Sqrt());
Assert.AreEqual(100.0, 10000.0.Sqrt());
Assert.AreEqual(0.0, 0.0.Sqrt(), 1e-6);
Assert.AreEqual(1.414213562373095, 2.0.Sqrt(), 1e-6);
Assert.AreEqual(3.0, 9.0.Sqrt(), 1e-6);
Assert.AreEqual(4.0, 16.0.Sqrt(), 1e-6);
Assert.AreEqual(100.0, 10000.0.Sqrt(), 1e-6);
}
[TestMethod]
@ -171,81 +171,72 @@ public class DoubleTests
[TestMethod]
public void Acos_ShouldBeCorrect()
{
Assert.AreEqual(1.0471975511965979, 0.5.Acos());
Assert.AreEqual(1.0471975511965979, 0.5.Acos(), 1e-6);
}
[TestMethod]
public void Acosh_ShouldBeCorrect()
{
Assert.AreEqual(0.9624236501192069, 1.5.Acosh());
Assert.AreEqual(0.9624236501192069, 1.5.Acosh(), 1e-6);
}
[TestMethod]
public void Asin_ShouldBeCorrect()
{
Assert.AreEqual(0.5235987755982989, 0.5.Asin());
Assert.AreEqual(0.5235987755982989, 0.5.Asin(), 1e-6);
}
[TestMethod]
public void Asinh_ShouldBeCorrect()
{
Assert.AreEqual(1.1947632172871094, 1.5.Asinh());
Assert.AreEqual(1.1947632172871094, 1.5.Asinh(), 1e-6);
}
[TestMethod]
public void Atan_ShouldBeCorrect()
{
Assert.AreEqual(0.4636476090008061, 0.5.Atan());
Assert.AreEqual(0.4636476090008061, 0.5.Atan(), 1e-6);
}
[TestMethod]
public void Atanh_ShouldBeCorrect()
{
try
{
Assert.AreEqual(0.5493061443340548, 0.5.Atanh());
}
catch
{
// floating point rounding errors cause this value to be different on CI vs my local machine.
// I have no idea why, but here we are. if THIS assertion fails, we'll just throw it back
Assert.AreEqual(0.5493061443340549, 0.5.Atanh());
}
Assert.AreEqual(0.5493061443340549, 0.5.Atanh(), 1e-6);
}
[TestMethod]
public void Cos_ShouldBeCorrect()
{
Assert.AreEqual(0.8775825618903728, 0.5.Cos());
Assert.AreEqual(0.8775825618903728, 0.5.Cos(), 1e-6);
}
[TestMethod]
public void Cosh_ShouldBeCorrect()
{
Assert.AreEqual(2.352409615243247, 1.5.Cosh());
Assert.AreEqual(2.352409615243247, 1.5.Cosh(), 1e-6);
}
[TestMethod]
public void Sin_ShouldBeCorrect()
{
Assert.AreEqual(0.479425538604203, 0.5.Sin());
Assert.AreEqual(0.479425538604203, 0.5.Sin(), 1e-6);
}
[TestMethod]
public void Sinh_ShouldBeCorrect()
{
Assert.AreEqual(2.1292794550948173, 1.5.Sinh());
Assert.AreEqual(2.1292794550948173, 1.5.Sinh(), 1e-6);
}
[TestMethod]
public void Tan_ShouldBeCorrect()
{
Assert.AreEqual(0.5463024898437905, 0.5.Tan());
Assert.AreEqual(0.5463024898437905, 0.5.Tan(), 1e-6);
}
[TestMethod]
public void Tanh_ShouldBeCorrect()
{
Assert.AreEqual(0.46211715726000974, 0.5.Tanh());
Assert.AreEqual(0.46211715726000974, 0.5.Tanh(), 1e-6);
}
}

View File

@ -10,23 +10,23 @@ public class SingleTests
[TestMethod]
public void DegreesToRadians_ShouldBeCorrect()
{
Assert.AreEqual(MathF.PI, 180.0f.DegreesToRadians());
Assert.AreEqual(MathF.PI * 1.5f, 270.0f.DegreesToRadians());
Assert.AreEqual(0.0f, 0.0f.DegreesToRadians());
Assert.AreEqual(0.017453292f, 1.0f.DegreesToRadians());
Assert.AreEqual(0.10471976f, 6.0f.DegreesToRadians());
Assert.AreEqual(0.20943952f, 12.0f.DegreesToRadians());
Assert.AreEqual(MathF.PI, 180.0f.DegreesToRadians(), 1e-6f);
Assert.AreEqual(MathF.PI * 1.5f, 270.0f.DegreesToRadians(), 1e-6f);
Assert.AreEqual(0.0f, 0.0f.DegreesToRadians(), 1e-6f);
Assert.AreEqual(0.017453292f, 1.0f.DegreesToRadians(), 1e-6f);
Assert.AreEqual(0.10471976f, 6.0f.DegreesToRadians(), 1e-6f);
Assert.AreEqual(0.20943952f, 12.0f.DegreesToRadians(), 1e-6f);
}
[TestMethod]
public void RadiansToDegrees_ShouldBeCorrect()
{
Assert.AreEqual(180.0f, MathF.PI.RadiansToDegrees());
Assert.AreEqual(270.0f, (MathF.PI * 1.5f).RadiansToDegrees());
Assert.AreEqual(0.0, 0.0f.RadiansToDegrees());
Assert.AreEqual(0.99999994f, 0.017453292f.RadiansToDegrees()); // rounding errors are fun
Assert.AreEqual(6.0f, 0.10471976f.RadiansToDegrees());
Assert.AreEqual(12.0f, 0.20943952f.RadiansToDegrees());
Assert.AreEqual(180.0f, MathF.PI.RadiansToDegrees(), 1e-6f);
Assert.AreEqual(270.0f, (MathF.PI * 1.5f).RadiansToDegrees(), 1e-6f);
Assert.AreEqual(0.0, 0.0f.RadiansToDegrees(), 1e-6f);
Assert.AreEqual(0.99999994f, 0.017453292f.RadiansToDegrees(), 1e-6f); // rounding errors are fun
Assert.AreEqual(6.0f, 0.10471976f.RadiansToDegrees(), 1e-6f);
Assert.AreEqual(12.0f, 0.20943952f.RadiansToDegrees(), 1e-6f);
}
[TestMethod]
@ -102,17 +102,17 @@ public class SingleTests
[TestMethod]
public void Round_ShouldRoundToNearestInteger()
{
Assert.AreEqual(4.0f, 3.5f.Round());
Assert.AreEqual(7.0f, 6.8f.Round());
Assert.AreEqual(7.0f, 7.2f.Round());
Assert.AreEqual(4.0f, 3.5f.Round(), 1e-6f);
Assert.AreEqual(7.0f, 6.8f.Round(), 1e-6f);
Assert.AreEqual(7.0f, 7.2f.Round(), 1e-6f);
}
[TestMethod]
public void Round_ShouldRoundToNearestMultiple()
{
Assert.AreEqual(5.0f, 3.5f.Round(5));
Assert.AreEqual(5.0f, 7.0f.Round(5));
Assert.AreEqual(10.0f, 7.5f.Round(5));
Assert.AreEqual(5.0f, 3.5f.Round(5), 1e-6f);
Assert.AreEqual(5.0f, 7.0f.Round(5), 1e-6f);
Assert.AreEqual(10.0f, 7.5f.Round(5), 1e-6f);
}
[TestMethod]
@ -140,11 +140,11 @@ public class SingleTests
[TestMethod]
public void Sqrt_ShouldBeCorrect_GivenValue()
{
Assert.AreEqual(0.0f, 0.0f.Sqrt());
Assert.AreEqual(1.4142135f, 2.0f.Sqrt());
Assert.AreEqual(3.0f, 9.0f.Sqrt());
Assert.AreEqual(4.0f, 16.0f.Sqrt());
Assert.AreEqual(100.0f, 10000.0f.Sqrt());
Assert.AreEqual(0.0f, 0.0f.Sqrt(), 1e-6f);
Assert.AreEqual(1.4142135f, 2.0f.Sqrt(), 1e-6f);
Assert.AreEqual(3.0f, 9.0f.Sqrt(), 1e-6f);
Assert.AreEqual(4.0f, 16.0f.Sqrt(), 1e-6f);
Assert.AreEqual(100.0f, 10000.0f.Sqrt(), 1e-6f);
}
[TestMethod]
@ -171,72 +171,72 @@ public class SingleTests
[TestMethod]
public void Acos_ShouldBeCorrect()
{
Assert.AreEqual(1.0471975803375244f, 0.5f.Acos());
Assert.AreEqual(1.0471975803375244f, 0.5f.Acos(), 1e-6f);
}
[TestMethod]
public void Acosh_ShouldBeCorrect()
{
Assert.AreEqual(0.9624236822128296f, 1.5f.Acosh());
Assert.AreEqual(0.9624236822128296f, 1.5f.Acosh(), 1e-6f);
}
[TestMethod]
public void Asin_ShouldBeCorrect()
{
Assert.AreEqual(0.5235987901687622f, 0.5f.Asin());
Assert.AreEqual(0.5235987901687622f, 0.5f.Asin(), 1e-6f);
}
[TestMethod]
public void Asinh_ShouldBeCorrect()
{
Assert.AreEqual(1.19476318359375f, 1.5f.Asinh());
Assert.AreEqual(1.19476318359375f, 1.5f.Asinh(), 1e-6f);
}
[TestMethod]
public void Atan_ShouldBeCorrect()
{
Assert.AreEqual(0.46364760398864746, 0.5f.Atan());
Assert.AreEqual(0.46364760398864746, 0.5f.Atan(), 1e-6f);
}
[TestMethod]
public void Atanh_ShouldBeCorrect()
{
Assert.AreEqual(0.5493061542510986f, 0.5f.Atanh());
Assert.AreEqual(0.5493061542510986f, 0.5f.Atanh(), 1e-6f);
}
[TestMethod]
public void Cos_ShouldBeCorrect()
{
Assert.AreEqual(0.8775825500488281f, 0.5f.Cos());
Assert.AreEqual(0.8775825500488281f, 0.5f.Cos(), 1e-6f);
}
[TestMethod]
public void Cosh_ShouldBeCorrect()
{
Assert.AreEqual(2.352409601211548f, 1.5f.Cosh());
Assert.AreEqual(2.352409601211548f, 1.5f.Cosh(), 1e-6f);
}
[TestMethod]
public void Sin_ShouldBeCorrect()
{
Assert.AreEqual(0.4794255495071411, 0.5f.Sin());
Assert.AreEqual(0.4794255495071411, 0.5f.Sin(), 1e-6f);
}
[TestMethod]
public void Sinh_ShouldBeCorrect()
{
Assert.AreEqual(2.129279375076294f, 1.5f.Sinh());
Assert.AreEqual(2.129279375076294f, 1.5f.Sinh(), 1e-6f);
}
[TestMethod]
public void Tan_ShouldBeCorrect()
{
Assert.AreEqual(0.4794255495071411f, 0.5f.Tan());
Assert.AreEqual(0.4794255495071411f, 0.5f.Tan(), 1e-6f);
}
[TestMethod]
public void Tanh_ShouldBeCorrect()
{
Assert.AreEqual(0.46211716532707214f, 0.5f.Tanh());
Assert.AreEqual(0.46211716532707214f, 0.5f.Tanh(), 1e-6f);
}
}