Best search algorithm to find condition within a range
Alain Ketterlin
alain at dpt-info.u-strasbg.fr
Thu Apr 9 10:53:19 EDT 2015
Chris Angelico <rosuav at gmail.com> writes:
> On Thu, Apr 9, 2015 at 11:57 PM, Alain Ketterlin
> <alain at dpt-info.u-strasbg.fr> wrote:
>> Because, in:
>>
>> z = x+y; // all signed ints
>> if ( z < x )
>> ...
>>
>> either there was no overflow (and the condition is false), or there was,
>> and the result is undefined, i.e., "z<x" can be considered false also.
>
> Do you mean "all unsigned ints" here? Because if y is negative, the
> condition will be true without overflow. If you didn't, then I'm
> puzzled as to where the undefined behaviour is coming from.
Ouch, you're right, I tried to stick with Marko's example and forgot the
basics. I meant "signed ints", but the "removable" condition should be
something like:
if ( x>0 && y>0 && z<x )
...
i.e., some condition that is never true (either false or undefined).
Thanks for the correction.
If the variables are all unsigned ints, the potential overflow has a
well-defined meaning, and the code is correct and doesn't trigger
undefined behavior. This was Marko's initial example.
-- Alain.
More information about the Python-list
mailing list