Re: integer divided by zero

Liste des GroupesRevenir à cl c 
Sujet : Re: integer divided by zero
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.c
Date : 26. Apr 2025, 00:09:00
Autres entêtes
Organisation : None to speak of
Message-ID : <87frhvq1o3.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5
User-Agent : Gnus/5.13 (Gnus v5.13)
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.  If it did, then

    time_t t = time(NULL);

would be a constraint violation; `time(NULL)` is a
conditional-expression, but it violates the constraint on function
calls -- but a constant expression is not required in that context
(assuming it's at block scope).

A construct is a constant-expression only if it appears in a place where
the grammar requires a constant-expression.  An array-declarator is not
one of those contexts; the syntax requires an assignment-expression.

There's a constraint on array declarators: "If the expression is
a constant expression, it shall have a value greater than zero."
That wording is a little iffy, but the meaning is clear enough.
It might have been more pedantically correct to say "If the
expression would be a constant expression if it appeared in a
context that requires a constant expression, ...".

None of these:
    int foo[time() % 10 + 1];
    int bar[1 / 0];
    int baz[INT_MAX + 1];
violates any constraint, even though constant expressions may not
include function calls, division by zero, or signed overflow.

A vaguely analogous case:
    int n = 1 + 2 * 3;
`1 + 2` is not an additive expression.  It looks like one, but it
appears in a context that syntactically doesn't allow an additive
expression.

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

Date Sujet#  Auteur
25 Apr 25 * integer divided by zero46Thiago Adams
25 Apr 25 +* Re: integer divided by zero6David Brown
25 Apr 25 i`* Re: integer divided by zero5Thiago Adams
25 Apr 25 i `* Re: integer divided by zero4Keith Thompson
26 Apr 25 i  `* Re: integer divided by zero3Thiago Adams
26 Apr 25 i   `* Re: integer divided by zero2Kenny McCormack
26 Apr 25 i    `- Re: integer divided by zero1Kaz Kylheku
25 Apr 25 +* Re: integer divided by zero9Keith Thompson
25 Apr 25 i+* Re: integer divided by zero6Thiago Adams
25 Apr 25 ii`* Re: integer divided by zero5Keith Thompson
25 Apr 25 ii +* Re: integer divided by zero3Kaz Kylheku
26 Apr 25 ii i`* Re: integer divided by zero2Keith Thompson
14 May 25 ii i `- Re: integer divided by zero1Tim Rentsch
28 Apr 25 ii `- Re: integer divided by zero1Thiago Adams
30 Apr 25 i`* Re: integer divided by zero2Rosario19
30 Apr 25 i `- Re: integer divided by zero1David Brown
25 Apr 25 +- Re: integer divided by zero1John McCue
26 Apr 25 +- Re: integer divided by zero1Waldek Hebisch
27 Apr 25 +* Re: integer divided by zero24Bonita Montero
27 Apr 25 i+* Re: integer divided by zero11Bonita Montero
27 Apr 25 ii`* Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero)10Kenny McCormack
27 Apr 25 ii `* Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero)9Bonita Montero
27 Apr 25 ii  +* Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero)3Kaz Kylheku
27 Apr 25 ii  i+- Re: Did you get confused again? You seem eaily bewildered. (Was: integer divided by zero)1Bonita Montero
28 Apr 25 ii  i`- Re: Did you get confused again? You seem eaily bewildered.1Waldek Hebisch
28 Apr 25 ii  +* Re: Did you get confused again? You seem eaily bewildered.3Waldek Hebisch
30 Apr 25 ii  i+- Re: Did you get confused again? You seem eaily bewildered.1Rosario19
30 Apr 25 ii  i`- Re: Did you get confused again? You seem eaily bewildered.1Richard Heathfield
28 Apr 25 ii  `* Re: Did you get confused again? You seem eaily bewildered.2Waldek Hebisch
28 Apr 25 ii   `- Re: Did you get confused again? You seem eaily bewildered.1Bonita Montero
28 Apr 25 i+* Re: integer divided by zero4Richard Heathfield
28 Apr 25 ii`* Re: integer divided by zero3Bonita Montero
28 Apr 25 ii `* Re: integer divided by zero2Richard Heathfield
28 Apr 25 ii  `- Re: integer divided by zero1Bonita Montero
28 Apr 25 i`* Re: integer divided by zero8Richard Damon
28 Apr 25 i +* Re: integer divided by zero5Bonita Montero
28 Apr 25 i i+- Re: integer divided by zero1Bonita Montero
28 Apr 25 i i`* Re: integer divided by zero3Michael S
28 Apr 25 i i +- Re: integer divided by zero1Bonita Montero
28 Apr 25 i i `- Re: integer divided by zero1Muttley
28 Apr 25 i `* Re: integer divided by zero2Waldek Hebisch
28 Apr 25 i  `- Re: integer divided by zero1Bonita Montero
28 Apr 25 +* Re: integer divided by zero2Richard Heathfield
28 Apr 25 i`- Re: integer divided by zero1Keith Thompson
3 May 25 `* Re: integer divided by zero2Vir Campestris
3 May 25  `- Re: integer divided by zero1Michael S

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal