-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Proposal: Throw on division by zero #670
Comments
Yes, absolutely. Most languages define division by zero as NaN or throw an exception/panic/halt; we should do the same rather than silently returning an incorrect answer. Ideally a hard fork will fix this in the EVM, but in the meantime Solidity should definitely add this check itself, just like it checks array bounds. |
On the object-level, I support this change. Failing silently is the worst possible thing to do from a safety perspective. On the meta-level, I am fine with backwards-compatibility breaking changes in Solidity, especially since I think the language will evolve over the next six months as a result of the community discovering incremental changes to make the risk of bugs much smaller. One possible way to make changes like this graceful is to put a hashbang tag into the file, or some similar parameter, specifying a version. |
Got here from the ethereum wiki. The issue is closed, so this means that division by zero is now "safe"? |
Division by zero currently behaves exactly like division by zero on the EVM: It results in zero.
This could be changed into throwing an exception, just like array access out of bounds and other cases.
Do we want to break backwards compatibility to add this feature?
The check will of course be optimized away for division by constants. An improved optimizer (it cannot do it yet) could also optimize it away if the developer checked for zero just prior to the division. The downside of the comparison is also that block-level optimization cannot be done anymore.
The text was updated successfully, but these errors were encountered: