1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

feat: add Saturate for floating point types (#60)

This commit is contained in:
Oliver Booth 2023-03-29 16:21:16 +01:00
parent 34d1f859a7
commit 436f56d912
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
7 changed files with 106 additions and 0 deletions

View File

@ -59,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- X10D: Added `ReadOnlySpan<T>.Split(T)`.
- X10D: Added `ReadOnlySpan<T>.Split(ReadOnlySpan<T>)`.
- X10D: Added `RoundUpToPowerOf2()` for built-in integer types.
- X10D: Added `Saturate()` for built-in floating-point types.
- X10D: Added `Size.ToPoint()`.
- X10D: Added `Size.ToPointF()`.
- X10D: Added `Size.ToVector2()`.

View File

@ -82,6 +82,24 @@ public class DecimalTests
Assert.AreEqual(10.0m, 7.5m.Round(5));
}
[TestMethod]
public void Saturate_ShouldClampValueTo1_GivenGreaterThan1()
{
Assert.AreEqual(1.0m, 1.5m.Saturate(), 1e-6m);
}
[TestMethod]
public void Saturate_ShouldClampValueTo0_GivenLessThan0()
{
Assert.AreEqual(0.0m, (-0.5m).Saturate(), 1e-6m);
}
[TestMethod]
public void Saturate_ShouldReturnValue_GivenValueBetween0And1()
{
Assert.AreEqual(0.5m, 0.5m.Saturate(), 1e-6m);
}
[TestMethod]
public void Sign_ShouldBeMinus1_GivenNegative()
{

View File

@ -117,6 +117,24 @@ public class DoubleTests
Assert.AreEqual(10.0, 7.5.Round(5), 1e-6);
}
[TestMethod]
public void Saturate_ShouldClampValueTo1_GivenGreaterThan1()
{
Assert.AreEqual(1.0, 1.5.Saturate(), 1e-6);
}
[TestMethod]
public void Saturate_ShouldClampValueTo0_GivenLessThan0()
{
Assert.AreEqual(0.0, (-0.5).Saturate(), 1e-6);
}
[TestMethod]
public void Saturate_ShouldReturnValue_GivenValueBetween0And1()
{
Assert.AreEqual(0.5, 0.5.Saturate(), 1e-6);
}
[TestMethod]
public void Sign_ShouldBeMinus1_GivenNegative()
{

View File

@ -117,6 +117,24 @@ public class SingleTests
Assert.AreEqual(10.0f, 7.5f.Round(5), 1e-6f);
}
[TestMethod]
public void Saturate_ShouldClampValueTo1_GivenGreaterThan1()
{
Assert.AreEqual(1.0f, 1.5f.Saturate(), 1e-6f);
}
[TestMethod]
public void Saturate_ShouldClampValueTo0_GivenLessThan0()
{
Assert.AreEqual(0.0f, (-0.5f).Saturate(), 1e-6f);
}
[TestMethod]
public void Saturate_ShouldReturnValue_GivenValueBetween0And1()
{
Assert.AreEqual(0.5f, 0.5f.Saturate(), 1e-6f);
}
[TestMethod]
public void Sign_ShouldBeMinus1_GivenNegative()
{

View File

@ -94,6 +94,23 @@ public static class DecimalExtensions
return System.Math.Round(value / nearest) * nearest;
}
/// <summary>
/// Saturates this decimal number.
/// </summary>
/// <param name="value">The value to saturate.</param>
/// <returns>The saturated value.</returns>
/// <remarks>This method clamps <paramref name="value" /> between 0 and 1.</remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Saturate(this decimal value)
{
return System.Math.Clamp(value, 0.0m, 1.0m);
}
/// <summary>
/// Returns an integer that indicates the sign of this decimal number.
/// </summary>

View File

@ -312,6 +312,23 @@ public static class DoubleExtensions
return System.Math.Round(value / nearest) * nearest;
}
/// <summary>
/// Saturates this double-precision floating-point number.
/// </summary>
/// <param name="value">The value to saturate.</param>
/// <returns>The saturated value.</returns>
/// <remarks>This method clamps <paramref name="value" /> between 0 and 1.</remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Saturate(this double value)
{
return System.Math.Clamp(value, 0.0, 1.0);
}
/// <summary>
/// Returns the sine of the specified angle.
/// </summary>

View File

@ -312,6 +312,23 @@ public static class SingleExtensions
return MathF.Round(value / nearest) * nearest;
}
/// <summary>
/// Saturates this single-precision floating-point number.
/// </summary>
/// <param name="value">The value to saturate.</param>
/// <returns>The saturated value.</returns>
/// <remarks>This method clamps <paramref name="value" /> between 0 and 1.</remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Saturate(this float value)
{
return System.Math.Clamp(value, 0.0f, 1.0f);
}
/// <summary>
/// Returns an integer that indicates the sign of this single-precision floating-point number.
/// </summary>