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
36 changes: 36 additions & 0 deletions PreciseNumber.Test/PreciseNumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,42 @@ public void TestIsOddInteger()
Assert.IsFalse(PreciseNumber.IsOddInteger(two), "Two should not be an odd integer");
}

// Trailing zeros are stored in the exponent, so 10 is significand 1 at exponent 1. The parity
// checks used to read only the significand, which made every multiple of 10 look odd.

[TestMethod]
[DataRow(10)]
[DataRow(100)]
[DataRow(30)]
[DataRow(-20)]
[DataRow(1000000)]
public void TestIsEvenIntegerForMultiplesOfTen(int value)
{
PreciseNumber number = value.ToPreciseNumber();
Assert.IsTrue(PreciseNumber.IsEvenInteger(number), $"{value} should be an even integer");
Assert.IsFalse(PreciseNumber.IsOddInteger(number), $"{value} should not be an odd integer");
}

[TestMethod]
public void TestParityOfASumWithTrailingZero()
{
PreciseNumber sum = 7.ToPreciseNumber() + 3.ToPreciseNumber();
Assert.IsTrue(PreciseNumber.IsEvenInteger(sum), "7 + 3 should be an even integer");
Assert.IsFalse(PreciseNumber.IsOddInteger(sum), "7 + 3 should not be an odd integer");
}

[TestMethod]
[DataRow(1)]
[DataRow(-3)]
[DataRow(21)]
[DataRow(1001)]
public void TestIsOddIntegerForOddValues(int value)
{
PreciseNumber number = value.ToPreciseNumber();
Assert.IsTrue(PreciseNumber.IsOddInteger(number), $"{value} should be an odd integer");
Assert.IsFalse(PreciseNumber.IsEvenInteger(number), $"{value} should not be an even integer");
}

[TestMethod]
public void TestIsPositive()
{
Expand Down
14 changes: 12 additions & 2 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 @@ -1045,7 +1045,13 @@
public static bool IsComplexNumber(PreciseNumber value) => !IsRealNumber(value);

/// <inheritdoc/>
public static bool IsEvenInteger(PreciseNumber value) => IsInteger(value) && value.Significand.IsEven;
/// <remarks>
/// Trailing zeros are stored in <see cref="Exponent"/>, so 10 is significand 1 at exponent 1. The
/// significand's parity is the value's only at exponent 0; any positive exponent makes the value a
/// multiple of 10, and so even.
/// </remarks>
public static bool IsEvenInteger(PreciseNumber value) =>
IsInteger(value) && (value.Exponent > 0 || value.Significand.IsEven);

/// <inheritdoc/>
public static bool IsFinite(PreciseNumber value) => true;
Expand Down Expand Up @@ -1077,7 +1083,11 @@
public static bool IsNormal(PreciseNumber value) => true;

/// <inheritdoc/>
public static bool IsOddInteger(PreciseNumber value) => IsInteger(value) && !value.Significand.IsEven;
/// <remarks>
/// Only a value at exponent 0 can be odd, for the reason given on <see cref="IsEvenInteger"/>.
/// </remarks>
public static bool IsOddInteger(PreciseNumber value) =>
IsInteger(value) && value.Exponent == 0 && !value.Significand.IsEven;

/// <inheritdoc/>
public static bool IsPositive(PreciseNumber value) =>
Expand Down Expand Up @@ -1111,7 +1121,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 1124 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 1124 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 1124 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 1124 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