mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 22:55:42 +00:00
feat: add MathUtility.Pulse (resolves #60)
This commit is contained in:
parent
1939bbe4ba
commit
514e5b12b0
@ -163,6 +163,78 @@ public class MathUtilityTests
|
||||
Assert.AreEqual(1.0f, floatResult);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn1_GivenDoubleValueWithinBounds()
|
||||
{
|
||||
const double value = 0.5;
|
||||
const double lower = 0.0;
|
||||
const double upper = 1.0;
|
||||
|
||||
double result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(1.0, result, 1e-6);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn0_GivenDoubleValueLessThanLowerBound()
|
||||
{
|
||||
const double value = -1.0;
|
||||
const double lower = 0.0;
|
||||
const double upper = 1.0;
|
||||
|
||||
double result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(0.0, result, 1e-6);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn0_GivenDoubleValueGreaterThanUpperBound()
|
||||
{
|
||||
const double value = 2.0;
|
||||
const double lower = 0.0;
|
||||
const double upper = 1.0;
|
||||
|
||||
double result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(0.0, result, 1e-6);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn1_GivenSingleValueWithinBounds()
|
||||
{
|
||||
const float value = 0.5f;
|
||||
const float lower = 0.0f;
|
||||
const float upper = 1.0f;
|
||||
|
||||
float result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(1.0f, result, 1e-6f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn0_GivenSingleValueLessThanLowerBound()
|
||||
{
|
||||
const float value = -1.0f;
|
||||
const float lower = 0.0f;
|
||||
const float upper = 1.0f;
|
||||
|
||||
float result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(0.0f, result, 1e-6f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Pulse_ShouldReturn0_GivenSingleValueGreaterThanUpperBound()
|
||||
{
|
||||
const float value = 2.0f;
|
||||
const float lower = 0.0f;
|
||||
const float upper = 1.0f;
|
||||
|
||||
float result = MathUtility.Pulse(value, lower, upper);
|
||||
|
||||
Assert.AreEqual(0.0f, result, 1e-6f);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Sawtooth_ShouldReturn0Point5_Given0Point5AsDouble()
|
||||
{
|
||||
|
@ -287,6 +287,54 @@ public static class MathUtility
|
||||
return System.Math.Pow(value, 1.0 / gamma);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the pulse wave for a given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to calculate.</param>
|
||||
/// <param name="lowerBound">The inclusive lower bound of the pulse.</param>
|
||||
/// <param name="upperBound">The inclusive upper bound of the pulse.</param>
|
||||
/// <returns>
|
||||
/// 1 if <paramref name="value" /> lies between <paramref name="lowerBound" /> and <paramref name="upperBound" />;
|
||||
/// otherwise, 0.
|
||||
/// </returns>
|
||||
public static float Pulse(float value, float lowerBound, float upperBound)
|
||||
{
|
||||
bool result = lowerBound <= value && value <= upperBound;
|
||||
#if NET6_0_OR_GREATER
|
||||
return Unsafe.As<bool, int>(ref result);
|
||||
#else
|
||||
unsafe
|
||||
{
|
||||
var pResult = (int*)&result;
|
||||
return *pResult;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the pulse wave for a given value.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to calculate.</param>
|
||||
/// <param name="lowerBound">The inclusive lower bound of the pulse.</param>
|
||||
/// <param name="upperBound">The inclusive upper bound of the pulse.</param>
|
||||
/// <returns>
|
||||
/// 1 if <paramref name="value" /> lies between <paramref name="lowerBound" /> and <paramref name="upperBound" />;
|
||||
/// otherwise, 0.
|
||||
/// </returns>
|
||||
public static double Pulse(double value, double lowerBound, double upperBound)
|
||||
{
|
||||
bool result = lowerBound <= value && value <= upperBound;
|
||||
#if NET6_0_OR_GREATER
|
||||
return Unsafe.As<bool, int>(ref result);
|
||||
#else
|
||||
unsafe
|
||||
{
|
||||
var pResult = (int*)&result;
|
||||
return *pResult;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the incremental sawtooth wave of a given value.
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user