mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 02:05:43 +00:00
feat: add double overload for Bias (#60)
This commit is contained in:
parent
f131c281cf
commit
3f147c98b2
@ -1,4 +1,4 @@
|
|||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
#if !NET6_0_OR_GREATER
|
#if !NET6_0_OR_GREATER
|
||||||
using X10D.Core;
|
using X10D.Core;
|
||||||
#endif
|
#endif
|
||||||
@ -12,36 +12,31 @@ public class MathUtilityTests
|
|||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Bias_ReturnsCorrectResult_WhenBiasIsLessThanPointFive()
|
public void Bias_ReturnsCorrectResult_WhenBiasIsLessThanPointFive()
|
||||||
{
|
{
|
||||||
const float value = 0.5f;
|
double doubleResult = MathUtility.Bias(0.5, 0.3);
|
||||||
const float bias = 0.3f;
|
float floatResult = MathUtility.Bias(0.5f, 0.3f);
|
||||||
|
|
||||||
const float expected = 0.3f;
|
Assert.AreEqual(0.3, doubleResult, 1e-4);
|
||||||
float result = MathUtility.Bias(value, bias);
|
Assert.AreEqual(0.3f, floatResult, 1e-4f);
|
||||||
|
|
||||||
Assert.AreEqual(expected, result, 1e-6f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Bias_ReturnsCorrectResult_WhenBiasIsEqualToPointFive()
|
public void Bias_ReturnsCorrectResult_WhenBiasIsEqualToPointFive()
|
||||||
{
|
{
|
||||||
const float value = 0.5f;
|
double doubleResult = MathUtility.Bias(0.5, 0.5);
|
||||||
const float bias = 0.5f;
|
float floatResult = MathUtility.Bias(0.5f, 0.5f);
|
||||||
|
|
||||||
float result = MathUtility.Bias(value, bias);
|
Assert.AreEqual(0.5, doubleResult, 1e-4);
|
||||||
|
Assert.AreEqual(0.5f, floatResult, 1e-4f);
|
||||||
Assert.AreEqual(value, result, 1e-6f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void Bias_ReturnsCorrectResult_WhenBiasIsGreaterThanPointFive()
|
public void Bias_ReturnsCorrectResult_WhenBiasIsGreaterThanPointFive()
|
||||||
{
|
{
|
||||||
const float value = 0.5f;
|
double doubleResult = MathUtility.Bias(0.5, 0.8);
|
||||||
const float bias = 0.8f;
|
float floatResult = MathUtility.Bias(0.5f, 0.8f);
|
||||||
|
|
||||||
const float expected = 0.8f;
|
Assert.AreEqual(0.8, doubleResult, 1e-4);
|
||||||
float result = MathUtility.Bias(value, bias);
|
Assert.AreEqual(0.8f, floatResult, 1e-4f);
|
||||||
|
|
||||||
Assert.AreEqual(expected, result, 1e-6f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
@ -50,8 +45,8 @@ public class MathUtilityTests
|
|||||||
double doubleResult = MathUtility.GammaToLinear(0.25, 1.0);
|
double doubleResult = MathUtility.GammaToLinear(0.25, 1.0);
|
||||||
float floatResult = MathUtility.GammaToLinear(0.25f, 1.0f);
|
float floatResult = MathUtility.GammaToLinear(0.25f, 1.0f);
|
||||||
|
|
||||||
Assert.AreEqual(0.25, doubleResult);
|
Assert.AreEqual(0.25, doubleResult, 1e-6);
|
||||||
Assert.AreEqual(0.25f, floatResult);
|
Assert.AreEqual(0.25f, floatResult, 1e-6f);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
|
@ -25,6 +25,20 @@ public static class MathUtility
|
|||||||
return value / ((1.0f / bias - 2.0f) * (1.0f - value) + 1.0f);
|
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>
|
/// <summary>
|
||||||
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
|
/// Converts a gamma-encoded value to a linear value using a gamma value of <c>2.2</c>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user