Skip to content

Fix multipleOf for small divisors and large values - #140

Merged
jdesrosiers merged 2 commits into
hyperjump-io:mainfrom
MohitSAGAR11:fix-multipleof-small-divisor
Nov 12, 2026
Merged

jdesrosiers merged 2 commits into
hyperjump-io:mainfrom
MohitSAGAR11:fix-multipleof-small-divisor

Conversation

@MohitSAGAR11

@MohitSAGAR11 MohitSAGAR11 commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #136

Problem

multipleOf compared the remainder to a fixed absolute epsilon (1.19209290e-7). Any divisor below about 1e-7 therefore accepted every value, for example 1e-9 with multipleOf: 3e-9.

Fix

This uses the scaled tolerance on the quotient suggested in #136, with one change to when the exact % check is used:

const quotient = value / multipleOf;
if (!Number.isFinite(quotient) || (Number.isInteger(value) && Number.isInteger(multipleOf))) {
  return value % multipleOf === 0;
}
return Math.abs(quotient - Math.round(quotient)) <= 4 * Number.EPSILON * Math.max(1, Math.abs(quotient));

Why not fall back to % whenever the quotient reaches 2^53? That breaks an existing test-suite case, "small multiple of large integer" (12391239123 with multipleOf: 1e-8, expected valid), in every dialect. 1e-8 isn't exact as a float, so 12391239123 % 1e-8 is 7.4e-10, not 0.

So % is now only used where it's exact:

  • The quotient overflows to Infinity. This keeps the float-overflow cases working: 1e308 / 0.123456789 is invalid and 1e308 / 0.5 is valid.
  • Both the value and multipleOf are integers. Integer % is always exact, which covers 1e20 / 3 and 2**60 / 3.

Everything else goes through the scaled tolerance. Above 2^53 the quotient is already a whole number, so 12391239123 / 1e-8 is valid.

Results

value / multipleOf correct before after
1e-9 / 3e-9 invalid valid ✗ invalid
1.5e-8 / 1e-8 invalid valid ✗ invalid
1.5e-7 / 1e-7 invalid valid ✗ invalid
1e20 / 3 invalid invalid invalid
2**60 / 3 invalid invalid invalid
123456789.1 / 0.1 valid valid valid
1234567890.12 / 0.01 valid invalid ✗ valid
12391239123 / 1e-8 (test suite) valid valid valid
1e308 / 0.123456789 (test suite) invalid invalid invalid
1e308 / 0.5 (optional/) valid valid valid

Testing

  • Added lib/keywords/multipleOf.spec.ts with 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.
  • Full vitest run: 29 files, 15010 passed, 64 skipped, 0 failed.
  • optional/ tests for all drafts give the same results before and after the change.
  • eslint and tsc --noEmit are clean.

Known gap

A huge value with a fractional divisor that is exact as a float is accepted. For example, 2**60 with multipleOf: 1.5 comes out valid. Without decimal arithmetic, the float can't tell this apart from the 1e-8 case 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.json and a large-integer case to optional/bignum.json.

@jdesrosiers

@MohitSAGAR11

Copy link
Copy Markdown
Contributor Author

The CI failure isn't from this change: the 8 failing tests are the v1 date-time-ext cases added to JSON-Schema-Test-Suite on Oct 20 (json-schema-org/JSON-Schema-Test-Suite#1223), so main fails the same way against the current suite. All multipleOf tests pass. Happy to send a separate PR wiring isDateTimeExt into v1's date-time if you'd like.

@MohitSAGAR11

Copy link
Copy Markdown
Contributor Author

Correction to my comment above: I checked the CI log again. The build (latest) job failed at npm install, before any tests ran. The newest npm refuses to fetch the test suite from GitHub (EALLOWGIT). The lts/* job was cancelled because of that. The date-time-ext failures I mentioned are what I saw locally against the latest test suite, not what CI hit.

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.
@MohitSAGAR11
MohitSAGAR11 force-pushed the fix-multipleof-small-divisor branch from 1a70f3b to 708e8ba Compare November 4, 2026 03:25
@jdesrosiers

Copy link
Copy Markdown
Collaborator

Thanks, this fixes small divisors.

One tweak: the Math.max(1, …) floor makes values much smaller than the divisor pass, like 1e-20 with multipleOf: 1. It isn't needed:

return Math.abs(quotient - Math.round(quotient)) <= 4 * Number.EPSILON * Math.abs(quotient);

Please remove multipleOf.spec.ts. This repo is tested through JSON-Schema-Test-Suite, so cases like these would be good additions there:

value multipleOf valid
2e-8 1e-8 yes
1.5e-8 1e-8 no
12391239123 1e-8 yes
1e-20 1 no
5e-324 0.5 no

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.
@MohitSAGAR11

Copy link
Copy Markdown
Contributor Author

Thanks! Done in 3d5e763:

  • Removed the Math.max(1, …) floor.
  • Removed multipleOf.spec.ts.

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 1e-20 / 1 and 5e-324 / 0.5. With those added locally, the old floor fails all 18 new tests, and this PR passes the full suite.

@jdesrosiers jdesrosiers left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jdesrosiers
jdesrosiers merged commit 4fce6b1 into hyperjump-io:main Nov 12, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

multipleOf accepts values that aren't multiples when the divisor is small

2 participants