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
32 changes: 30 additions & 2 deletions PreciseNumber.Test/PreciseNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public void TestStaticMinMagnitude()
PreciseNumber one = PreciseNumber.One;
PreciseNumber negativeOne = PreciseNumber.NegativeOne;
PreciseNumber result = PreciseNumber.MinMagnitude(one, negativeOne);
Assert.AreEqual(one, result);
Assert.AreEqual(negativeOne, result);
}

[TestMethod]
Expand All @@ -617,7 +617,35 @@ public void TestStaticMinMagnitudeNumber()
PreciseNumber one = PreciseNumber.One;
PreciseNumber negativeOne = PreciseNumber.NegativeOne;
PreciseNumber result = PreciseNumber.MinMagnitudeNumber(one, negativeOne);
Assert.AreEqual(one, result);
Assert.AreEqual(negativeOne, result);
}

[TestMethod]
[DataRow(-2, 2)]
[DataRow(2, -2)]
public void TestMagnitudeTiesFollowInt(int x, int y)
{
PreciseNumber px = x.ToPreciseNumber();
PreciseNumber py = y.ToPreciseNumber();

Assert.AreEqual(int.MaxMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MaxMagnitude(px, py));
Assert.AreEqual(int.MaxMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MaxMagnitudeNumber(px, py));
Assert.AreEqual(int.MinMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MinMagnitude(px, py));
Assert.AreEqual(int.MinMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MinMagnitudeNumber(px, py));
}

[TestMethod]
[DataRow(-3, 2)]
[DataRow(2, -3)]
[DataRow(3, -2)]
[DataRow(-2, 3)]
public void TestMagnitudeWithoutATiePicksByAbsoluteValue(int x, int y)
{
PreciseNumber px = x.ToPreciseNumber();
PreciseNumber py = y.ToPreciseNumber();

Assert.AreEqual(int.MaxMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MaxMagnitude(px, py));
Assert.AreEqual(int.MinMagnitude(x, y).ToPreciseNumber(), PreciseNumber.MinMagnitude(px, py));
}

[TestMethod]
Expand Down
30 changes: 26 additions & 4 deletions PreciseNumber/PreciseNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,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 700 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 700 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 700 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 700 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 700 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 700 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 @@ -1097,21 +1097,43 @@
value.Significand == 0;

/// <inheritdoc/>
public static PreciseNumber MaxMagnitude(PreciseNumber x, PreciseNumber y) =>
x.Abs() >= y.Abs() ? x : y;
/// <remarks>
/// When the magnitudes are equal, the positive value is returned, as for <see cref="int"/> and <see cref="double"/>.
/// </remarks>
public static PreciseNumber MaxMagnitude(PreciseNumber x, PreciseNumber y)
{
int comparison = x.Abs().CompareTo(y.Abs());
if (comparison != 0)
{
return comparison > 0 ? x : y;
}

return IsNegative(x) ? y : x;
}

/// <inheritdoc/>
public static PreciseNumber MaxMagnitudeNumber(PreciseNumber x, PreciseNumber y) => MaxMagnitude(x, y);

/// <inheritdoc/>
public static PreciseNumber MinMagnitude(PreciseNumber x, PreciseNumber y) =>
x.Abs() <= y.Abs() ? x : y;
/// <remarks>
/// When the magnitudes are equal, the negative value is returned, as for <see cref="int"/> and <see cref="double"/>.
/// </remarks>
public static PreciseNumber MinMagnitude(PreciseNumber x, PreciseNumber y)
{
int comparison = x.Abs().CompareTo(y.Abs());
if (comparison != 0)
{
return comparison < 0 ? x : y;
}

return IsNegative(x) ? x : y;
}

/// <inheritdoc/>
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 1136 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 1136 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 1136 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 1136 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