Path: csiph.com!1.us.feeder.erje.net!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!bcyclone05.am1.xlned.com!bcyclone05.am1.xlned.com!newsreader4.netcologne.de!news.netcologne.de!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Antoon Pardon Newsgroups: comp.lang.python Subject: Re: Floating point equality [was Re: What exactly is "exact" (was Clean Singleton Docstrings)] Date: Wed, 20 Jul 2016 09:09:29 +0200 Lines: 35 Message-ID: References: <7046c74e-e5ea-4dde-8847-8c556756a563@googlegroups.com> <5789B601.9000604@stoneleaf.us> <9d2a0934-bf26-42a8-9bb2-e4e75b2d2ad3@googlegroups.com> <1468652038.1427719.667878897.460955C0@webmail.messagingengine.com> <578a027c$0$1616$c3e8da3$5496439d@news.astraweb.com> <69d8d886-c18a-4c05-8bca-9a05afacd9a9@googlegroups.com> <578ca188$0$1505$c3e8da3$5496439d@news.astraweb.com> <877fcjcllf.fsf@elektro.pacujo.net> <578da1b5$0$1591$c3e8da3$5496439d@news.astraweb.com> <578f0f5c$0$11115$c3e8da3@news.astraweb.com> <578F23A9.7070105@rece.vub.ac.be> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de aEK5pwqyDI6nMc5mTy60CgDHYM05tNai2oqtiJQDN8xw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.024 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'received:134': 0.05; '"but': 0.09; '1.0)': 0.09; 'none.': 0.09; 'rounding': 0.09; 'appropriate': 0.14; 'subject: \n ': 0.15; 'fuzzy': 0.16; 'integers.': 0.16; 'received:ac.be': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Singleton': 0.16; 'too?': 0.16; 'integer': 0.18; 'arguments': 0.22; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'example': 0.26; 'error': 0.27; '(e.g.': 0.27; '(such': 0.27; '2.0': 0.27; 'values': 0.28; 'arithmetic': 0.29; 'equality': 0.29; 'subject: [': 0.29; 'programmers': 0.30; 'received:be': 0.30; 'waste': 0.30; 'operations': 0.31; "can't": 0.32; 'point': 0.33; 'steven': 0.33; 'fail': 0.35; 'mix': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'doing': 0.38; 'why': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'forget': 0.60; 'your': 0.60; 'no.': 0.62; 'limit': 0.65; 'results': 0.66; 'carefully': 0.72; 'calculations': 0.84; "d'aprano:": 0.84; 'maths': 0.84; 'schreef': 0.84; 'spite': 0.84; 'do:': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: ArIIAHYeUleGuA9G/2dsb2JhbABehFsBvRCGEgKCAAEBAQEBAWaEbQEBAwEjVQYLCxoCBRYLAgIJAwIBAgFFEwgCiCMIsU2NPoNbAQEIAiWBAYUmhE2EHW+CNYI9HAEEmEWBV4oRgjyCAIczhWmPUlSCOYE3iTuBQwEBAQ User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.8.0 In-Reply-To: <578f0f5c$0$11115$c3e8da3@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <578F23A9.7070105@rece.vub.ac.be> X-Mailman-Original-References: <7046c74e-e5ea-4dde-8847-8c556756a563@googlegroups.com> <5789B601.9000604@stoneleaf.us> <9d2a0934-bf26-42a8-9bb2-e4e75b2d2ad3@googlegroups.com> <1468652038.1427719.667878897.460955C0@webmail.messagingengine.com> <578a027c$0$1616$c3e8da3$5496439d@news.astraweb.com> <69d8d886-c18a-4c05-8bca-9a05afacd9a9@googlegroups.com> <578ca188$0$1505$c3e8da3$5496439d@news.astraweb.com> <877fcjcllf.fsf@elektro.pacujo.net> <578da1b5$0$1591$c3e8da3$5496439d@news.astraweb.com> <578f0f5c$0$11115$c3e8da3@news.astraweb.com> X-Received-Bytes: 6502 X-Received-Body-CRC: 3926477562 Xref: csiph.com comp.lang.python:111661 Op 20-07-16 om 07:42 schreef Steven D'Aprano: > Floating point maths is hard, thinking carefully about what you are doing and > whether it is appropriate to use == or a fuzzy almost-equal comparison, or if > equality is the right way at all. > > "But thinking is hard, can't you just tell me the answer?" > > No. But I can give some guidelines: > > Floating point arithmetic is deterministic, it doesn't just randomly mix in > error out of spite or malice. So in principle, you can always estimate the > rounding error from any calculation -- and sometimes there is none. I would like to see a practical example of such an outcome. > Arithmetic on integer-values (e.g. 1.0) is always exact, up to a limit of > either 2**53 or approximately 1e53, I forget which. (That's why most Javascript > programmers fail to notice that they don't have an integer type.) So long as > you're using operations that only produce integer values from integer arguments > (such as + - * // but not / ) then all calculations are exact. It is a waste of > time to do: > > x = 2.0 > y = x*1002.0 > is_equal(y, 2004.0, 1e-16) > > when you can just do y == 2004.0. But why perforem integer arithmetics in floats, isn't that a waste of time too? I really see no reason to use floats if you know all your results will be integers. -- Antoon.