Deutsch English Français Italiano |
<v8k21v$33nca$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Thiago Adams <thiago.adams@gmail.com> Newsgroups: comp.lang.c Subject: Re: What is your opinion about unsigned int u = -2 ? Date: Fri, 2 Aug 2024 22:44:31 -0300 Organization: A noiseless patient Spider Lines: 32 Message-ID: <v8k21v$33nca$1@dont-email.me> References: <v8dfo9$1k7cg$1@dont-email.me> <pan$d2c8a$8c54ac9f$29a202e0$12c6ce86@invalid.invalid> <87bk2cecan.fsf@bsb.me.uk> <v8inds$2qpqh$1@dont-email.me> <v8iqnr$7l3c$1@news.xmission.com> <v8irje$2rolg$1@dont-email.me> <87r0b6g3qx.fsf@nosuchdomain.example.com> <v8jbj5$2us0r$4@dont-email.me> <v8jvln$33atp$1@dont-email.me> <87h6c2fldh.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 03 Aug 2024 03:44:32 +0200 (CEST) Injection-Info: dont-email.me; posting-host="6ca7cab00778520cac37785ddd7f8d1f"; logging-data="3267978"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+N6JxKloXAzvRRGpl1FIiXJdoducoKRm4=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:o4pfdqAOHfUBhLxag8BtbNLgVJ4= Content-Language: en-GB In-Reply-To: <87h6c2fldh.fsf@nosuchdomain.example.com> Bytes: 2241 Em 8/2/2024 10:25 PM, Keith Thompson escreveu: > Thiago Adams <thiago.adams@gmail.com> writes: > [...] >> I am doing experiments with constexpr. What happens with overflow in >> compile time? The answer is not so clear. Sometimes it does not >> compile and sometimes it works as in runtime. > > Your first example fails due to signed integer overflow. Your second > example is well defined because there is no such thing as unsigned > integer overflow. I need a new name other than overflow. The expression ULLONG_MAX*ULLONG_MAX/ULLONG_MAX/ULLONG_MAX has the result 1 if calculated with infinite precision. But the calculated value here is 0 #include <stdio.h> int main() { constexpr unsigned long long ULLONG_MAX = 18446744073709551615; constexpr unsigned long long r = ULLONG_MAX*ULLONG_MAX/ULLONG_MAX/ULLONG_MAX; printf("%llu", r); }