• Re: integer divided by zero

    From David Brown@21:1/5 to Thiago Adams on Fri Apr 25 19:50:39 2025
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From John McCue@21:1/5 to Thiago Adams on Fri Apr 25 19:41:23 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.

    I know of none and I have programmed on a few platforms.

    Some had the ability to trap these issues with a statement,
    but usually it was easier to check for 0 first.

    --
    [t]csh(1) - "An elegant shell, for a more... civilized age."
    - Paraphrasing Star Wars

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Keith Thompson on Fri Apr 25 21:36:46 2025
    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.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Waldek Hebisch@21:1/5 to Thiago Adams on Sat Apr 26 09:57:00 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.

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to thiago.adams@gmail.com on Sat Apr 26 16:27:44 2025
    In article <vuiu6e$2lqot$1@dont-email.me>,
    Thiago Adams <thiago.adams@gmail.com> wrote:
    Em 4/25/2025 5:32 PM, Keith Thompson escreveu:
    Thiago Adams <thiago.adams@gmail.com> writes:
    [...]
    Since 1/0 is not a constant, then if 1/0 were a constrain, the
    programmer could use a variable instead.

    static int zero = 0;
    1/zero //ok

    They could, but why? `1/zero` is not "ok"; it's undefined behavior.


    It could me used in the sample given by Waldek

    int signal_error(void) {
    return 1/0;
    }

    int signal_error(void) {
    int zero = 0;
    return 1/zero;
    }

    Wouldn't it be easier just to use raise(3)?

    --
    So to cure the problem of arrogant incompetent rich people we should turn
    the government over to an arrogant incompetent trust fund billionaire
    who knows nothing about government and who has never held a job in his
    entire spoiled life?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to Kenny McCormack on Sat Apr 26 17:01:11 2025
    On 2025-04-26, Kenny McCormack <gazelle@shell.xmission.com> wrote:
    int signal_error(void) {
    int zero = 0;
    return 1/zero;
    }

    Wouldn't it be easier just to use raise(3)?

    This is being described elsethread as something portable
    across languages, not all of which have access to signal-related
    functions.

    --
    TXR Programming Language: http://nongnu.org/txr
    Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
    Mastodon: @Kazinator@mstdn.ca

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Bonita Montero on Mon Apr 28 08:54:19 2025
    On 27/04/2025 14:33, Bonita Montero wrote:
    The platform with the most comfortable handling of division
    by zeroes is Windows. Win32 allows to catch that errors
    easily, whereas with Posix it's hard to continue the code in
    the same function or with a calling function.

    Tell it to the marines, because the sailors won't believe you.

    <https://web.archive.org/web/20221230163646/https://www.wired.com/1998/07/sunk-by-windows-nt/>

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Bonita Montero on Mon Apr 28 12:01:54 2025
    On 28/04/2025 11:49, Bonita Montero wrote:
    Am 28.04.2025 um 09:54 schrieb Richard Heathfield:

    Tell it to the marines, because the sailors won't believe you.
    <https://web.archive.org/web/20221230163646/https://
    www.wired.com/1998/07/sunk-by-windows-nt/>

    Then they didn't catch the divide by zero.

    You wrote: "The platform with the most comfortable handling of
    division by zeroes is Windows."

    The report tells us: "The data contained a zero where it
    shouldn't have, and when the software attempted to divide by
    zero, a buffer overrun occurred -- crashing the entire network
    and causing the ship to lose control of its propulsion system."

    A platform that crashes the network is not "handling" division by
    zero; it's /mis/handling division by zero. Unix would have
    core-dumped the program and left everything else up and running,
    including the Yorktown.

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Damon@21:1/5 to Bonita Montero on Mon Apr 28 07:15:37 2025
    On 4/27/25 9:33 AM, Bonita Montero wrote:
    Am 25.04.2025 um 19:38 schrieb Thiago Adams:

    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.

    I guess the trap is induced on all platforms since there's no binary representation for +/-inf with integrals.
    The platform with the most comfortable handling of division by zeroes
    is Windows. Win32 allows to catch that errors easily, whereas with
    Posix it's hard to continue the code in the same function or with
    a calling function.


    NO, not all processors have a trap for divide by zero on integers, and
    many that do allow the disabling of that trap.

    The results of the divide are just declaired to be undefined (but their
    is likely some definite result that will happen due to the division
    algorithm being performed).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Heathfield@21:1/5 to Thiago Adams on Mon Apr 28 14:52:17 2025
    On 25/04/2025 18: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.

    C? I doubt it very much.

    In the 1980s, there was a BASIC dialect for 68000s, called GFA
    BASIC, which was available as both an interpreter and a compiler.
    The interpreter produced an error for 0/0, but the compiler
    optimised it to 1 (on the grounds that, for any a, a/a=1).

    --
    Richard Heathfield
    Email: rjh at cpax dot org dot uk
    "Usenet is a strange place" - dmr 29 July 1999
    Sig line 4 vacant - apply within

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Waldek Hebisch@21:1/5 to Richard Damon on Mon Apr 28 14:44:53 2025
    Richard Damon <richard@damon-family.org> wrote:
    On 4/27/25 9:33 AM, Bonita Montero wrote:
    Am 25.04.2025 um 19:38 schrieb Thiago Adams:

    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.

    I guess the trap is induced on all platforms since there's no binary
    representation for +/-inf with integrals.
    The platform with the most comfortable handling of division by zeroes
    is Windows. Win32 allows to catch that errors easily, whereas with
    Posix it's hard to continue the code in the same function or with
    a calling function.


    NO, not all processors have a trap for divide by zero on integers, and
    many that do allow the disabling of that trap.

    The results of the divide are just declaired to be undefined (but their
    is likely some definite result that will happen due to the division
    algorithm being performed).

    RISCV seem to be one of those that do not trap.

    --
    Waldek Hebisch

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael S@21:1/5 to Scott Lurndal on Mon Apr 28 18:03:39 2025
    On Mon, 28 Apr 2025 14:28:41 GMT
    scott@slp53.sl.home (Scott Lurndal) wrote:

    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 28.04.2025 um 13:15 schrieb Richard Damon:

    NO, not all processors have a trap for divide by zero on integers,
    and many that do allow the disabling of that trap.

    Windows requires processors that can trap divisions by zero,

    No, Windows only runs on processors (x86) that happen to trap
    divisions by zero. Linux runs on hundreds of processors, some of
    which do, some of which don't.

    Historically, Windows NT family of Windows OSes supported 6 different architectures (or 8, depending of whether you consider AMD64 and
    Aarch64 as different or the same as their 32-bit predecessors).
    2 architectures architectures supported in the latest version (Win11).
    IIRC, majority of those architectures can trap on integer division
    by zero. Exceptions to that are Alpha and Itanium that *do not have*
    integer division instructions. Alpha has floating-point division that
    traps on zero. Itanium has no division instructions at all.
    I'd guess, emulation of trap by compiler is easier in this cases than
    in case of processors that do have integer division but do not trap on
    zero divisor.

    The "other" Windows OS family, now mostly defunct Windows CE, supported
    few more architectures. I don't know nearly enough about some of them.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Bonita Montero on Mon Apr 28 14:28:41 2025
    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 28.04.2025 um 13:15 schrieb Richard Damon:

    NO, not all processors have a trap for divide by zero on integers, and
    many that do allow the disabling of that trap.

    Windows requires processors that can trap divisions by zero,

    No, Windows only runs on processors (x86) that happen to trap divisions
    by zero. Linux runs on hundreds of processors, some of which
    do, some of which don't.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Scott Lurndal@21:1/5 to Michael S on Mon Apr 28 16:35:26 2025
    Michael S <already5chosen@yahoo.com> writes:
    On Mon, 28 Apr 2025 14:28:41 GMT
    scott@slp53.sl.home (Scott Lurndal) wrote:

    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 28.04.2025 um 13:15 schrieb Richard Damon:

    NO, not all processors have a trap for divide by zero on integers,
    and many that do allow the disabling of that trap.

    Windows requires processors that can trap divisions by zero,

    No, Windows only runs on processors (x86) that happen to trap
    divisions by zero. Linux runs on hundreds of processors, some of
    which do, some of which don't.

    Historically, Windows NT family of Windows OSes supported 6 different >architectures (or 8, depending of whether you consider AMD64 and
    Aarch64 as different or the same as their 32-bit predecessors).

    MIPS was supported for a time as well.

    That doesn't matter today - modern windows is x86_64 only.

    In any case, using 16-bit characters in NTFS filenames was
    a mistake from the start.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Muttley@DastardlyHQ.org@21:1/5 to All on Mon Apr 28 17:02:47 2025
    On Mon, 28 Apr 2025 18:03:39 +0300
    Michael S <already5chosen@yahoo.com> wibbled:
    On Mon, 28 Apr 2025 14:28:41 GMT
    scott@slp53.sl.home (Scott Lurndal) wrote:

    Bonita Montero <Bonita.Montero@gmail.com> writes:
    Am 28.04.2025 um 13:15 schrieb Richard Damon:

    NO, not all processors have a trap for divide by zero on integers,
    and many that do allow the disabling of that trap.

    Windows requires processors that can trap divisions by zero,

    No, Windows only runs on processors (x86) that happen to trap
    divisions by zero. Linux runs on hundreds of processors, some of
    which do, some of which don't.

    Historically, Windows NT family of Windows OSes supported 6 different >architectures (or 8, depending of whether you consider AMD64 and
    Aarch64 as different or the same as their 32-bit predecessors).
    2 architectures architectures supported in the latest version (Win11).
    IIRC, majority of those architectures can trap on integer division
    by zero. Exceptions to that are Alpha and Itanium that *do not have*
    integer division instructions. Alpha has floating-point division that
    traps on zero. Itanium has no division instructions at all.

    If it has right shift it has divide by 2.

    However it seems an odd ommision in this day and age.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rosario19@21:1/5 to Keith Thompson on Wed Apr 30 12:41:15 2025
    On Fri, 25 Apr 2025 12:05:13 -0700, Keith Thompson wrote:

    Thiago Adams 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.

    I wrote this quick and dirty program:

    #include <stdio.h>
    #include <time.h>
    int main(void) {
    int one = time(NULL) / 1000000000;
    int zero = one - 1;
    int ratio = one / zero;
    printf("%d\n", ratio);

    in math
    1/0 has to be +infinite ( for approximation +INT_MAX for C?)
    but should be one error if one want one infinite array long
    as in "int a[1/0]" or want to do infinite/0 infinite-inifinite infinite/infinite ecc could be ok if one make infinite+infinite=
    infinite, infinite * infinite = infinite ecc



    }

    It's not portable, but on the systems where I've tried it it sets
    one to 1 and zero to 0 in a way that the compiler can't detect,
    so the division won't be optimized away. (At least, it does so if
    you run it between 2001 and 2286.)

    On x86_64, it dies with a floating point exception.

    On aarch64, it prints 0.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Brown@21:1/5 to All on Wed Apr 30 13:35:09 2025
    On 30/04/2025 12:41, Rosario19 wrote:
    On Fri, 25 Apr 2025 12:05:13 -0700, Keith Thompson wrote:

    Thiago Adams 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.

    I wrote this quick and dirty program:

    #include <stdio.h>
    #include <time.h>
    int main(void) {
    int one = time(NULL) / 1000000000;
    int zero = one - 1;
    int ratio = one / zero;
    printf("%d\n", ratio);

    in math
    1/0 has to be +infinite ( for approximation +INT_MAX for C?)

    In common mathematics, 1 / 0 is undefined - just like in C. If you
    extend the set of real numbers to include some kind of infinity ∞ , you immediately lose many other useful properties of the set. INT_MAX is
    not a good approximation - there is no int value that makes sense for
    the result of 1 / 0.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Vir Campestris@21:1/5 to Thiago Adams on Sat May 3 21:47:56 2025
    On 25/04/2025 18: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.

    I'm not interested enough to read this whole long thread, but ISTR that
    1.0/0.0 on a '287 returns #inf - infinity.

    #inf is sticky. If it's on your input it tends to be on your outputs.

    Andy

    --
    Do not listen to rumour, but, if you do, do not believe it.
    Ghandi.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Michael S@21:1/5 to Vir Campestris on Sat May 3 23:58:07 2025
    On Sat, 3 May 2025 21:47:56 +0100
    Vir Campestris <vir.campestris@invalid.invalid> wrote:

    On 25/04/2025 18: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.

    I'm not interested enough to read this whole long thread, but ISTR
    that 1.0/0.0 on a '287 returns #inf - infinity.

    #inf is sticky. If it's on your input it tends to be on your outputs.

    Andy


    I understand that reading the whole thread take plenty of time.
    But reading the subject line is easy.
    The thread is very obviously about *integer* division.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Tim Rentsch@21:1/5 to Keith Thompson on Wed May 14 01: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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)