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

feat: add MathUtility.Sawtooth (#60)

This commit is contained in:
Oliver Booth 2023-04-03 17:15:11 +01:00
parent c7370c39fd
commit 1939bbe4ba
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
3 changed files with 87 additions and 0 deletions

View File

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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.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.Sawtooth(float)` and `MathUtility.Sawtooth(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)`
- X10D: Added `MathUtility.Sigmoid(float)` and `MathUtility.Sigmoid(double)`. - X10D: Added `MathUtility.Sigmoid(float)` and `MathUtility.Sigmoid(double)`.

View File

@ -163,6 +163,72 @@ public class MathUtilityTests
Assert.AreEqual(1.0f, floatResult); Assert.AreEqual(1.0f, floatResult);
} }
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_Given0Point5AsDouble()
{
const double value = 0.5;
const double expected = 0.5;
double actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_Given0Point5AsSingle()
{
const float value = 0.5f;
const float expected = 0.5f;
float actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_Given1Point5AsDouble()
{
const double value = 1.5;
const double expected = 0.5;
double actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_Given1Point5AsSingle()
{
const float value = 1.5f;
const float expected = 0.5f;
float actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_GivenNegative1Point5AsDouble()
{
const double value = -1.5;
const double expected = 0.5;
double actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6);
}
[TestMethod]
public void Sawtooth_ShouldReturn0Point5_GivenNegative1Point5AsSingle()
{
const float value = -1.5f;
const float expected = 0.5f;
float actual = MathUtility.Sawtooth(value);
Assert.AreEqual(expected, actual, 1e-6f);
}
[TestMethod] [TestMethod]
public void ScaleRangeDouble_ShouldScaleRange_GivenItsValues() public void ScaleRangeDouble_ShouldScaleRange_GivenItsValues()
{ {

View File

@ -287,6 +287,26 @@ public static class MathUtility
return System.Math.Pow(value, 1.0 / gamma); return System.Math.Pow(value, 1.0 / gamma);
} }
/// <summary>
/// Returns the incremental sawtooth wave of a given value.
/// </summary>
/// <param name="value">The value to calculate.</param>
/// <returns>The sawtooth wave of the given value.</returns>
public static float Sawtooth(float value)
{
return (value - MathF.Floor(value));
}
/// <summary>
/// Returns the incremental sawtooth wave of a given value.
/// </summary>
/// <param name="value">The value to calculate.</param>
/// <returns>The sawtooth wave of the given value.</returns>
public static double Sawtooth(double value)
{
return (value - System.Math.Floor(value));
}
/// <summary> /// <summary>
/// Converts a value from being a percentage of one range, to being the same percentage in a new range. /// Converts a value from being a percentage of one range, to being the same percentage in a new range.
/// </summary> /// </summary>