processing / processing/p5.js

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

Open
#8,282 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Math Bug
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Most appropriate sub-area of p5.js?
  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • p5.strands
  • WebGL
  • DevOps, Build process, Unit testing
  • Internationalization (i18n)
  • Friendly Errors
  • Other (specify if possible)
p5.js version

Latest main branch (development version from the repository)

Web browser and version

All browsers — issue is in core math logic, not rendering

Operating system

macOS (development environment)

Steps to reproduce this
Steps:
  1. Call map() with a collapsed input range (start1 === stop1)
  2. Example: map(5, 10, 10, 0, 100)
  3. The result becomes Infinity or NaN due to division by zero
Snippet:
// This produces Infinity or NaN:
let x = map(5, 10, 10, 0, 100);
console.log(x); // Infinity or NaN

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating p5.js's map() entry point and reproduce map(5, 10, 10, 0, 100). Check the surrounding map tests or conventions to determine the intended result for a collapsed input range. Done means this case has defined behavior and no longer produces NaN or Infinity.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.