Sujet : Re: integer divided by zero
De : antispam (at) *nospam* fricas.org (Waldek Hebisch)
Groupes : comp.lang.cDate : 26. Apr 2025, 10:57:00
Autres entêtes
Organisation : To protect and to server
Message-ID : <vuial9$ph3c$1@paganini.bofh.team>
References : 1
User-Agent : tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
Thiago Adams <
thiago.adams@gmail.com> wrote:
Does anyone know of any platform where integer division by zero returns
a number, or in other words, where it's not treated as an error? I'm
asking because division by zero is undefined behaviour, but I think
division by a constant zero should be a constraint instead.
That question/proposal keeps to reappearing in various languages.
One reason to _not_ make it a constraint is the following nonportable
code (which assumes that division by zero is trapped at runtime):
int
signal_error(void) {
return 1/0;
}
This in not portable, but at least in the past it was most portable
method to signal errors. Portable in the sense that the method worked
in several programming languages and on various operating systems.
C library has 'exit' which should be preferable in most cases but
sometimes division by zero gives most appropriate indication of
error.
There is also another (probably more important) issue: there may
be division by zero on code path that is never executed. One can
argue that division by "absolute" zero should be eliminated and non
exectuted code path should be removed. But having zero or not may
depend on configuration, on configurations where constant is nonzero
code path may be doing something useful.
I am affraid that there is enough uses of the both features to
consider your proposal as breaking change.
-- Waldek Hebisch