Skip to content

Return NaN from int() for unrecognised input types#8735

Closed
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Chessing234:fix/int-default-return-nan
Closed

Return NaN from int() for unrecognised input types#8735
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Chessing234:fix/int-default-return-nan

Conversation

@Chessing234
Copy link
Copy Markdown

Bug

p5.prototype.int() silently returns undefined when it's given a
value that doesn't match any of its recognised input types, so callers
that pass null, undefined, or a plain object get a non-numeric
result that poisons downstream arithmetic (e.g. int(null) + 1 is
NaN, but from an unexpected source — the debugger shows int()
returned undefined, not a number).

Root cause

p5.prototype.int = function(n, radix = 10) {
  if (n === Infinity || n === 'Infinity') {
    return Infinity;
  } else if (n === -Infinity || n === '-Infinity') {
    return -Infinity;
  } else if (typeof n === 'string') {
    return parseInt(n, radix);
  } else if (typeof n === 'number') {
    return n | 0;
  } else if (typeof n === 'boolean') {
    return n ? 1 : 0;
  } else if (n instanceof Array) {
    return n.map(n => p5.prototype.int(n, radix));
  }
};

Every branch returns a concrete value, but the if/else if chain has
no final else, so unmatched types fall off the end and JavaScript
returns undefined implicitly.

Why the fix is correct

  • Matches the documented behaviour of the sibling float() helper in
    the same file, whose docstring already promises:
    "If a string can't be converted to a number, as in float('giraffe'),
    then the value NaN (not a number) will be returned."
  • NaN is the conventional JavaScript signal for "couldn't coerce
    to a number" (same as what parseInt/Number produce for bad
    input), so downstream math produces NaN in a predictable,
    self-describing way instead of a silent undefined.
  • Every existing code path is untouched; the new return NaN; only
    runs on inputs that previously fell through to undefined.

Change

src/utilities/conversion.js: add return NaN; at the end of
p5.prototype.int. One-line addition.

p5.prototype.int walks a chain of type checks (Infinity string/number,
string, number, boolean, Array) and returns the matched conversion.
Any other input — null, undefined, plain objects — falls off the chain
and the function implicitly returns undefined. Callers that do
arithmetic on the result then silently propagate NaN-like behaviour
(undefined * n === NaN) instead of getting a clear numeric value.

Match p5's sibling conversion float(), whose docs already promise NaN
when the input can't be interpreted as a number, and return NaN from
int() for the unmatched fall-through. All recognised paths keep their
exact return values (Infinity/-Infinity, parseInt result, bitwise
truncation, 0/1, or recursive array map).
@welcome
Copy link
Copy Markdown

welcome bot commented Apr 20, 2026

🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors!
🤔 Please ensure that your PR links to an issue, which has been approved for work by a maintainer; otherwise, there might already be someone working on it, or still ongoing discussion about implementation. You are welcome to join the discussion in an Issue if you're not sure!
🌸 Once your PR is merged, be sure to add yourself to the list of contributors on the readme page !

Thank You!

@ksen0
Copy link
Copy Markdown
Member

ksen0 commented Apr 20, 2026

This does not link to an open bug report, so I will close it. Please review contribution guidelines before making future PRs. Thank you!

@ksen0 ksen0 closed this Apr 20, 2026
Copy link
Copy Markdown

@alialobidm alialobidm left a comment

Choose a reason for hiding this comment

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

`` #8645

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.

3 participants