1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

feat: Add MathUtility.ExponentialDecay (#60)

This commit is contained in:
Oliver Booth 2023-04-03 16:21:43 +01:00
parent 9cf003481c
commit 654d5b5b08
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 51 additions and 0 deletions

View File

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added .NET 7 target. - Added .NET 7 target.
- X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70) - X10D: Added `IntrinsicExtensions` and `IntrinsicUtility` which offer methods for vectors in `System.Runtime.Instrinsics`. (#70)
- X10D: Added `MathUtility.Bias(float, float)` and `MathUtility.Bias(double, double)`. - X10D: Added `MathUtility.Bias(float, float)` and `MathUtility.Bias(double, double)`.
- X10D: Added `MathUtility.ExponentialDecay(float, float, float)` and `MathUtility.ExponentialDecay(double, double, double)`.
- X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)` - X10D: Added `MathUtility.InverseLerp(float, float, float)` and `MathUtility.InverseLerp(double, double, double)`
- X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)` - X10D: Added `MathUtility.ScaleRange(float, float, float, float, float)`
and `MathUtility.ScaleRange(double, double, double, double, double)` and `MathUtility.ScaleRange(double, double, double, double, double)`

View File

@ -39,6 +39,32 @@ public class MathUtilityTests
Assert.AreEqual(0.8f, floatResult, 1e-4f); Assert.AreEqual(0.8f, floatResult, 1e-4f);
} }
[TestMethod]
public void ExponentialDecay_ShouldReturnCorrectValue_GivenDouble()
{
const double value = 100.0;
const double alpha = 0.5;
const double decay = 0.1;
const double expected = 95.122942;
double actual = MathUtility.ExponentialDecay(value, alpha, decay);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void ExponentialDecay_ShouldReturnCorrectValue_GivenSingle()
{
const float value = 100.0f;
const float alpha = 0.5f;
const float decay = 0.1f;
const float expected = 95.12295f;
float actual = MathUtility.ExponentialDecay(value, alpha, decay);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod] [TestMethod]
public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1() public void GammaToLinear_ShouldReturnQuarter_GivenQuarterAndGamma1()
{ {

View File

@ -39,6 +39,30 @@ public static class MathUtility
return value / ((1.0 / bias - 2.0) * (1.0 - value) + 1.0); return value / ((1.0 / bias - 2.0) * (1.0 - value) + 1.0);
} }
/// <summary>
/// Calculates exponential decay for a value.
/// </summary>
/// <param name="value">The value to decay.</param>
/// <param name="alpha">A factor by which to scale the decay.</param>
/// <param name="decay">The decay amount.</param>
/// <returns>The exponentially decayed value.</returns>
public static float ExponentialDecay(float value, float alpha, float decay)
{
return value * MathF.Exp(-decay * alpha);
}
/// <summary>
/// Calculates exponential decay for a value.
/// </summary>
/// <param name="value">The value to decay.</param>
/// <param name="alpha">A factor by which to scale the decay.</param>
/// <param name="decay">The decay amount.</param>
/// <returns>The exponentially decayed value.</returns>
public static double ExponentialDecay(double value, double alpha, double decay)
{
return value * System.Math.Exp(-decay * alpha);
}
/// <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>