Skip to content

Fix edge-case bugs in Decimal arithmetic, comparisons, and null handling - #67

Open
alvaro9rqc wants to merge 2 commits into
FHIR:masterfrom
alvaro9rqc:fix/decimal-edge-cases-formal-audit
Open

alvaro9rqc wants to merge 2 commits into
FHIR:masterfrom
alvaro9rqc:fix/decimal-edge-cases-formal-audit

Conversation

@alvaro9rqc

Copy link
Copy Markdown

Summary

While auditing org.fhir.ucum.Decimal (using JML/OpenJML static verification and JUnit tests), we identified and fixed five edge-case defects:

  1. 0 / 0 silently returning 0 in doDivide(): isZero() was checked before other.isZero(), so new Decimal("0").divide(new Decimal("0")) returned 0 instead of throwing UcumException. Reordered the check so dividing by zero always throws UcumException.
  2. Error("Unhandled") in doAdd() / doSubtract() for values < 0.1 or scaled scientific notation: When max < 0 (e.g., 0.01 + 0.01, 0.05 - 0.01) or max > s3.length() (e.g., 1e3 + 1e3), both methods threw new Error("Unhandled"). Added proper zero-padding for both branches.
  3. Null and negative-precision guards: Decimal(String) and Decimal(String, int) now throw UcumException on null input (and negative precision) instead of NullPointerException; equals(Decimal, Decimal) returns false on null arguments and limitPrecisionTo(Decimal) ignores null.
  4. Signed-zero consistency in comparesTo(): new Decimal("-0").comparesTo(new Decimal("0")) previously returned -1, contradicting equals(Object) (true) and toString() ("0"). Normalized zero operands before sign comparison.
  5. Linear borrow tracking in stringSubtraction(): Replaced the string-mutating replaceChar borrow approach (which temporarily wrote '/' on chained borrows across '0' and ran in O(n²)) with a standard O(n) borrow variable, removing the now-unused replaceChar helper.

Testing

  • Added unit tests in UcumDecimalTests covering all five fixes (testDivideByZero, testAddSubtractSmallAndScientificValues, testNullAndPrecisionGuards, testNegativeZeroComparisonConsistency).
  • Verified locally with mvn package -P CHECKSTYLE (617/617 tests passing, 0 Checkstyle violations).

Note: Analysis, patch drafting, and test verification were assisted by AI tools and reviewed manually.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

@grahamegrieve

Copy link
Copy Markdown
Contributor
  1. comparesTo returns the raw string-comparison difference, not -1/0/1. That was already true for most inputs: -0 against 879810669895 gives 8, for instance. The new test asserts exactly -1 and 1, which only works because of the particular values chosen. Either have comparesTo return Integer.signum(...), which is the better fix since callers only check the sign, or change the test to check < 0 and > 0.

  2. Add a line to RELEASE_NOTES.md, which is empty again since 1.0.11, mentioning at least the 0/0 change and the add/subtract fix.

@alvaro9rqc

Copy link
Copy Markdown
Author

Updated in ab90c8e:

  • Normalized comparesTo() return values with Integer.signum(...) and added test cases with non-adjacent/multi-digit values (including -0 vs 879810669895).
  • Added an entry to RELEASE_NOTES.md covering the 0 / 0, add()/subtract(), comparesTo(), and null/precision changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants