fix(math): add zero-range guard to map() to prevent NaN/Infinity #8283
+4
−0
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.
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