> The well-defined semantics of division by zero are part of Annex F of the C
> specification, which is an optional thing that compilers are not required
> to implement. In the absence of Annex F, division by zero has undefined
> behavior. C++ doesn't have anything like Annex F and division by zero is
> stated to be undefined (although they mention in a non-normative comment
> that most implementations allow the behavior to be controlled using library
> functions).
>
> A division by zero could result in a trap, exception, or signal such as
> SIGFPE. The convention has been to move away from those behaviors because
> it's convenient to use 1.0/0 as a way to get infinity, but unless Lua's
> documentation specifically requires that you don't do that, nothing stops
> you from passing a compiler flag or calling a library function to change
> that.
That part I know. C is not required to implement IEEE arithmetic (as it
is not required to use 2-complement integers). But repeating my original
question, does anyone know of real platforms where this may cause real
problems? Lua always has allowed division by 0 and we have never got any
report about problems there.
-- Roberto
Once upon a time I remember "how do I turn on error handling for division by zero?" being a common question in my programming circles. I haven't seen it recently because the mainstream behavior is kinda assumed by this point.
I think the main reason you wouldn't get reports of problems is because such a report would imply that someone is using such a system unknowingly. Given that it's an opt-in kind of feature on the dominant software platforms, encountering a FPE means either (1) you turned on FPEs, or (2) you're using a niche platform. In either case, the developer is likely to understand what's going on and not blame Lua for it.
On the assumption that Lua doesn't intentionally do divisions by zero in the internals or in the standard library, you can get away with documenting that division by zero is undefined for Lua scripts. If you want to explicitly define the behavior, then you'll need to likewise explicitly handle it in the code.
/s/ Adam