Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to avoid an integer overflow?

3 views
Skip to first unread message

Bonita Montero

unread,
May 7, 2021, 5:11:26 AM5/7/21
to
>>     bool     signChange = ((left + right) ^ left) >> 31;

> This is presumably missing the casts to uint32_t. As it stands, you
> have a recursive loop.

No, the values should remain signeds to result -1 or 0,
converted to truw or fase.

>>     bool     carry      = (uint32_t)left + (uint32_t)right <
>> (uint32_t)left;
>>     bool     overflow   = signChange & !carry;
>>     if( overflow )
>>         throw range_error( "bint32_t add-overflow" );
>>     return left.m_value + right.m_value;
>> }
>
> The class you gave earlier was buint32_t, with the underlying type being
> unsigned (uint32_t). Now you have switched to bint32_t, but it is not
> clear if your underlying arithmetic is done using signed or unsigned types.

I just wanted to give an exampe how a oveflow-test would look like
for a signed version of my class.

0 new messages