Sujet : Re: What is your opinion about unsigned int u = -2 ?
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 03. Aug 2024, 02:44:31
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v8k21v$33nca$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Mozilla Thunderbird
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);
}