Sujet : Re: integer divided by zero
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 25. Apr 2025, 18:50:39
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vugi1f$ghms$1@dont-email.me>
References : 1
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 25/04/2025 19:38, Thiago Adams 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.
Some processors might give a specific value for their integer division instructions for divide by 0 - or they might support masking of relevant traps, exceptions or other fault mechanisms. Software should probably not rely on division by 0 behaviour, even if it is in non-portable code.
But most processors that are too small to have a fault, trap or exception system are also too small to have division instructions. There are exceptions, of course.
Division by a constant 0 is not a constraint in C because that would require compilers to detect division by zero. But conforming compilers are free to detect it anyway, and issue a warning. Note that compilers should probably not consider it a fatal error in the code (i.e., one that stops the compilation) unless the compiler can be sure that the code in question is not executed. There is no problem having undefined behaviour in the code until you try to execute that code path.