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
35 changes: 35 additions & 0 deletions SignificantNumber.Test/SignificantNumberAdditionPlaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace SignificantNumber.Test;

using System.Globalization;
using ktsu.SignificantNumber;

/// <summary>
/// Covers the rounding of <see cref="SignificantNumber.Add"/> and <see cref="SignificantNumber.Subtract"/> to the
/// coarsest least significant place of their operands, including places left of the decimal point.
/// </summary>
[TestClass]
public class SignificantNumberAdditionPlaceTests
{
private static SignificantNumber Parse(string text) =>
SignificantNumber.Parse(text, NumberStyles.Float, CultureInfo.InvariantCulture);

[TestMethod]
[DataRow("1.2E3", "34", "1.2E3")]
[DataRow("1200", "34", "1.2E3")]
[DataRow("34", "1.2E3", "1.2E3")]
[DataRow("1.2E3", "56", "1.3E3")]
[DataRow("1234", "5.6", "1240")]
[DataRow("1.2E3", "1", "1.2E3")]
public void Add_RoundsToTheCoarsestLeastSignificantPlace(string left, string right, string expected) =>
Assert.AreEqual(Parse(expected), Parse(left) + Parse(right));

[TestMethod]
[DataRow("1.2E3", "34", "1.2E3")]
[DataRow("1200", "34", "1.2E3")]
[DataRow("1.2E3", "56", "1.1E3")]
[DataRow("1234", "5.6", "1228")]
public void Subtract_RoundsToTheCoarsestLeastSignificantPlace(string left, string right, string expected) =>
Assert.AreEqual(Parse(expected), Parse(left) - Parse(right));
}
12 changes: 6 additions & 6 deletions SignificantNumber/SignificantNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,14 @@ private static bool HasInfinitePrecision(PreciseNumber value) =>
value.Exponent == 0 && BigInteger.Abs(value.Significand) <= BigInteger.One;

/// <summary>
/// Counts the digits after the decimal point in a number.
/// Counts the decimal places of a number's least significant digit.
/// </summary>
/// <param name="value">The number to count the decimal digits of.</param>
/// <returns>The number of digits after the decimal point.</returns>
private static int CountDecimalDigits(PreciseNumber value) =>
value.Exponent > 0
? 0
: int.Abs(value.Exponent);
/// <returns>
/// The number of digits after the decimal point, or, when the least significant digit is left of the decimal point,
/// the negated count of places it sits to the left of the units: -1 for tens, -2 for hundreds, and so on.
/// </returns>
private static int CountDecimalDigits(PreciseNumber value) => -value.Exponent;

/// <summary>
/// Gets the lower of the decimal digit counts of two numbers, ignoring an operand with unlimited precision.
Expand Down
Loading