Hash the sanitized form, so Equals and GetHashCode agree - #103
Merged
Merged
Conversation
Equals compares numerically, so an un-sanitized 2.50 - significand 250 at exponent -2 - equals a sanitized 2.5. GetHashCode hashed the raw fields, so the two produced different hashes, violating the Equals/GetHashCode contract: they land in different buckets of a Dictionary or HashSet and lookups miss. Such instances are reachable through CreateFromComponents(sanitize: false) and MakeCommonized/MakeCommonizedWithExponent, all internal and all kept for ktsu.SignificantNumber, and all of which return un-normalized values directly rather than only as transient internal state. Strip the trailing zeros before combining. Zero hashes as the sanitized zero whatever exponent it was stored at, and the exponent adjustment is unchecked because a hash must not throw. Fixes #101 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FLcTd3PKEVEnz3UcufaB4o
|
This was referenced Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #101.
The bug
EqualscomparesPreciseNumbervalues numerically, viaCompare— it aligns exponents and compares significands, so2.5and an un-sanitized2.50(Significand=250,Exponent=-2) are equal.GetHashCodehashed the raw(Exponent, Significand)fields, so those same two values produced different hashes. That violates .NET's Equals/GetHashCode contract.Un-sanitized instances are reachable through
CreateFromComponents(exponent, significand, sanitize: false)and throughMakeCommonized/MakeCommonizedWithExponent— all internal, all documented as kept for thektsu.SignificantNumberderived library, and all of which hand back un-normalized values directly rather than only as transient internal state.The practical failure, reproduced on
main:Two
Equals-equal values, two different buckets, a lookup that silently misses.Public
Parseand the arithmetic paths always sanitize first, so ordinary usage is unaffected — the exposure is to code consuming those internal escape hatches.The fix
Strip the trailing zeros before combining, so the hash is taken over the sanitized form however the instance was built:
0).CountTrailingZeros— the same helper the sanitizing constructor uses — gives the shift, and the significand is divided down with the exponent moved up to match.trailingZeros == 0and combines the stored fields exactly as before, so no existing hash value changes.unchecked, becauseGetHashCodemust not throw. The constructor does the same additionchecked, so an exponent that could overflow here cannot survive ordinary arithmetic anyway, and a wrapped value still sendsEquals-equal pairs to the same bucket.Tests
Three tests added to
PreciseNumberTests, all verified to fail against the previous implementation (3 failed before the change, 383 passed after):TestGetHashCodeAgreesWithEqualsForUnsanitizedValues— seven(exponent, significand)pairs built both ways, assertingEqualsand hash equality against the sanitized form:2.50,2.500,100spelled two ways, a negative-25.0, and zero at both a zero and a non-zero exponent.TestGetHashCodeAgreesWithEqualsAfterCommonizing— covers the other route to an un-normalized instance,MakeCommonized, rather than only the explicitsanitize: falseflag.TestUnsanitizedValuesShareADictionarySlotWithTheirSanitizedForm— the contract violation as a caller actually meets it: aDictionarylookup that used to miss.Full suite on the branch: 383 passed, 0 failed (380 pre-existing, unchanged — including the existing
TestGetHashCode).Scope
One method and three tests. No public API change. Independent of #102 (also against
main); the two touch different methods and do not conflict.🤖 Generated with Claude Code
https://claude.ai/code/session_01FLcTd3PKEVEnz3UcufaB4o
Generated by Claude Code