| Deutsch English Français Italiano |
|
<vuo1un$3f0b9$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!weretis.net!feeder9.news.weretis.net!news.quux.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Thiago Adams <thiago.adams@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: integer divided by zero
Date: Mon, 28 Apr 2025 11:05:11 -0300
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <vuo1un$3f0b9$1@dont-email.me>
References: <vughb7$g6cm$1@dont-email.me>
<87selwoydy.fsf@nosuchdomain.example.com> <vugo19$m2n2$1@dont-email.me>
<87o6wkouea.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 28 Apr 2025 16:05:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="927eb2a6bd76ce55eaf6ab90fb8993f1";
logging-data="3637609"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18IaESWLxys/E05yqk63fRlHTAhztLo8zM="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:z6Pinw5Cr0Pxja9Nr86Xf0Xw9u0=
In-Reply-To: <87o6wkouea.fsf@nosuchdomain.example.com>
Content-Language: en-US
Bytes: 3108
On 25/04/2025 17:31, Keith Thompson 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.
>
>> (But what old c89 compilers where doing in this case?)
>>
>> This sample is a motivation to make integer divided by
>> zero a constrain.
>
> I consider it motivation not to write code like that.
>
> Sure, I wouldn't mind if using / or % with a right operand
> that's a constant expression with value zero (either integer or
> floating-point) were a constraint violation, but some compilers
> are going to warn about it anyway, and I doubt that such a language
> change would catch many errors.
>
Another interesting example
enum {A = ((-2147483647-1) / -1)};
In this case it is a constant expression.
The resulting value is -2147483648 of type int.
The mathematically correct value is 2147483648 that does not fit
in a integer.