Is -1 > 7 ?

michaelc37 michaelc37 at msn.com
Fri Aug 9 09:07:10 PDT 2013


On Friday, 9 August 2013 at 15:18:47 UTC, Dicebot wrote:
> On Friday, 9 August 2013 at 15:11:42 UTC, michaelc37 wrote:
>> forgive me if i'm doing something stupid, i'm extremely tired 
>> and trying to avoid drinking coffee.
>>
>> void main()
>> {
>> 	int[] arr = [0, 1, 2, 3, 4, 5, 6];
>>
>> 	//check 1
>> 	if (-1 > arr.length)
>> 		writefln("WTF -> %d is greater than %d ????", -1, 
>> arr.length);
>> 	else
>> 		writefln("GOOD -> %d is NOT greater than %d", -1, 
>> arr.length);
>> }
>>
>> here is my output:
>> WTF -> -1 is greater than 7 ????
>
> And signed vs unsigned design issues pops up again! :) 
> *summoning bearophile*
>
> On topic: arr.length has type size_t which is unsigned integer. 
> -1 gets silently casted to unsigned, resulting in size_t.max 
> value (0xFFF..) - which is obviously bigger than actual length.
>
> I believe it should be a compile-time error but, unfortunately, 
> this is unlikely to happen.

Ahhh thanks for the explaination. if this was a compile time 
error, i would have saved precious time; I found strange behavior 
while writing some other related code, and I kept overlooking the 
condition thinking "it cant be that".


More information about the Digitalmars-d-learn mailing list