Repository navigation
Fix multipleOf for small divisors and large values - #140
Conversation
|
The CI failure isn't from this change: the 8 failing tests are the v1 |
|
Correction to my comment above: I checked the CI log again. The |
Compare the quotient to the nearest integer with a tolerance that scales with the quotient, instead of comparing the remainder to a fixed epsilon. Use the exact remainder only when it's reliable: when the quotient overflows, or when both numbers are integers.
1a70f3b to
708e8ba
Compare
|
Thanks, this fixes small divisors. One tweak: the return Math.abs(quotient - Math.round(quotient)) <= 4 * Number.EPSILON * Math.abs(quotient);Please remove
|
The tolerance had a floor of 4 * EPSILON, so a value much smaller than the divisor passed, like 1e-20 with multipleOf: 1. The tolerance now scales with the quotient only. Remove multipleOf.spec.ts. These cases belong in JSON-Schema-Test-Suite.
|
Thanks! Done in 3d5e763:
The first three rows of your table are already in the suite from json-schema-org/JSON-Schema-Test-Suite#1254. I opened json-schema-org/JSON-Schema-Test-Suite#1262 for |
Fixes #136
Problem
multipleOfcompared the remainder to a fixed absolute epsilon (1.19209290e-7). Any divisor below about1e-7therefore accepted every value, for example1e-9withmultipleOf: 3e-9.Fix
This uses the scaled tolerance on the quotient suggested in #136, with one change to when the exact
%check is used:Why not fall back to
%whenever the quotient reaches 2^53? That breaks an existing test-suite case, "small multiple of large integer" (12391239123withmultipleOf: 1e-8, expected valid), in every dialect.1e-8isn't exact as a float, so12391239123 % 1e-8is7.4e-10, not0.So
%is now only used where it's exact:Infinity. This keeps the float-overflow cases working:1e308/0.123456789is invalid and1e308/0.5is valid.multipleOfare integers. Integer%is always exact, which covers1e20 / 3and2**60 / 3.Everything else goes through the scaled tolerance. Above 2^53 the quotient is already a whole number, so
12391239123 / 1e-8is valid.Results
1e-9 / 3e-91.5e-8 / 1e-81.5e-7 / 1e-71e20 / 32**60 / 3123456789.1 / 0.11234567890.12 / 0.0112391239123 / 1e-8(test suite)1e308 / 0.123456789(test suite)1e308 / 0.5(optional/)Testing
lib/keywords/multipleOf.spec.tswith small-divisor cases in both directions and every row from the table in multipleOf accepts values that aren't multiples when the divisor is small #136. 4 of the 9 fail without the fix, and all 9 pass with it.vitest run: 29 files, 15010 passed, 64 skipped, 0 failed.optional/tests for all drafts give the same results before and after the change.eslintandtsc --noEmitare clean.Known gap
A huge value with a fractional divisor that is exact as a float is accepted. For example,
2**60withmultipleOf: 1.5comes out valid. Without decimal arithmetic, the float can't tell this apart from the1e-8case above, and this seemed better than breaking the test-suite case. Happy to change it if you'd prefer a different trade-off.Follow-up
Tests for the suite: json-schema-org/JSON-Schema-Test-Suite#1254 adds a small-divisor case to
multipleOf.jsonand a large-integer case tooptional/bignum.json.@jdesrosiers