processing / processing-website

Repository for the processing.org website
https://processing.org
GNU General Public License v2.0
66 stars 94 forks source link

Equality operator fails with NaN #381

Open Achimoto opened 4 years ago

Achimoto commented 4 years ago

Issue description

"==" operator fail with NaN confuses beginners

float result = float(s); // String s = "something wrong" println(result); // prints NaN if (result == Float.NaN) throw new Exception("Float error"); // does not throw exception

Proposed fix

maybe add correct syntax proposal (Float.isNaN) to language description, section Equality

benfry commented 4 years ago

Hm, maybe we need to add a NaN page to equality, since that's what would be seen in the console… (Not sure of a better place and also concerned that people would actually find it.) In that page we could mention testing a variable against itself if (x != x) and Float.isNan(x) as ways to test for NaN, and x == x as a way to verify not NaN.

Though any “beginner” who knows Float.NaN probably also knows Float.isNaN() and its use. :)

Achimoto commented 4 years ago

Hi Ben,A lot of beginners (in my workshops) have heard of NaN but are not aware that NaN cannot be treated by logic operators. They think human (if x is not a number --> x == NaN). The IDE gives warnings e.g. if a int is out of range. Maybe you could implement a warning if "Float.NaN" is found together with logic operators like == or != .

On Thursday, 30 April 2020, 12:58:01 CEST, Ben Fry <notifications@github.com> wrote:  

Hm, maybe we need to add a NaN page to equality, since that's what would be seen in the console… (Not sure of a better place and also concerned that people would actually find it.) In that page we could mention testing a variable against itself if (x != x) and Float.isNan(x) as ways to test for NaN.

Though any “beginner” who knows Float.NaN probably also knows Float.isNaN() and its use. :)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

REAS commented 4 years ago

I'm wondering how common it is for beginners to see NaN and how it happens. I think some examples would help. It doesn't feel right to "bury" it inside the equality reference page (https://processing.org/reference/equality.html), but maybe that's right? What about NaN having it's own page?

jeremydouglass commented 4 years ago

What about NaN having it's own page?

That would make it easy to add a "see also" reference from the equality page -- and from anywhere else needed.