Wrong result with enum

Patrick Schluter Patrick.Schluter at bbox.fr
Thu Nov 11 13:04:45 UTC 2021


On Thursday, 11 November 2021 at 12:05:19 UTC, Tejas wrote:
> On Thursday, 11 November 2021 at 09:11:37 UTC, Salih Dincer 
> wrote:
>> On Thursday, 11 November 2021 at 06:34:16 UTC, Stanislav 
>> Blinov wrote:
>>> On Thursday, 11 November 2021 at 05:37:05 UTC, Salih Dincer 
>>> wrote:
>>>> is this a issue, do you need to case?
>>>>
>>>> ```d
>>>> enum tLimit = 10_000;  // (1) true result
>>>> enum wLimit = 100_000; // (2) wrong result
>>>> ```
>>>
>>> https://dlang.org/spec/enum.html#named_enums
>>>
>>> Unless explicitly set, default type is int. 10000100000 is 
>>> greater than int.max.
>> 100001
>> ```d
>>   enum w = 100_000;
>>   size_t b = w * w;
>>   // size_t b = 100000 * 100000; // ???
>>   assert(b == 10_000_000_000); // Assert Failure
>> ```
>> The w!(int) is not greater than the b!(size_t)...
>
> Are you on 32-bit OS? I believe `size_t` is 32 bits on 32 bit 
> OS and 64 on a 64-bit OS

That's not the issue with his code. The 32 bit overflow happens 
already during the `w * w` mulitplication. The wrong result is 
then assigned to the `size_t`.

`cast(size_t)w * w` or the declaration `enum  : size_t { w = 
100_000 };` would change that.




More information about the Digitalmars-d-learn mailing list