Some bug (may be) in Array.indexOf #1254
Replies: 3 comments
-
Posted at 2018-11-29 by @gfwilliams Which board are you running this on? It looks like this is kind of a bug and kind of not a bug. What you're hitting is floating point inaccuracy. The issue boils down to whether this is true or false: You can't represent In fact to show you the actual binary numbers behind o.95:
So there's one bit different in Espruino. I just checked with Node.js and it looks like the parsing of numbers is actually different, since Node.js gets However it's really bad practice to do what you're doing anyway. Even if I fix this issue there will be cases where it fails. For instance the classic To work around this you could explicitly check if the number was close or not: I think this kind of thing is always better to check on the forum with first - I have now filed a bug but in many cases they can be things that have been asked before - very often on GitHub I just keep getting similar issues filed time after time. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-11-29 by @gfwilliams Just to add that another option is just to compare strings...
|
Beta Was this translation helpful? Give feedback.
-
Posted at 2018-11-29 by asez73 Thanks, So I tested the second solution and it works. I totally agree with the no bug approach. The trick is that it's a silent killer. As well as dynamic typing is indeed. However, the value of v is continuous (risk level in probability) so the exact comparison done by Array.indexOf was not my need. I finally settled on Array.findIndex as I can get an index through a comparison function even if the value is really not in the array but rather in between values in the array. My array is sorted in increase order. This page provides the formulas and statistics tables from which I extracted the 2 arrays below...
Thank's again for your reactivity and commitment to understand our questions! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Posted at 2018-11-29 by asez73
Hi @gfwilliams,
While trying to do some statistics, I found that situation on a v2.00 upgrade Pico:
However, using the test box in the mozilla page, I got
which once run showed:
So, is this a bug or I am not understanding something?
P.S.: Should I report this type of problem on the forum or in github?
Beta Was this translation helpful? Give feedback.
All reactions