New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map does not throw an error when producing NaN #3314

Closed
liasomething opened this Issue May 22, 2015 · 14 comments

Comments

Projects
None yet
5 participants
@liasomething

liasomething commented May 22, 2015

example:
i have a Shape class and every Shape gets an id number.
i am mapping the id number to some speed variable like this.
float verticalSpeed = map(id, 0, maxShapeNum-1, 2, 5);

but when i only have 1 shape (maxShapeNum being 1) , then the mapping writes something like:
float verticalSpeed = map(id, 0, 0, 2, 5);
and this results in verticalSpeed in being Nan which can easily be overlooked.

it would be great to get some error message when something like this happens... :)

@clankill3r

This comment has been minimized.

Show comment
Hide comment
@clankill3r

clankill3r Jun 7, 2015

I think it's up to you to deal with such a situation
for example:
map(id, 0, max(maxShapeNum-1, 1), 2, 5);

clankill3r commented Jun 7, 2015

I think it's up to you to deal with such a situation
for example:
map(id, 0, max(maxShapeNum-1, 1), 2, 5);

@liasomething

This comment has been minimized.

Show comment
Hide comment
@liasomething

liasomething Jun 7, 2015

my point here was that it would just be nice not to have to deal with such a situation myself. :)

liasomething commented Jun 7, 2015

my point here was that it would just be nice not to have to deal with such a situation myself. :)

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Jun 7, 2015

Member

Hm, we don't throw errors for NaN anywhere in the API, so this would be a fairly huge shift to do it consistently. For instance, why would map() throw an error but line() or vertex() do not? (Answer: we probably would need to support all of them, and that's a lot of overhead.) As a rule, we only throw errors/halt the sketch for things that really mean the sketch can't continue.

But... I can understand the interest, so I'll check with @shiffman @REAS and @codeanticode.

Member

benfry commented Jun 7, 2015

Hm, we don't throw errors for NaN anywhere in the API, so this would be a fairly huge shift to do it consistently. For instance, why would map() throw an error but line() or vertex() do not? (Answer: we probably would need to support all of them, and that's a lot of overhead.) As a rule, we only throw errors/halt the sketch for things that really mean the sketch can't continue.

But... I can understand the interest, so I'll check with @shiffman @REAS and @codeanticode.

@shiffman

This comment has been minimized.

Show comment
Hide comment
@shiffman

shiffman Jun 8, 2015

Member

Maybe this is less a question of dealing with NaN and more a question of how map() should deal with an invalid range. If I understand correctly, the issue is:

float val = map(id, 0, 0, 2, 5); // returns NaN

What if we instead changed it to:

float val = map(id, 0, 0, 2, 5); // just returns id back? just always returns 2?

Maybe this is too much hocus-pocus and a bad idea.

In any case, I agree that as much as it would be nice I don't think we can add special error handling for NaN everywhere in the API. Adds to much overhead for somewhat of an edge case? I am not confident in this opinion, just rattling off some thoughts.

Member

shiffman commented Jun 8, 2015

Maybe this is less a question of dealing with NaN and more a question of how map() should deal with an invalid range. If I understand correctly, the issue is:

float val = map(id, 0, 0, 2, 5); // returns NaN

What if we instead changed it to:

float val = map(id, 0, 0, 2, 5); // just returns id back? just always returns 2?

Maybe this is too much hocus-pocus and a bad idea.

In any case, I agree that as much as it would be nice I don't think we can add special error handling for NaN everywhere in the API. Adds to much overhead for somewhat of an edge case? I am not confident in this opinion, just rattling off some thoughts.

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 10, 2015

Member

Yeah, I'm not sure what the right behavior would be... In most cases we shouldn't halt a sketch based on a NaN value coming back. But I know this sort of thing can be really difficult to debug, so it'd be nice to have a way of warning users.

Member

benfry commented Aug 10, 2015

Yeah, I'm not sure what the right behavior would be... In most cases we shouldn't halt a sketch based on a NaN value coming back. But I know this sort of thing can be really difficult to debug, so it'd be nice to have a way of warning users.

@shiffman

This comment has been minimized.

Show comment
Hide comment
@shiffman

shiffman Aug 11, 2015

Member

We could detect an invalid range for map() and throw an error in the console like "vertex(x,y,z) not available with this renderer" kind of thing? It's a wonky precedent (there are so many places we could do that) but map() is so incredibly useful and often introduced early on in a course/workshop context maybe it makes sense?

Member

shiffman commented Aug 11, 2015

We could detect an invalid range for map() and throw an error in the console like "vertex(x,y,z) not available with this renderer" kind of thing? It's a wonky precedent (there are so many places we could do that) but map() is so incredibly useful and often introduced early on in a course/workshop context maybe it makes sense?

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 11, 2015

Member

Would that be enough or do you think the audience that most needs to see that would also be most likely to ignore it unless it halted the sketch?

Member

benfry commented Aug 11, 2015

Would that be enough or do you think the audience that most needs to see that would also be most likely to ignore it unless it halted the sketch?

@shiffman

This comment has been minimized.

Show comment
Hide comment
@shiffman

shiffman Aug 11, 2015

Member

It might not be enough, but at the moment I'm feeling like we should not halt any sketch that wouldn't "naturally" be halted by Java itself. So maybe let's just try a warning and see?

Member

shiffman commented Aug 11, 2015

It might not be enough, but at the moment I'm feeling like we should not halt any sketch that wouldn't "naturally" be halted by Java itself. So maybe let's just try a warning and see?

@REAS

This comment has been minimized.

Show comment
Hide comment
@REAS

REAS Aug 11, 2015

Member

There are so many things that can create issues with code; math errors, logic errors, infinite loops. I think NaN errors are one example of many. I see this as one part of a much larger conversation. To react specifically to the thread, I think a warning would be nice if it's possible, but I think I agree with Dan that if Java doesn't halt, Processing shouldn't too.

Member

REAS commented Aug 11, 2015

There are so many things that can create issues with code; math errors, logic errors, infinite loops. I think NaN errors are one example of many. I see this as one part of a much larger conversation. To react specifically to the thread, I think a warning would be nice if it's possible, but I think I agree with Dan that if Java doesn't halt, Processing shouldn't too.

@liasomething

This comment has been minimized.

Show comment
Hide comment
@liasomething

liasomething Aug 11, 2015

i don't think processing necessarily needs to stop, but an error message would help very much i think.

if i would do something like this:
float myVarThatIThinkIsNotZero = 0;
float m = map(3, 0, myVarThatIThinkIsNotZero, 2, 5);
ellipse(100, m, 30, 30);
then i would probably ask myself for a very long time why my ellipse is not drawn.

would some error like "your code contains a (variable with a value of) NaN" or something like that be possible ?
without stopping processing...
this way it would be up to me to find the variable, but at least i would get a hint that something in my code is wrong...

liasomething commented Aug 11, 2015

i don't think processing necessarily needs to stop, but an error message would help very much i think.

if i would do something like this:
float myVarThatIThinkIsNotZero = 0;
float m = map(3, 0, myVarThatIThinkIsNotZero, 2, 5);
ellipse(100, m, 30, 30);
then i would probably ask myself for a very long time why my ellipse is not drawn.

would some error like "your code contains a (variable with a value of) NaN" or something like that be possible ?
without stopping processing...
this way it would be up to me to find the variable, but at least i would get a hint that something in my code is wrong...

@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 11, 2015

Member

Added the warning for 3.0 beta 3. This code:

float myVarThatIThinkIsNotZero = 0;
float m = map(3, 0, myVarThatIThinkIsNotZero, 2, 5);

will produce the error message:

map(3, 0, 0, 2, 5) called, which returns NaN or infinity
Member

benfry commented Aug 11, 2015

Added the warning for 3.0 beta 3. This code:

float myVarThatIThinkIsNotZero = 0;
float m = map(3, 0, myVarThatIThinkIsNotZero, 2, 5);

will produce the error message:

map(3, 0, 0, 2, 5) called, which returns NaN or infinity
@benfry

This comment has been minimized.

Show comment
Hide comment
@benfry

benfry Aug 11, 2015

Member

On second thought, added separate checks so it will either say

map(3, 0, 0, 2, 5) called, which returns NaN (not a number)

or it would say which returns infinity if that's the case.

Member

benfry commented Aug 11, 2015

On second thought, added separate checks so it will either say

map(3, 0, 0, 2, 5) called, which returns NaN (not a number)

or it would say which returns infinity if that's the case.

@liasomething

This comment has been minimized.

Show comment
Hide comment
@liasomething

liasomething Aug 11, 2015

lovely, thanks a lot !!!

liasomething commented Aug 11, 2015

lovely, thanks a lot !!!

@clankill3r

This comment has been minimized.

Show comment
Hide comment
@clankill3r

clankill3r Aug 26, 2015

@benfry Is it possible to output a stack trace as well when it happens?
I have the warning a few times in a 3d skecth but I don't even use the map method!!

clankill3r commented Aug 26, 2015

@benfry Is it possible to output a stack trace as well when it happens?
I have the warning a few times in a 3d skecth but I don't even use the map method!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment