Validate multipleOf with exact decimal arithmetic - #151
Open
jnbdz wants to merge 1 commit into
Open
Conversation
The multipleOf check compared a binary floating point remainder against an absolute epsilon (FLT_EPSILON). This falsely accepts any value when multipleOf is smaller than the epsilon itself, e.g. 0.00000015 passed validation against multipleOf 0.0000001. Compute the remainder with BigDecimal on the decimal representation instead, which removes the epsilon workaround and its false accepts while keeping the rounding artifact cases (1.001 with multipleOf 0.001) valid. Fixes eclipse-vertx#106
jnbdz
force-pushed
the
fix-multipleof-precision
branch
from
August 9, 2026 17:16
90642b3 to
742d9a2
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.
Fixes #106
Motivation
#106 reported
multipleOf: 0.001rejecting values like1.001,1.01,1.02in the legacy 4.xMultipleOfValidatorFactory. Those cases already pass in the current validator thanks to its epsilon workaround — but the workaround has the opposite defect: the epsilon is absolute (1.1920929e-7, FLT_EPSILON), so anymultipleOfsmaller than the epsilon accepts every value. For example0.00000015validates againstmultipleOf: 0.0000001even though it is 1.5× the divisor — a false accept that matters for schemas describing high-precision quantities (8-decimal currency amounts, coordinates).Changes
Replace the epsilon comparison with an exact remainder computed via
BigDecimalon the decimal representation (BigDecimal.valueOf, which the existingtoBigDecimalhelper already uses). This makes the check exact for the decimal values users write in schemas and instances:1.001/0.001etc.) remain valid — binary rounding artifacts no longer need an epsilon;Numbers.remainderhad no other callers and is replaced byNumbers.isMultipleOf. The official test-suitemultipleOfcases (small numbers,1e308overflow withmultipleOf: 0.5, float-division-inf) all pass unchanged; full suite green.Added regression tests covering the #106 values and the sub-epsilon false-accept cases.