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
30 changes: 30 additions & 0 deletions SignificantNumber.Test/SignificantNumberValueTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,34 @@ public void CompareAtSignificance_ComparesAtTheLowerSignificantDigitCount(string
{
Assert.AreEqual(expected, int.Sign(SignificantNumber.CompareAtSignificance(Parse(left), Parse(right))));
}

[TestMethod]
[DataRow("1.5", "1.5", true)]
[DataRow("1.50", "1.5", true)]
[DataRow("0", "0", true)]
[DataRow("-2.25", "-2.25", true)]
[DataRow("1.5", "1.4", false)]
[DataRow("1.4", "1.5", false)]
public void EqualsPreciseNumber_AgreesInBothArgumentOrdersAndWithOperator(string significant, string precise, bool expected)
{
SignificantNumber s = Parse(significant);
PreciseNumber p = PreciseNumber.Parse(precise, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, s == p, $"{s} == {p}");
Assert.AreEqual(expected, s.Equals(p), $"{s}.Equals({p})");
Assert.AreEqual(expected, p.Equals(s), $"{p}.Equals({s})");
Assert.AreEqual(expected, s.CompareTo((object)p) == 0, $"{s}.CompareTo((object){p}) == 0");
}

[TestMethod]
public void EqualsObject_BoxedPreciseNumber_IsFalseEvenWhenEqual()
{
SignificantNumber s = Parse("1.5");
object boxed = PreciseNumber.Parse("1.5", CultureInfo.InvariantCulture);

// The record struct's Equals(object) only accepts another SignificantNumber, so a boxed PreciseNumber is
// never equal to it, although CompareTo(object) treats the two as the same value.
Assert.IsFalse(s.Equals(boxed));
Assert.AreEqual(0, s.CompareTo(boxed));
}
}
15 changes: 15 additions & 0 deletions SignificantNumber/SignificantNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
/// <exception cref="DivideByZeroException">Thrown when the current number is zero and <paramref name="power"/> is negative.</exception>
/// <exception cref="ArgumentException">Thrown when the current number is negative and <paramref name="power"/> is not an integer, as the result is not a real number.</exception>
/// <exception cref="OverflowException">Thrown when the result's power of ten is too large or too small to represent.</exception>
public SignificantNumber Pow(PreciseNumber power)

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

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

Check warning on line 592 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 17 to the 15 allowed.
{
if (Equal(power, Zero))
{
Expand Down Expand Up @@ -714,7 +714,7 @@
double magnitude = Math.Abs(exponent);
int lostDigits = !double.IsFinite(magnitude)
? DoubleSignificantDigits
: magnitude > 1 ? (int)Math.Ceiling(Math.Log10(magnitude)) : 0;

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 717 in SignificantNumber/SignificantNumber.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

return int.Max(1, int.Min(significantDigits, DoubleSignificantDigits - lostDigits));
}
Expand Down Expand Up @@ -891,6 +891,21 @@
? Value.CompareTo(significantNumber.Value)
: Value.CompareTo(obj);

/// <summary>
/// Determines whether the current instance holds the same value as a <see cref="PreciseNumber"/>.
/// </summary>
/// <param name="other">The precise number to compare with the current instance.</param>
/// <returns><see langword="true"/> if the values are equal; otherwise, <see langword="false"/>.</returns>
/// <remarks>
/// The values are compared exactly, so the result agrees with <c>==</c> and with
/// <see cref="PreciseNumber.Equals(PreciseNumber)"/> called the other way round. A <see cref="PreciseNumber"/> boxed as
/// an <see cref="object"/> still goes to <see cref="Equals(SignificantNumber)"/> through the record's
/// <c>Equals(object)</c>, which returns <see langword="false"/> because the object is not a
/// <see cref="SignificantNumber"/>, even though <see cref="CompareTo(object)"/> returns zero for it.
/// </remarks>
public bool Equals(PreciseNumber other) =>
Value.Equals(other);

/// <summary>
/// Returns the absolute value of the specified <see cref="SignificantNumber"/>.
/// </summary>
Expand Down
Loading