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

[p5.js 2.0 Bug Report]: if use min() and max() with Infinity or -Infinity, it throws friendly error

Open inaridarkfox4231 opened this issue 2 months ago • 7 comments

Most appropriate sub-area of p5.js?

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

p5.js version

2.0.4

Web browser and version

Chrome

Operating system

Windows

Steps to reproduce this

Steps:

  1. console.log(min(Infinity,3));
  2. console.log(max(3,-Infinity));
  3. until 2.0.3, it throws no error. 2.0.4 and 2.0.5 throws friendly error. (There is no problem in getting the value.)

Snippet:

function setup() {
	createCanvas(windowWidth, windowHeight);
	background(100);
	console.log(min(Infinity,3));
	console.log(max(3,-Infinity));
}

2.0.3

no error.

Image

2.0.4, 2.0.5

friendly error occurs.

Image Image
p5.disableFriendlyErrors = true;

This might solve the problem, but most users probably don't want to go through the hassle.

inaridarkfox4231 avatar Sep 23 '25 09:09 inaridarkfox4231

A similar problem occurs with p5.Editor, but I was unable to confirm this on the reference site.

Image Image

This problem doesn't seem to occur when using the library locally also, so I think it's specific to OpenProcessing and p5.Editor.

inaridarkfox4231 avatar Sep 23 '25 09:09 inaridarkfox4231

Ah apparently in zod v4, Infinity stopped being a valid value for a number() validation: https://github.com/colinhacks/zod/issues/4721

Probably need to try updating this line: https://github.com/processing/p5.js/blob/53c12c39e190a16418cc2f05348711976d72c7e4/src/core/friendly_errors/param_validator.js#L67

Something like z.number().or(z.literal(Infinity)) maybe?

davepagurek avatar Sep 23 '25 12:09 davepagurek

Hi @davepagurek I’d like to work on this issue — could you please assign it to me?

Nitin2332 avatar Sep 23 '25 12:09 Nitin2332

Thanks @Nitin2332!

davepagurek avatar Sep 23 '25 12:09 davepagurek

Hi !! @davepagurek if this issue still opens please assign it to me

ayushman1210 avatar Oct 05 '25 12:10 ayushman1210

This issue is open for volunteers again! @ayushman1210 , if you still want to work on this - feel free to go ahead and make the PR. There's a suggestion in a comment above, as well as some previous reviews that may be helpful. Please let us know if there are any questions, we're happy to help! If you don't respond to claim the issue in the next 7 days, it will be open again for volunteers.

ksen0 avatar Nov 06 '25 23:11 ksen0

Hi !! davepagurekif this issue still opens please assign it to me

maynkxx avatar Nov 11 '25 05:11 maynkxx

Hey @perminder-17 , I was looking into this issue more and went through the related PRs and your suggestion. My understanding is: earlier with plain z.number() the friendly error only showed “Expected number…”. After changing it to z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]), the error builder now sees both number and constant (because +-Infinity are literals), so the message becomes “number or constant…”, which breaks the color() tests. The fix for this is by flattening the union issues and, when both number and constant appear but the only constants are +-Infinity, I’ll remove the constant (please refer to documentation for allowed values) entry from expectedTypes so the message goes back to just “Expected number…”. Please let me know if this matches what you had in mind.

menacingly-coded avatar Dec 12 '25 06:12 menacingly-coded

Hey @perminder-17 , I was looking into this issue more and went through the related PRs and your suggestion. My understanding is: earlier with plain z.number() the friendly error only showed “Expected number…”. After changing it to z.union([z.number(), z.literal(Infinity), z.literal(-Infinity)]), the error builder now sees both number and constant (because +-Infinity are literals), so the message becomes “number or constant…”, which breaks the color() tests. The fix for this is by flattening the union issues and, when both number and constant appear but the only constants are +-Infinity, I’ll remove the constant (please refer to documentation for allowed values) entry from expectedTypes so the message goes back to just “Expected number…”. Please let me know if this matches what you had in mind.

Yeah, that's the actual idea, yeah. We need to have an exttra condition check.

perminder-17 avatar Dec 12 '25 18:12 perminder-17

I know about the fact that the issue is assigned to you, but do you mind if I give it a try once?

menacingly-coded avatar Dec 13 '25 08:12 menacingly-coded

I know about the fact that the issue is assigned to you, but do you mind if I give it a try once?

Sure, feel free to give it a try.

perminder-17 avatar Dec 13 '25 09:12 perminder-17