diff --git a/X10D.Tests/src/Math/MathUtilityTests.cs b/X10D.Tests/src/Math/MathUtilityTests.cs index e267adf..4056a54 100644 --- a/X10D.Tests/src/Math/MathUtilityTests.cs +++ b/X10D.Tests/src/Math/MathUtilityTests.cs @@ -1,4 +1,4 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.VisualStudio.TestTools.UnitTesting; #if !NET6_0_OR_GREATER using X10D.Core; #endif @@ -12,36 +12,31 @@ public class MathUtilityTests [TestMethod] public void Bias_ReturnsCorrectResult_WhenBiasIsLessThanPointFive() { - const float value = 0.5f; - const float bias = 0.3f; + double doubleResult = MathUtility.Bias(0.5, 0.3); + float floatResult = MathUtility.Bias(0.5f, 0.3f); - const float expected = 0.3f; - float result = MathUtility.Bias(value, bias); - - Assert.AreEqual(expected, result, 1e-6f); + Assert.AreEqual(0.3, doubleResult, 1e-4); + Assert.AreEqual(0.3f, floatResult, 1e-4f); } [TestMethod] public void Bias_ReturnsCorrectResult_WhenBiasIsEqualToPointFive() { - const float value = 0.5f; - const float bias = 0.5f; + double doubleResult = MathUtility.Bias(0.5, 0.5); + float floatResult = MathUtility.Bias(0.5f, 0.5f); - float result = MathUtility.Bias(value, bias); - - Assert.AreEqual(value, result, 1e-6f); + Assert.AreEqual(0.5, doubleResult, 1e-4); + Assert.AreEqual(0.5f, floatResult, 1e-4f); } [TestMethod] public void Bias_ReturnsCorrectResult_WhenBiasIsGreaterThanPointFive() { - const float value = 0.5f; - const float bias = 0.8f; + double doubleResult = MathUtility.Bias(0.5, 0.8); + float floatResult = MathUtility.Bias(0.5f, 0.8f); - const float expected = 0.8f; - float result = MathUtility.Bias(value, bias); - - Assert.AreEqual(expected, result, 1e-6f); + Assert.AreEqual(0.8, doubleResult, 1e-4); + Assert.AreEqual(0.8f, floatResult, 1e-4f); } [TestMethod] @@ -50,8 +45,8 @@ public class MathUtilityTests double doubleResult = MathUtility.GammaToLinear(0.25, 1.0); float floatResult = MathUtility.GammaToLinear(0.25f, 1.0f); - Assert.AreEqual(0.25, doubleResult); - Assert.AreEqual(0.25f, floatResult); + Assert.AreEqual(0.25, doubleResult, 1e-6); + Assert.AreEqual(0.25f, floatResult, 1e-6f); } [TestMethod] diff --git a/X10D/src/Math/MathUtility.cs b/X10D/src/Math/MathUtility.cs index f292b43..7e1bd59 100644 --- a/X10D/src/Math/MathUtility.cs +++ b/X10D/src/Math/MathUtility.cs @@ -25,6 +25,20 @@ public static class MathUtility return value / ((1.0f / bias - 2.0f) * (1.0f - value) + 1.0f); } + /// + /// Applies a simple bias function to value. + /// + /// The value to which the bias function will be applied. + /// The bias value. Valid values range from 0-1. + /// The biased result. + /// + /// If is less than 0.5, will be shifted downward; otherwise, upward. + /// + public static double Bias(double value, double bias) + { + return value / ((1.0 / bias - 2.0) * (1.0 - value) + 1.0); + } + /// /// Converts a gamma-encoded value to a linear value using a gamma value of 2.2. ///