| ▲ | robertlagrant an hour ago | |
What we need is a numeric type that cannot be zero. | ||
| ▲ | roadbuster 12 minutes ago | parent | next [-] | |
The only way to achieve this is to either put a runtime software check on a variable whenever it's assigned/used, or to literally add hardware support in processors themselves which literally throws an interrupt when a "neverShallBeZero" variable is assigned to zero. There's no viable way to statically prove at compile-time that these variables will never become zero at runtime, ultimately forcing a system of endless runtime checks (be it software or hardware)... which is why processors already throw exception interrupts when division by zero is attempted. | ||
| ▲ | winwang 44 minutes ago | parent | prev | next [-] | |
Every day, we stray closer to Haskell. Dare I say it: good! | ||
| ▲ | drdaeman an hour ago | parent | prev | next [-] | |
What we need are refinement types, where there’s a base type and a predicate. F* has this: | ||
| ▲ | yeputons an hour ago | parent | prev | next [-] | |
And also cannot be INT_MIN, otherwise -1 / INT_MIN is undefined behaviour(!) in C and C++. | ||
| ▲ | rhdunn an hour ago | parent | prev | next [-] | |
It would be more flexible for a compiler to reuse the range analysis logic used in optimizations for statically verifiable divide by zeros. That way you could extend it to other things like statically verifiable overflows. | ||
| ▲ | duped 17 minutes ago | parent | prev [-] | |
For stuff like niche value optimization sure. For practical arithmetic code, nah. Like with this bug, all that changed is that garbage data in gives the user an error that they tried to process garbage data. Adding a new type doesn't make the code better, it just moves the error around. And you really don't want an infix division operator to fail to type check if the right hand side isn't a nonzero type, do you? | ||