Return NaN from int() for unrecognised input types#8735
Closed
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Closed
Return NaN from int() for unrecognised input types#8735Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Conversation
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).
|
🎉 Thanks for opening this pull request! For guidance on contributing, check out our contributor guidelines and other resources for contributors! Thank You! |
Member
|
This does not link to an open bug report, so I will close it. Please review contribution guidelines before making future PRs. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Bug
p5.prototype.int()silently returnsundefinedwhen it's given avalue that doesn't match any of its recognised input types, so callers
that pass
null,undefined, or a plain object get a non-numericresult that poisons downstream arithmetic (e.g.
int(null) + 1isNaN, but from an unexpected source — the debugger showsint()returned
undefined, not a number).Root cause
Every branch returns a concrete value, but the
if/else ifchain hasno final
else, so unmatched types fall off the end and JavaScriptreturns
undefinedimplicitly.Why the fix is correct
float()helper inthe 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."NaNis the conventional JavaScript signal for "couldn't coerceto a number" (same as what
parseInt/Numberproduce for badinput), so downstream math produces
NaNin a predictable,self-describing way instead of a silent
undefined.return NaN;onlyruns on inputs that previously fell through to
undefined.Change
src/utilities/conversion.js: addreturn NaN;at the end ofp5.prototype.int. One-line addition.