Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3.7k
fix(math): add zero-range guard to map() to prevent NaN/Infinity#8283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Conversation
VIPAX-JIT commentedNov 24, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Hi maintainers..@davepagurek@perminder-17 Just checking in on this PR. It fixes a potential division-by-zero case in If there’s any additional changes or improvements needed, I’ll be happy to update the PR quickly. |
Uh oh!
There was an error while loading.Please reload this page.
Resolves#8282
Changes:
This PR introduces a small but important defensive check to the
map()function located insrc/math/calculation.js.Currently, when
start1 === stop1, the expression:(n - start1) / (stop1 - start1)
results in a division-by-zero case. This produces
InfinityorNaN, which then propagates through subsequent drawing and color computations. These invalid values are especially confusing for beginners, as this scenario frequently occurs in sketches involving sliders, dynamic ranges, or programmatically determined bounds.This PR adds an early return when the input range collapses, defaulting the mapped value to
start2. This matches the intuitive expectation that if no mapping is possible (because the source range has zero width), the target output should fall back to its lower bound. The normal behavior ofmap()for all valid ranges remains fully preserved.Why this change:
NaNandInfinityvalues from appearing in sketchesAdditional Notes:
This fix is intentionally minimal and avoids introducing any behavior that would alter existing valid mappings. It simply guards an undefined mathematical case while keeping the function’s logic identical for all standard uses. No documentation or unit test changes are strictly required, as the current behavior is undefined for
start1 === stop1and this PR only stabilizes that edge condition.PR Checklist
npm run lintpasses