Short-circuit Logic

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri May 31 04:11:30 EDT 2013


On Fri, 31 May 2013 09:42:38 +0300, Carlos Nepomuceno wrote:

>> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit
>> Logic
>> Date: Fri, 31 May 2013 05:13:51 +0000 To: python-list at python.org
>>
>> On Fri, 31 May 2013 00:03:13 +0300, Carlos Nepomuceno wrote:
>>>> From: steve+comp.lang.python at pearwood.info Subject: Re: Short-circuit
>>>> Logic
>>>> Date: Thu, 30 May 2013 05:42:17 +0000 To: python-list at python.org
>>> [...]
>>>> Here's another way, mathematically equivalent (although not
>>>> necessarily equivalent using floating point computations!) which
>>>> avoids the divide-by- zero problem:
>>>>
>>>> abs(a - b) < epsilon*a
>>>
>>> That's wrong! If abs(a) < abs(a-b)/epsilon you will break the
>>> commutative law. For example:
>>
>> What makes you think that the commutative law is relevant here?
> 
> How can't you see?

I can ask the same thing about you. How can you see that it is not 
relevant?


> I'll requote a previous message:

Thanks, but that's entirely irrelevant. It says nothing about the 
commutative law.

[...]
> Since we are considering Chris's supposition ("to compare floating point
> numbers") it's totally relevant to understand how that operation can be
> correctly implemented.

Of course! But what does that have to do with the commutative law?


>> Many things break the commutative law, starting with division and
>> subtraction:
>>
>> 20 - 10 != 10 - 20
>>
>> 1/2 != 2/1
>>
>> Most comparison operators other than equality and inequality:
>>
>> (23 < 42) != (42 < 23)
[...]
> That's is totally irrelevant in this case. The commutative law is
> essential to the equality operation.

That's fine, but we're not talking about equality, we're talking about 
*approximately equality* or *almost equal*. Given the simple definition 
of relative error under discussion, the commutative law does not hold. 
The mere fact that it does not hold is no big deal. It doesn't hold for 
many comparison operators.

Nor does the transitive law hold, even using absolute epsilon:

eps = 0.5
a = 1.1
b = 1.5
c = 1.9

then a ≈ b, and b ≈ c, but a ≉ c.


-- 
Steven



More information about the Python-list mailing list