Skip to content

feat: Float64Range and Float64Set over finite doubles - #198

Merged
CaffeinatedCoder merged 5 commits into
mainfrom
feat/float64-range
Oct 18, 2026
Merged

CaffeinatedCoder merged 5 commits into
mainfrom
feat/float64-range

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Closes #196
Part of #153 (the double half; Float32Range/Float32Set follow in a stacked PR that closes #153)

Problem

There was no range or set over double. docs/why.md excluded it for an imprecise reason (IEEE operators vs CompareTo); the real break, measured in #153, is that .NET sorts NaN first and PostgreSQL last.

Change

Following docs/architecture.md#adding-a-range-or-set-type: measured first, then copied DecimalRange/DecimalSet (continuous numeric) with TimeRange's custom-type EF setup and YearMonthRange's refuse-at-construction mechanism.

  • Core: Float64Range (five variants, each with its own [JsonConverter]/[TypeConverter]; the unbounded variants are not positional so with { Start = NaN } cannot bypass the check), Float64Set, Float64Set<T> (AOT ConvertersRegistered, pinned G17, ValueSetFloatElementJsonConverter), closed JSON converters, RangeAgg/RangeIntersectAgg, variant Parse/TryParse.
  • Finite guard everywhere: Internals/FloatLiteral.RequireFinite in every factory and constructor; a new internal IRange<T>.NormalizeElement hook applied in Contains(T), Clamp(T) and RangeSet.Contains(T) (the empty and infinite shapes compare nothing, so the guard sits before the switch); IValueSet<T>.NormalizeElement; EF normalizeValue (FiniteFloat), so a NaN parameter throws at query time and a stored NaN/Infinity throws on load.
  • Text: FloatLiteral.Format reproduces float8out byte for byte (Ryu with acceptBounds = false, PostgreSQL's fixed/exponent layout). The parser is float8in's grammar for finite numbers, refusing NaN/inf, strtod hexadecimal, and out-of-range text that double.Parse silently turns into infinity or zero.
  • JSON: a closed double set is written and read through that text when System.Text.Json's built-in converter would have handled it — STJ's own writer prints 2^-25 as a different double.
  • EF Core: RangeTypeRegistry row (float8range, float8multirange, double precision, no subtype_diff), SetTypeRegistry closed row and Bridged<double> arity, SetProviderText arm for double, and NeedsExplicitCast skips a definition whose own element mapping already writes a typed literal (no ::double precision::double precision).
  • Docs: docs/efcore.md "Custom range types" table (timerange, float8range; float4range lands with PR 2) and a float8range section; docs/why.md rewritten to the measured reason with the table, one sentence on Intervals.NET, and the stricter-than-the-database entry; README "why double is absent" text replaced; ranges/value-sets/package README tables. 10 new claims (W01–W04, E45–E48, M12, S20), all held. CHANGELOG under 10.0.0.

Measurements (PostgreSQL 16.15 and 18.1, scratch containers; 17 in CI)

  • Float64Range/Float64Set and Float32Range/Float32Set over finite values only, mapped to custom float8range and float4range #153's table reproduces on 16 and 18.
  • .NET 10 defect: double.ToString(), "R" and Utf8JsonWriter write 2^-25 and 2^-958 as text that reads back one ulp lower — exactly those two (neighbourhood of every power of two exhaustively, 0 of 20M random doubles otherwise). PostgreSQL prints them correctly. Probably worth an upstream dotnet/runtime report; not filed.
  • .NET's shortest digits differ from float8out for 128 of 40,016 sampled doubles (values still equal), and the layout for most exponent forms. FloatLiteral.Format matches on 649,929 doubles on 16 and 18, 0 mismatches.
  • subtype_diff = float8mi fails GiST insert/build over extreme values with 22003 on 16 and 18; without it the index builds and answers.

Tests

  • New Float64DomainTests (unit, 57), Float64TranslationTests (no DB, 7), FloatDomainParityTests (integration, 5: 20,000-double text sweep vs float8out; range/array/multirange literals through range_in/array_in and EF's read leg, with the non-finite rows required to throw; NaN parameters; exact inlined constants; GiST with and without float8mi), plus Float64 rows in the shape matrix, algebra, literal-whitespace, inline-constant and catalogue tests, and every discovered sweep (fuzz, captured collections, host culture, JSON options, wrapper grammar/parity).
  • New oracle FloatDomainParityTests, two seeds, both CAUGHT by dotnet run tools/oracles.cs seeds float64: float64-admits-nan.patch (the [5,NaN) row loads as empty) and float64-literal-is-dotnet-shortest.patch (16,017 of 20,000 texts differ; the inlined 2^-25 constant matches nothing).
  • Reverted and confirmed failing: the operand hook in Contains/RangeSet.Contains, the JSON float write path, the EF finite guard, the SetProviderText double arm, and Float64Set<T>'s AOT registration (the Native AOT consumer fails with NotSupportedException).
  • Clone sweep: 92 divergences, all waived (28 new, each with its reason; 2 StringSet waivers went stale because Float64Set now shares NormalizeElement).
  • All suites green locally: conventions 338, unit 1089, EF 258, NodaTime 196, EF NodaTime 81, ASP.NET Core 88, integration 239 (PG 17), InfinityDisabled 4, consumer smoke test (with Homebrew SMOKE_AOT_LINKER_ARGS).

Members added in parallel sessions (scheduling, parity) will need Float64 coverage when both land.

🤖 Generated with Claude Code

Update: rebased onto the 10.0.0 members (merged origin/main)

Float64Range maps to the custom float8range (float8multirange), Float64Set
and Float64Set<T> to double precision[]. NaN and the infinities are refused
on every construction path and every scalar operand (IRange<T>.NormalizeElement,
IValueSet<T>.NormalizeElement, the EF definitions' normalizeValue), because
.NET sorts NaN first and PostgreSQL last.

The text is exactly float8out's (Internals/FloatLiteral.cs): Ryu with
acceptBounds = false and PostgreSQL's layout, measured equal on 649,929
doubles on PostgreSQL 16 and 18. .NET's shortest form differs in digits for
about one double in three hundred and, on .NET 10, does not read back for
2^-25 and 2^-958, so the JSON writer, SQL literals and the wrapper arity
(G17) never use it.

FloatDomainParityTests is a new oracle with two seeds, both caught.

Closes #196
Part of #153

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 2026
@CaffeinatedCoder CaffeinatedCoder added enhancement New feature or request area/ranges Range unions, variants and the shape engines area/value-sets Value set families and wrapper arities package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL area/json System.Text.Json and TypeConverter wire forms area/literals Parse, TryParse and formatting package/core Affects CodoMetis.ValueRanges labels Oct 18, 2026
CaffeinatedCoder and others added 4 commits October 18, 2026 20:20
The closed-union copy constructor (#213), CompareTo in PostgreSQL's range
order (#183), Shift/Expand (#185) and Split(step) (#197). Values(step)
stays with the discrete types.

Float arithmetic overflows to an infinity rather than throwing, so a moved
bound or split boundary past the finite values throws OverflowException as
for the other types, and a NaN or infinite step throws ArgumentException.
Each split boundary is Start + n * step, rounded once.

The arithmetic and split oracles gain a double grid step and an edge step
of whole ulps at MaxValue, where half a grid unit is absorbed by rounding.
Claims are renumbered onto the free ids after main's (E54-E57, M17, S22),
with R40 and R41 for the new sentences.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder
CaffeinatedCoder merged commit eb1e69b into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/float64-range branch October 18, 2026 18:34
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 area/value-sets Value set families and wrapper arities enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL

Projects

None yet

1 participant