• Re: integer divided by zero

    From Tim Rentsch@3:633/280.2 to All on Wed May 14 18:09:51 2025
    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

    Kaz Kylheku <643-408-1753@kylheku.com> writes:

    On 2025-04-25, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:

    Thiago Adams <thiago.adams@gmail.com> writes:

    Em 4/25/2025 4:05 PM, Keith Thompson escreveu:

    Thiago Adams <thiago.adams@gmail.com> writes:

    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. >>>>>
    Division by a constant zero is a constraint violation in a context
    that requires a constant expression.

    Consider this sample

    int main(){
    int a[1/0];
    }

    1/0 does not have a value in compile time,
    So I believe compilers are making "a" a VLA because 1/0 is
    not constant.

    1/0 is not a constant expression.

    A conforming compiler that supports VLAs (C99, or optionally C11 or
    later) would make `a` a VLA, with undefined behavior at runtime when
    1/0 is evaluated. For a conforming compiler that doesn't support
    VLAs (C89/C90, or optionally C11 or later) the declaration is a
    constraint violation.

    My interpretation (looking at n3301) is that 1/0 is a constant
    expression, which violates a constraint.

    ("Each constant expression shall evaluate to a constant that is in the
    range of representable values for its type.")

    The constraint makes it clear that there may be constant expressions
    which evaluate out of range. (They are constant expressions in form:
    constant operands, subject to the permitted operators.)

    The constraint's purpose isn't to give a classifying requirement in the
    sense that expressions not meeting the constraint are not taken to be
    constant. It is for diagnostic use: constant expressions not meeting the >> constraint are to be diagnosed.

    According to this interpretation the declarator a[1/0] would be deemed
    to be a regular array, not VLA, and so a constraint violation occurs
    regardless of VLA support.

    Also looking at n3301, the syntax is:

    constant-expression:
    conditional-expression

    That doesn't imply that all conditional-expressions are
    constant-expressions. [...]

    Based on a memory of reading an old Defect Report (sorry don't know
    which one), I think there was an official statement to the effect
    that any conditional-expression is also a constant-expression, but
    the constraints aren't in force unless the surrounding context
    requires a constant-expression. In other words whether something is
    a constant-expression is purely a local syntactic condition; the
    conditions stated for constant expressions matter only when being
    a constant expression is necessary to satisfy some other rule in
    the C standard.


    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From goldside000@outlook.com@3:633/280.2 to All on Sun Jun 1 04:56:45 2025
    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.


    Yeah, I cannot think of an architecture where dividing by zero doesn't
    result in an exception or error of some sort. But there's ARM, where it
    might return 0, the dividend, or just garbage.

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: A noiseless patient Spider (3:633/280.2@fidonet)
  • From Keith Thompson@3:633/280.2 to All on Sun Jun 1 07:07:02 2025
    goldside000@outlook.com writes:
    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.

    Yeah, I cannot think of an architecture where dividing by zero doesn't
    result in an exception or error of some sort. But there's ARM, where it
    might return 0, the dividend, or just garbage.

    Yes, I mentioned that in this thread more than a month ago. On two
    Linux AARM64 systems I've tried, dividing an integer by zero in a
    non-constant expression yields 0.

    --
    Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
    void Void(void) { Void(); } /* The recursive call of the void */

    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: None to speak of (3:633/280.2@fidonet)
  • From Scott Lurndal@3:633/280.2 to All on Sun Jun 1 07:53:31 2025
    Reply-To: slp53@pacbell.net

    Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
    goldside000@outlook.com writes:
    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.

    Yeah, I cannot think of an architecture where dividing by zero doesn't
    result in an exception or error of some sort. But there's ARM, where it
    might return 0, the dividend, or just garbage.

    Yes, I mentioned that in this thread more than a month ago. On two
    Linux AARM64 systems I've tried, dividing an integer by zero in a >non-constant expression yields 0.


    From the ARMv8 Architecture Reference Manual (DDI0487)

    If a signed integer division (INT_MIN / -1) is performed
    where INT_MIN is the most negative integer value representable
    in the selected register size, then the result overflows the
    signed integer range. No indication of this overflow is produced
    and the result that is written to the destination register is INT_MIN.

    A division by zero results in a zero being written to the destination
    register, without any indication that the division by zero occurred.


    --- MBSE BBS v1.1.1 (Linux-x86_64)
    * Origin: UsenetServer - www.usenetserver.com (3:633/280.2@fidonet)