Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
VIPAX-JIT wants to merge1 commit intoprocessing:main
base:main
Choose a base branch
Loading
fromVIPAX-JIT:fix-map-zero-range

Conversation

@VIPAX-JIT
Copy link

@VIPAX-JITVIPAX-JIT commentedNov 20, 2025
edited
Loading

Resolves#8282

Changes:

This PR introduces a small but important defensive check to themap() function located insrc/math/calculation.js.

Currently, whenstart1 === stop1, the expression:

(n - start1) / (stop1 - start1)

results in a division-by-zero case. This producesInfinity orNaN, 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 tostart2. 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:

  • Prevents hard-to-debugNaN andInfinity values from appearing in sketches
  • Eliminates a common edge case encountered by beginners
  • Improves overall robustness and predictability of the math utilities
  • Fully backward-compatible and does not alter any expected API behavior
  • Aligns with p5.js’s mission of supporting learning, accessibility, and beginner-friendly design
  • Low-risk change that improves stability without touching rendering, DOM, or WebGL subsystems

Additional 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 forstart1 === stop1 and this PR only stabilizes that edge condition.

PR Checklist

  • npm run lint passes
  • Inline reference is included / updated
  • Unit tests are included / updated

@VIPAX-JIT
Copy link
Author

VIPAX-JIT commentedNov 24, 2025
edited
Loading

Hi maintainers..@davepagurek@perminder-17

Just checking in on this PR. It fixes a potential division-by-zero case inmap() whenstart1 === stop1, which currently producesInfinity/NaN and can affect drawing logic.

If there’s any additional changes or improvements needed, I’ll be happy to update the PR quickly.
Thanks for your time and effort in maintaining p5.js!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Add zero-range guard to map() to prevent NaN/Infinity when start1 equals stop1

1 participant

@VIPAX-JIT

[8]ページ先頭

©2009-2025 Movatter.jp