mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 23:45:42 +00:00
feat: add MathUtility.Sawtooth (#60)
This commit is contained in:
parent
c7370c39fd
commit
1939bbe4ba
@ -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)`.
|
||||||
|
@ -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()
|
||||||
{
|
{
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user