---------------------------------------- > From: steve+comp.lang.pyt...@pearwood.info > Subject: Re: Short-circuit Logic > Date: Fri, 31 May 2013 05:13:51 +0000 > To: python-list@python.org > > On Fri, 31 May 2013 00:03:13 +0300, Carlos Nepomuceno wrote: > >> ---------------------------------------- >>> From: steve+comp.lang.pyt...@pearwood.info Subject: Re: Short-circuit >>> Logic >>> Date: Thu, 30 May 2013 05:42:17 +0000 To: python-list@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'll requote a previous message: }On Thu, 30 May 2013 13:45:13 +1000, Chris Angelico wrote: } }> Let's suppose someone is told to compare floating point numbers by }> seeing if the absolute value of the difference is less than some }> epsilon. } }Which is usually the wrong way to do it! Normally one would prefer }*relative* error, not absolute: 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. > 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) > > String concatenation: > > "Hello" + "World" != "World" + "Hello" > > Many operations in the real world: > > put on socks, then shoes != put on shoes, then socks. > That's is totally irrelevant in this case. The commutative law is essential to the equality operation. > But you are correct that approximately-equal using *relative* error is > not commutative. (Absolute error, on the other hand, is commutative.) As > I said, any form of "approximate equality" has gotchas. But this gotcha > is simple to overcome: > > abs(a -b) < eps*max(abs(a), abs(b)) > > (Knuth's "approximately equal to" which you give.) > > >> This discussion reminded me of TAOCP and I paid a visit and bring the >> following functions: > > "TAOCP"? The Art of Computer Programming[1]! An old book full of excellent stuff! A MUST READ!!!! ;) http://www-cs-faculty.stanford.edu/~uno/taocp.html [1] Knuth, Donald (1981). The Art of Computer Programming. 2nd ed. Vol. 2. p. 218. Addison-Wesley. ISBN 0-201-03822-6. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list