-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Divide by zero in map() function #2219
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
Comments
I like this one as I can ask about exception cause Adruino Passing non initialized Handling of division by zero for integer numbers on various Arduino boards can be quickly checked with a short sketch:
Results are as follows (Arduino IDE 1.6.9): Arduino UNO (core ver. 1.5.11): Arduino DUE / SAM (core ver.1.6.7) : ESP8266 / Arduino (core ver. 2.3.0): This explains why Personally I prefer to keep the ESP telling me that I am performing an illegal operation instead of potentially seeping this fact under carpet by returning -1 or 0 as a result. Having legitimate -1 inside some complex calculations may return seemingly legitimate result for invalid arguments. Now, returning to my original question, does anybody know why division by zero is flagged with |
I assumed the same (divide by zero) but since -1 is in general "Error" you can check for it but crashing is no option that's acceptable for my opinion. |
@krzychb It's slightly weird that you get an exception at PC=0x40106889. When I run the same sketch exception happens, as I would expect, at
Regarding your question why we get exception 0 and not exception 6, it's pretty easy. CPU core inside the ESP8266 doesn't have hardware division instruction enabled. If it did, this instruction would cause exception 6. Because there is no division instruction, gcc calls aforementioned Overall, i don't consider the fact that integer division triggers exception an issue. Just as unaligned reads/writes work (albeit slowly) on some architectures and don't work on others. However, I think we should still check divisor against zero in |
I have just implemented this function in my code generator app (pfodDesignerV2). In that case I use the code like the following |
"fixed" to return -1 in case of division by zero |
V 2.3.0
Arduino Uno / ESP-12F
While porting an sketch from Arduino it mysteriously crashed. I isolated the problem to the map() function when the arrays are empty. Once I fill it it works as it should.
The sketch is working without problems on Arduino Uno (returning -1 for each value) but uploaded to the ESP it crashes.
(Background: I need empty calibration data which will be filled later once sensors are read)
Minimal Sketch part to show it:
The text was updated successfully, but these errors were encountered: