X-Received: by 10.224.59.205 with SMTP id m13mr5714182qah.7.1369969898934; Thu, 30 May 2013 20:11:38 -0700 (PDT) X-Received: by 10.50.29.38 with SMTP id g6mr211896igh.11.1369969898869; Thu, 30 May 2013 20:11:38 -0700 (PDT) Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!ch1no36592qab.0!news-out.google.com!y6ni17qax.0!nntp.google.com!ch1no36589qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.python Date: Thu, 30 May 2013 20:11:38 -0700 (PDT) In-Reply-To: <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=123.192.32.215; posting-account=5JdMBQoAAABHnS4mjpqEzxnmWtgiiVNw NNTP-Posting-Host: 123.192.32.215 References: <5f101d70-e51f-4531-9153-c92ee2486fd9@googlegroups.com> <51a1fc7b$0$30002$c3e8da3$5496439d@news.astraweb.com> <2abf4e9c-8c3b-4e2f-80c9-50c1f1d75c9d@googlegroups.com> <51a4b5a1$0$29966$c3e8da3$5496439d@news.astraweb.com> <04b90c02-833a-4bad-88ad-ab71178b8f79@googlegroups.com> <48519aa0-d0cd-4ffc-a2f5-2107465321d8@qn4g2000pbc.googlegroups.com> <51a6b969$0$29966$c3e8da3$5496439d@news.astraweb.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <987fed2a-e7c7-4bcc-8aaf-9608e21eff26@googlegroups.com> Subject: Re: Short-circuit Logic From: 88888 Dihedral Injection-Date: Fri, 31 May 2013 03:11:38 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 5052 Xref: csiph.com comp.lang.python:46570 Steven D'Aprano=E6=96=BC 2013=E5=B9=B45=E6=9C=8830=E6=97=A5=E6=98=9F=E6=9C= =9F=E5=9B=9BUTC+8=E4=B8=8A=E5=8D=8810=E6=99=8228=E5=88=8657=E7=A7=92=E5=AF= =AB=E9=81=93=EF=BC=9A > On Wed, 29 May 2013 10:50:47 -0600, Ian Kelly wrote: >=20 >=20 >=20 > > On Wed, May 29, 2013 at 8:33 AM, rusi wrote: >=20 > >> 0.0 =3D=3D 0.0 implies 5.4 =3D=3D 5.4 >=20 > >> is not a true statement is what (I think) Steven is saying. 0 (or if >=20 > >> you prefer 0.0) is special and is treated specially. >=20 > >=20 >=20 > > It has nothing to do with 0 being special. A floating point number wil= l >=20 > > always equal itself (except for nan, which is even more special), and i= n >=20 > > particular 5.4 =3D=3D 5.4. But if you have two different calculations = that >=20 > > produce 0, or two different calculations that produce 5.4, you might >=20 > > actually get two different numbers that approximate 0 or 5.4 thanks to >=20 > > rounding error. If you then compare those two ever-so-slightly >=20 > > different numbers, you will find them unequal. >=20 >=20 >=20 > EXACTLY! >=20 >=20 >=20 > The problem does not lie with the *equality operator*, it lies with the= =20 >=20 > calculations. And that is an intractable problem -- in general, floating= =20 >=20 > point is *hard*. So the problem occurs when we start with a perfectly=20 >=20 > good statement of the facts: >=20 >=20 >=20 > "If you naively test the results of a calculation for equality without=20 >=20 > understanding what you are doing, you will often get surprising results" >=20 >=20 >=20 > which then turns into a general heuristic that is often, but not always,= =20 >=20 > reasonable: >=20 >=20 >=20 > "In general, you should test for floating point *approximate* equality,= =20 >=20 > in some appropriate sense, rather than exact equality" >=20 >=20 >=20 > which then gets mangled to: >=20 >=20 >=20 > "Never test floating point numbers for equality" >=20 >=20 >=20 > and then implemented badly by people who have no clue what they are doing= =20 >=20 > and have misunderstood the nature of the problem, leading to either: >=20 >=20 >=20 > * de facto exact equality testing, only slower and with the *illusion* of= =20 >=20 > avoiding equality, e.g. "abs(x-y) < sys.float_info.epsilon" is just a=20 >=20 > long and slow way of saying "x =3D=3D y" when both numbers are sufficient= ly=20 >=20 > large; >=20 >=20 >=20 > * incorrectly accepting non-equal numbers as "equal" just because they=20 >=20 > happen to be "close". >=20 >=20 >=20 >=20 >=20 > The problem is that there is *no one right answer*, except "have everyone= =20 >=20 > become an expert in floating point, then judge every case on its merits",= =20 >=20 > which will never happen. >=20 >=20 >=20 > But if nothing else, I wish that we can get past the rank superstition=20 >=20 > that you should "never" test floats for equality. That would be a step=20 >=20 > forward. >=20 >=20 >=20 >=20 >=20 >=20 >=20 > --=20 >=20 > Steven The string used to represent a floating number in a computer language is normally in the decimal base of very=20 some limited digits. Anyway with the advances of A/D-converters in the past 10 years which are reflected in the anttena- transmitter parts in phones,=20 the long integer part in Python can really beat the low cost=20 32- 64 bit floating computations in scientific calculations.