1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

feat: add double overload for Bias (#60)

This commit is contained in:
Oliver Booth 2023-04-03 15:00:13 +01:00
parent f131c281cf
commit 3f147c98b2
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
2 changed files with 29 additions and 20 deletions

View File

@ -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]

View File

@ -25,6 +25,20 @@ public static class MathUtility
return value / ((1.0f / bias - 2.0f) * (1.0f - value) + 1.0f);
}
/// <summary>
/// Applies a simple bias function to value.
/// </summary>
/// <param name="value">The value to which the bias function will be applied.</param>
/// <param name="bias">The bias value. Valid values range from 0-1.</param>
/// <returns>The biased result.</returns>
/// <remarks>
/// If <paramref name="bias" /> is less than 0.5, <paramref name="value" /> will be shifted downward; otherwise, upward.
/// </remarks>
public static double Bias(double value, double bias)
{
return value / ((1.0 / bias - 2.0) * (1.0 - value) + 1.0);
}
/// <summary>
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
/// </summary>