Correctness bug in TDPL

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Fri May 13 14:43:50 PDT 2011


On 5/13/11 1:01 PM, Timon Gehr wrote:
>> On 5/13/11 3:25 AM, Timon Gehr wrote:
>>> On p368 the CheckedInt struct does not check for overflow in the unary minus
>>> operator.
>>
>> Unary minus never overflows. That being said, there is the oddity that
>> -x is x when x == int.min. Even in that case there is no loss of
>> information.
>>
>> Andrei
>
> This behavior is caused by _overflow_ when the error condition that is checked in
> ++ is overflow:
>
> auto x=CheckedInt(int.min);
> x=-x; //passes
>
> x=~x;
> x++;//throws

Not sure I understand the point here. I do agree that this may be 
confusing and also that it's reasonable to check against int.min in 
unary minus.

> Also, the statement that there is no loss of information is just wrong:
>
> scanf("%d %d %d",&n_,&m_);
> auto n=CheckedInt!int(n_),m=CheckedInt!int(m_);
> enforce(n>0&&  m<0, "provide meaningful input!");
> foreach(i;0..n) m=-m;
> writeln(n," is"~(m<0?"odd":"even")); //disaster strikes!

Depends on how one defines "information". I meant it simply as state 
available to the program.


Andrei


More information about the Digitalmars-d mailing list