p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

[p5.js 2.0 Bug Report]: noise(null) is treated as noise(0) while noise(undefined) errors, and this behaviour is undocumented and inconsistent.

Open shuklaaryan367-byte opened this issue 10 months ago • 15 comments

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [x] Friendly errors
  • [ ] Other (specify if possible)

p5.js version

2.1.2

Web browser and version

Google Chrome Version 143.0.7499.170

Operating system

Windows 11

Steps to reproduce this

Steps:

  1. Run the sketch below
  2. Compare the outputs of noise(null) and noise(0)
  3. Observe that they are identical, while noise(undefined) throws an FES warning

Snippet:


function setup() {
  noCanvas();

  console.log("null:", noise(null));
  console.log("0:", noise(0));
  console.log("undefined:", noise(undefined));
}


Explanation:

noise(null) returns the same value as noise(0), while noise(undefined) produces a NaN and a Friendly Error message. Both null and undefined represent missing values in JavaScript, but they are handled differently here. This behaviour is not documented and can lead to silent bugs when null is passed unintentionally.

I'm happy to take on this task, if this is approved

shuklaaryan367-byte avatar Jan 13 '26 06:01 shuklaaryan367-byte

noise(null) gets coerced to noise(0), while noise(undefined) becomes NaN and triggers FES. This inconsistency isn’t documented and can cause silent bugs. It would be helpful to align these behaviors or document them clearly. I’m happy to contribute a fix once the expected behavior is confirmed.

sidhansu10 avatar Jan 21 '26 15:01 sidhansu10

Hi @shuklaaryan367-byte, it sounds like this is probably not unique to noise and would need an update to the zod type checking in FES for numeric parameters. Thanks for volunteering, I'll assign this to you since you expressed interest first.

davepagurek avatar Jan 21 '26 23:01 davepagurek

Hi @davepagurek I did a bit more digging around this and wanted to share some observations before moving forward with a PR.

While the original issue was noticed with noise(null), this behaviour doesn’t seem to be unique to noise() itself and appears to be more generally related to how numeric parameters are validated by FES / Zod.

I tested a few other numeric functions with null and found inconsistent behaviour:

function setup() {
  noCanvas();

  console.log('sin(null):', sin(null));           // 0 (no FES warning)
  console.log('noise(null):', noise(null));       // same as noise(0), no warning

  console.log('lerp(0, 10, null):', lerp(0, 10, null));
  // 🌸 Expected number at the third parameter in lerp()

  console.log('dist(null, 0, 0, 0):', dist(null, 0, 0, 0));
  // 🌸 Expected number at the first parameter in dist()
}

So it looks like:

  • sin(null) and noise(null) silently coerce null to 0 with no FES message
  • lerp() and dist() do trigger FES warnings for null

This made me think the root cause is likely in the parameter validation layer rather than any individual function. In param_validator.js, Number parameters currently map to z.number(), which doesn’t explicitly guard against null before JS coercion happens.

Before making changes, I wanted to check:

  • Should null be consistently treated as an invalid numeric argument across the API (i.e. always produce a friendly warning)?
  • Or is the current behaviour intentional for lower-level math functions like sin()?

Once I understand the intended policy here, I’m happy to either:

  • make a focused fix for noise() only, or
  • work on a more general FES/Zod-level solution if that’s preferred.

shuklaaryan367-byte avatar Jan 23 '26 08:01 shuklaaryan367-byte

Should null be consistently treated as an invalid numeric argument across the API (i.e. always produce a friendly warning)?

I think this is right, and null should log an FES parameter validation error.

An FES/zod-level fix would be great!

davepagurek avatar Jan 24 '26 19:01 davepagurek

Agreed. I’ll work on an FES-level fix so null is consistently rejected for numeric parameters across the API.

shuklaaryan367-byte avatar Jan 25 '26 08:01 shuklaaryan367-byte

Hi @davepagurek kindly review my PR and suggest changes if there are any.

shuklaaryan367-byte avatar Feb 20 '26 07:02 shuklaaryan367-byte

Should null be consistently treated as an invalid numeric argument across the API (i.e. always produce a friendly warning)?

I think this is right, and null should log an FES parameter validation error.

An FES/zod-level fix would be great!

@davepagurek Zod already log warning for passing null. From what I see, null gives FES warning and is treated as 0 but undefined does not give FES warning (while not providing any parameter gives FES warning).

The behavior should probably be both null and undefined gives parameter validation warning and the function return NaN.

limzykenneth avatar Apr 04 '26 22:04 limzykenneth

Alright so I've looked into this and this is actually two problems.

  1. noise(undefined) (or any functions that are only passed undefined) does not print an FES parameter validation warning. There is a check within the validation logic that check if all parameter passed is undefined, however in this check, the message was never actually printed, just returned when the return value is not used. The message should be printed in this case.

  2. noise(null) is treated as noise(0). The first step to processing the arguments to noise() is to apply Math.floor() to them, applying Math.floor(null) returns 0. If we want to specifically catch this, we can add a check within noise() itself and on encountering null return NaN.

limzykenneth avatar Apr 04 '26 22:04 limzykenneth

This issue is open to volunteers to address one or both of the points in the above comment. Thanks!

ksen0 avatar Jun 10 '26 12:06 ksen0

Hi @ksen0 , I would like to work on this issue

I can work on the FES fix for the undefined case.

For the null behavior, would you prefer that null be treated as an invalid parameter and trigger the same validation warning, or should it continue following JavaScript coercion semantics and behave like 0?

jeetsdev avatar Jun 21 '26 09:06 jeetsdev

@jeetsdev null should trigger a parameter validation warning, it should also return NaN when passed to noise()

limzykenneth avatar Jun 21 '26 16:06 limzykenneth

Understood @limzykenneth , thanks a lot for the clarification I would be happy to work on this issue. Since it isn't currently assigned, is it be okay if I pick it up and submit a PR for the fix?

jeetsdev avatar Jun 22 '26 07:06 jeetsdev

Reassigned to @jeetsdev, feel free to ask any questions while working on it.

limzykenneth avatar Jun 22 '26 08:06 limzykenneth

Thanks! I have worked on the issue and opened a PR for review: #8960. Please let me know if there are any changes or improvements you would like me to make.

jeetsdev avatar Jun 25 '26 17:06 jeetsdev

Hi @limzykenneth I have opened PR #8960 for this issue. Whenever you have some time, I wouldd appreciate a review. Thanks!

jeetsdev avatar Jul 24 '26 09:07 jeetsdev