Skip to content

fix: read decimal text exactly or refuse it - #166

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/decimal-precision
Oct 18, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/decimal-precision

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #115

Stacked on #165 (fix/wrapper-round-trip) → #164 → #163 → #162; the diff against main includes them until they merge. Last of the six element-grammar PRs for 10.0.0; the value-semantics review ran over the whole stack before it was opened (its one finding is the second commit of #164).

Problem

decimal.Parse rounds a 29th significant digit, a scale beyond 28 and 1e-30 to the nearest representable value without a word, while PostgreSQL's numeric keeps every digit. So for the same text the value in memory was a different number from the server's:

DecimalRange.Parse("[1.00000000000000000000000000001,2)", null).Contains(1m);   // true; the server's range: false
DecimalSet.Parse("{1.00000000000000000000000000001,1}", null).Count;          // 1; cardinality on the server: 2
JsonSerializer.Deserialize<DecimalSet>("[1.00000000000000000000000000001]");    // {1.0000000000000000000000000000}

The JSON document and the SQL constant written back carried the rounded number. Only excess precision was folded in: a magnitude that does not fit overflowed, loudly, as it still does.

Change

Tests

  • DecimalPrecisionTests (new): TextDecimalWouldRound_IsRefused_AtEveryEntryPoint (7 texts × DecimalRange Parse/TryParse string and span, RangeSet<DecimalRange>, JSON, TypeConverter, DecimalSet and DecimalSet<TestMoney> Parse/TryParse, JSON number and quoted number), TextDecimalHoldsExactly_Reads (12 spellings, scale kept), JsonNumber_IsReadFromItsOwnText, MagnitudeBeyondDecimal_StillOverflows.
  • LiteralCorpusProbeTests (postgres:17): the beyond 28 digits family of DecimalRange, RangeSet<DecimalRange> and DecimalSet now counts as a domain refusal (the library refuses the server's canonical text too), as the probe's rules define it.
  • Fix reverted (DecimalLiteral.cs deleted, DecimalSet.cs, DecimalRange.cs and ValueSetJsonConverter.cs from the parent commit, tests kept): 7 of the 21 DecimalPrecisionTests rows fail, the acceptance rows still pass. Restored afterwards.
  • Full runs on the rebased stack: core 1006, conventions 324, EF translation 246, NodaTime 196, EF NodaTime 81, AspNetCore 84; integration subset (corpus, JSON columns, sets, grammar parity, wrapper arities) 83 — green. Clone sweep: 65 waived, 0 unwaived, 0 stale. The whole integration project on the final branch: result in a comment below.

🤖 Generated with Claude Code

decimal.Parse rounds a 29th significant digit, a scale beyond 28 and
1e-30 to the nearest representable value without a word, while
PostgreSQL's numeric keeps every digit. So
DecimalRange.Parse("[1.00000000000000000000000000001,2)") was a range
containing 1 in memory and not on the server,
DecimalSet.Parse("{1.00000000000000000000000000001,1}") had one element
here and two there, a JSON array read the same way, and the JSON
document and SQL constant written back carried the rounded number. Only
excess precision was folded in: a magnitude that does not fit
overflowed, loudly, as it still does.

DecimalLiteral reads the text's digits and exponent exactly and compares
them with the parsed decimal's mantissa and scale, so every spelling of
a number decimal holds passes (1e3, +1.5, 007.50, a trailing zero beyond
the 28th place) and only a different number is refused, with
FormatException. DecimalRange.ParseValue and DecimalSet.ParseValue share
it: the range side had the same decimal.Parse call, so both are fixed
here rather than leaving DecimalRange to the range-literal group, and
RangeFormat, IsoTemporal and NodaPatterns are untouched. DecimalSet<T>,
RangeSet<DecimalRange>, the TypeConverter and the EF Core JSON document
go through them. The primitive DecimalSet reads its JSON elements
through ParseValue whenever System.Text.Json's own decimal converter
would have, from the token's text — a quoted one only under
AllowReadingFromString, as that converter would — the way the temporal
sets already do.

Closes #115

Found-By: OT
Defect-Class: LIT

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred breaking Changes an answer or a signature — needs a major found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) area/ranges Range unions, variants and the shape engines area/json System.Text.Json and TypeConverter wire forms area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges severity/low Cosmetic, message or documentation-level difference stacked Base is another open PR; merge in order, CI does not run until it targets main labels Oct 18, 2026
@CaffeinatedCoder

Copy link
Copy Markdown
Owner Author

Whole integration project (test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests, postgres:17 via Testcontainers) on the final branch of the stack: Bestanden! : Fehler: 0, erfolgreich: 209, übersprungen: 0, gesamt: 209, Dauer: 14 s - CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests.dll (net10.0)

Base automatically changed from fix/wrapper-round-trip to main October 18, 2026 13:24
@CaffeinatedCoder CaffeinatedCoder removed the stacked Base is another open PR; merge in order, CI does not run until it targets main label Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/json System.Text.Json and TypeConverter wire forms area/literals Parse, TryParse and formatting area/ranges Range unions, variants and the shape engines breaking Changes an answer or a signature — needs a major bug Something isn't working found-by/oracle Found by the generated-oracle machinery (claims × API surfaces) package/core Affects CodoMetis.ValueRanges severity/low Cosmetic, message or documentation-level difference silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Decimal text beyond 28 significant digits is rounded silently when a literal or JSON array is read

1 participant