Skip to content

Commit 8a5e501

Browse files
committed
update: aded generic clamp utility
1 parent 454c760 commit 8a5e501

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/FixedMathSharp/Core/FixedMath.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,28 @@ public static Fixed64 Clamp01(Fixed64 value)
7373
}
7474

7575
/// <summary>
76-
/// Clamps a fixed-point value between the given minimum and maximum values (defaults to Fixed64.Max).
76+
/// Clamps a fixed-point value between the given minimum and maximum values.
7777
/// </summary>
7878
[MethodImpl(MethodImplOptions.AggressiveInlining)]
79-
public static Fixed64 Clamp(Fixed64 f1, Fixed64 min, Fixed64? max = null)
79+
public static Fixed64 Clamp(Fixed64 f1, Fixed64 min, Fixed64 max)
8080
{
81-
Fixed64 m = max ?? Fixed64.MAX_VALUE;
82-
return f1 < min ? min : f1 > m ? m : f1;
81+
return f1 < min ? min : f1 > max ? max : f1;
82+
}
83+
84+
/// <summary>
85+
/// Clamps a value to the inclusive range [min, max].
86+
/// </summary>
87+
/// <typeparam name="T">The type of the value, must implement IComparable&lt;T&gt;.</typeparam>
88+
/// <param name="value">The value to clamp.</param>
89+
/// <param name="min">The minimum allowed value.</param>
90+
/// <param name="max">The maximum allowed value.</param>
91+
/// <returns>The clamped value.</returns>
92+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
93+
public static T Clamp<T>(T value, T min, T max) where T : IComparable<T>
94+
{
95+
if (value.CompareTo(max) > 0) return max;
96+
if (value.CompareTo(min) < 0) return min;
97+
return value;
8398
}
8499

85100
/// <summary>

tests/FixedMathSharp.Tests/FixedMath.Tests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public void Clamp_ValueWithinRange_ReturnsSameValue()
352352
{
353353
var value = new Fixed64(5);
354354
var min = new Fixed64(3);
355-
var result = FixedMath.Clamp(value, min);
355+
var result = FixedMath.Clamp(value, min, Fixed64.MAX_VALUE);
356356
Assert.Equal(new Fixed64(5), result);
357357
}
358358

0 commit comments

Comments
 (0)