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
20 changes: 20 additions & 0 deletions PreciseNumber.Test/PreciseNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,26 @@ public void TestReduceSignificance()
Assert.AreEqual(number, number.ReduceSignificance(5));
}

// A precision below one has no meaning, and used to return a wrong number rather than fail:
// 5 reduced to zero digits came back as 10, and to minus one digit as 0.

[TestMethod]
[DataRow(0)]
[DataRow(-1)]
[DataRow(int.MinValue)]
public void TestReduceSignificanceRejectsAPrecisionBelowOne(int significantDigits)
{
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => 5.ToPreciseNumber().ReduceSignificance(significantDigits));
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => PreciseNumber.Pi.ReduceSignificance(significantDigits));
}

[TestMethod]
public void TestReduceSignificanceThrowsWhenTheExponentLeavesTheIntRange()
{
PreciseNumber number = PreciseNumber.CreateFromComponents(int.MaxValue - 1, 12345);
Assert.ThrowsExactly<OverflowException>(() => number.ReduceSignificance(3));
}

[TestMethod]
public void TestReduceSignificanceRoundsHalfAwayFromZeroOnTheDroppedDigits()
{
Expand Down
11 changes: 8 additions & 3 deletions PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,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 711 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 711 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 711 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 711 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 711 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 711 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 @@ -857,8 +857,15 @@
/// <param name="significantDigits">The number of significant digits to reduce to.</param>
/// <returns>A new instance of <see cref="PreciseNumber"/> reduced to the specified number of significant digits.</returns>
/// <remarks>Rounds half away from zero, so 123.5 becomes 124 and 123.456 becomes 123 at three digits.</remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="significantDigits"/> is less than one.</exception>
/// <exception cref="OverflowException">Thrown when the result needs an exponent outside the range of an <see cref="int"/>.</exception>
public PreciseNumber ReduceSignificance(int significantDigits)
{
if (significantDigits < 1)
{
throw new ArgumentOutOfRangeException(nameof(significantDigits), significantDigits, "At least one significant digit is required.");
}

int significantDifference = significantDigits < SignificantDigits
? SignificantDigits - significantDigits
: 0;
Expand All @@ -868,9 +875,7 @@
return this;
}

int newExponent = Exponent == 0
? significantDifference
: Exponent + significantDifference;
int newExponent = checked(Exponent + significantDifference);
BigInteger newSignificand = DropDigitsRoundingHalfAwayFromZero(Significand, significantDifference);
return new(newExponent, newSignificand);
}
Expand Down Expand Up @@ -1154,7 +1159,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 1162 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 1162 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 1162 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 1162 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
Loading