Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions PreciseNumber.Test/PreciseNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ public void TestClampLower()
Assert.AreEqual(min, result);
}

[TestMethod]
public void TestClampThrowsWhenMinIsGreaterThanMax()
{
PreciseNumber min = 3.ToPreciseNumber();
PreciseNumber max = 1.ToPreciseNumber();

// Every value, inside or outside the inverted range, is a caller bug, as it is for Math.Clamp.
Assert.ThrowsExactly<ArgumentException>(() => PreciseNumber.Clamp(2.ToPreciseNumber(), min, max));
Assert.ThrowsExactly<ArgumentException>(() => PreciseNumber.Clamp(5.ToPreciseNumber(), min, max));
Assert.ThrowsExactly<ArgumentException>(() => 2.ToPreciseNumber().Clamp(3, 1));
}

[TestMethod]
public void TestClampAcceptsEqualBounds()
{
PreciseNumber bound = 3.ToPreciseNumber();
Assert.AreEqual(bound, PreciseNumber.Clamp(7.ToPreciseNumber(), bound, bound));
}

[TestMethod]
public void TestClampUpper()
{
Expand Down
7 changes: 7 additions & 0 deletions PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,11 +572,17 @@
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value.</returns>
/// <exception cref="ArgumentException"><paramref name="min"/> is greater than <paramref name="max"/>.</exception>
public PreciseNumber Clamp<TNumber>(TNumber min, TNumber max)
where TNumber : INumber<TNumber>
{
PreciseNumber sigMin = min.ToPreciseNumber();
PreciseNumber sigMax = max.ToPreciseNumber();
if (sigMin > sigMax)
{
throw new ArgumentException($"'{sigMin}' cannot be greater than {sigMax}.", nameof(min));
}

PreciseNumber clampedToMax = this > sigMax ? sigMax : this;
return this < sigMin ? sigMin : clampedToMax;
}
Expand Down Expand Up @@ -697,7 +703,7 @@
/// A fixed precision such as <c>E15</c> rounds values that need 17 digits, which turns
/// <see cref="double.MaxValue"/> into a number that converts back to infinity.
/// </remarks>
internal static string GetStringFormatForFloatType<TFloat>()

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.

Check warning on line 706 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Remove this method and declare a constant for this value.
where TFloat : INumber<TFloat>
=> "R";

Expand Down Expand Up @@ -1111,7 +1117,7 @@
public static PreciseNumber MinMagnitudeNumber(PreciseNumber x, PreciseNumber y) => MinMagnitude(x, y);

/// <inheritdoc/>
public static PreciseNumber Parse(ReadOnlySpan<char> s, NumberStyles style, IFormatProvider? provider)

Check warning on line 1120 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1120 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1120 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.

Check warning on line 1120 in PreciseNumber/PreciseNumber.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 23 to the 15 allowed.
{
if (s.IsEmpty)
{
Expand Down Expand Up @@ -1710,6 +1716,7 @@
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped number.</returns>
/// <exception cref="ArgumentException"><paramref name="min"/> is greater than <paramref name="max"/>.</exception>
public static PreciseNumber Clamp(PreciseNumber value, PreciseNumber min, PreciseNumber max) =>
value.Clamp(min, max);

Expand Down
Loading