Reject numbers the type cannot faithfully hold - #32
Open
VSN2015 wants to merge 1 commit into
Open
Conversation
VSN2015
commented
Sep 5, 2026
| # genuine zero whose exponent digits say nothing about the value — as are | ||
| # "0", "0.0" and "0.0000". | ||
| def finite_float(result, source: nil) | ||
| return [:error, "invalid_type"] unless result.finite? |
Owner
Author
There was a problem hiding this comment.
Detecting Float underflow by comparing result.zero? against nonzero_significand?(source) is a clever check to reject inputs like "1e-400" that underflow to 0.0 while preserving legitimate zeroes ("0.0", "0e10").
| # raises, so a client could send the literal string "NaN" for a price and | ||
| # have it stored. Nothing else in the gem disagreed with itself this | ||
| # loudly: :float rejected those strings and :decimal did not. | ||
| def finite_decimal(result) |
Owner
Author
There was a problem hiding this comment.
Explicitly checking result.finite? on BigDecimal closes the loophole where strings like "NaN" or "Infinity" were parsed as valid numbers by BigDecimal().
Coercion promises that "a value the type cannot faithfully represent is
a violation, not a guess". Four ways it wasn't:
:float "1e400" -> Infinity (overflow, accepted)
:float "1e-400" -> 0.0 (whole value lost, accepted)
:float Float::NAN -> NaN (passed through)
:decimal "NaN" -> NaN (a LITERAL STRING from a client)
The last is the one that matters most: BigDecimal("NaN") and
BigDecimal("Infinity") succeed where Float() raises, so a client could
send the string "NaN" for a price field and have it stored. :float
rejected exactly those strings while :decimal accepted them — the two
types disagreeing is what marks this as accidental rather than
designed.
Non-finite results are now invalid_type for both types.
A genuine zero is unaffected however it is spelled. Underflow is only
visible against the source text, since the result is an ordinary 0.0,
so a zero result is rejected only when the string named a nonzero
SIGNIFICAND — the exponent's digits say nothing about the value. My
first attempt scanned the whole string and rejected "0e10", which the
spec caught; the check now splits on e/E first.
:decimal keeps accepting the large exponents BigDecimal genuinely
represents ("1e400" -> 0.1e401), since it has no exponent limit and
so cannot overflow. Only NaN and Infinity are non-finite there.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
VSN2015
force-pushed
the
fix/reject-non-finite-numbers
branch
from
September 11, 2026 22:01
6e1e880 to
ebd99b6
Compare
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.
The promise
Four ways it wasn't kept
:float"1e400"Infinity— overflow, accepted:float"1e-400"0.0— the entire value lost, accepted:float/:decimalFloat::NAN,Float::INFINITY:decimal"NaN","Infinity"— literal stringsThe last is the one that matters.
BigDecimal("NaN")andBigDecimal("Infinity")succeed whereFloat()raises, so a client could send the string"NaN"for a:decimalprice field and have it stored — aNaNthat then breaks every comparison downstream and that no numeric column can hold.And
:floatrejected exactly those strings while:decimalaccepted them. The two types disagreeing is what marks this as accidental rather than designed.After
Two details worth reviewing
Underflow needs the source text. The result of
Float("1e-400")is an ordinary0.0— indistinguishable from a real zero after the fact. So a zero result is rejected only when the string named a nonzero significand. Only the significand: the exponent's digits say nothing about the value.My first attempt scanned the whole string and rejected
"0e10"— a genuine zero. The spec caught it; the check now splits one/Efirst. That's why"0","0.0","0.0000"and"0e10"are all still accepted, and there's a spec for each.:decimalcannot overflow.BigDecimalhas no exponent limit, so"1e400"is a value it genuinely represents and keeps working. OnlyNaNandInfinityare non-finite there — hence afinite?check rather than a range check.Verification