Path: csiph.com!feeder.erje.net!2.eu.feeder.erje.net!newsfeed.kamp.net!newsfeed.kamp.net!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: What exactly is "exact" (was Clean Singleton Docstrings) Date: Mon, 18 Jul 2016 20:37:23 +1000 Lines: 50 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> <402d8e8a-6acf-44e8-99e2-fa09da10d7bc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de KQaYNVx200SljXcobu2qawh9hGBbojQhFwo0eqS9JeLQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.017 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'cc:addr:python-list': 0.09; 'python': 0.10; 'def': 0.13; "hasn't": 0.15; 'intermediate': 0.15; 'properly': 0.15; '2016': 0.16; 'binary,': 0.16; 'binary.': 0.16; 'decimal.': 0.16; 'equal.': 0.16; 'false:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Singleton': 0.16; 'wrote:': 0.16; 'looked': 0.16; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; "aren't": 0.22; 'trying': 0.22; 'accuracy': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; '2.0': 0.27; 'values': 0.28; 'skip:( 20': 0.28; 'equality': 0.29; 'sure,': 0.29; 'code': 0.30; 'getting': 0.33; 'point': 0.33; 'source': 0.33; 'adjust': 0.33; 'received:google.com': 0.35; 'false': 0.35; 'functions.': 0.35; 'something': 0.35; 'problem.': 0.35; 'but': 0.36; 'should': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'why': 0.39; 'some': 0.40; 'your': 0.60; 'course': 0.62; 'more': 0.63; 'dont': 0.64; 'here': 0.66; 'sum': 0.69; 'jul': 0.72; 'obvious': 0.76; '*really*': 0.84; '5.3.1': 0.84; 'chrisa': 0.84; "it'd": 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:cc; bh=M3jje7RQrIhvKjySYgv3Wy3kL5+0uWZlOU67OMEUhoA=; b=ZOQEhuCLCCAbegiju0jNLzqzTEbYXwGov7g/DYkjnpfcVQPO5E/NwfHcqGZpL/6HTt npLCSsU44i9cmBWiQCegM4wEzPRL8/a6fO6Ayaw5cWjL0lcTg1Lb4hgNktF44MMJjE0K Y4RlqDtJadmrTYAJrVhfOISH1Kn1j5Xgv5gK+ZpotwkIC9sLX/dT6waezSnSPoVgbGSu lHXUpWog0r+x/gGKf7xeASHINGZT7MSf4p/Hs2t0utgq/1p+Pbw5sO3kpKNpoQaLX9vU d9EcPF2KqzqkONm/rmT0BL6LKRAV3owZCxgsuCO6kVZuSgcBTynj6qt2BtpwXkoAkdrM J+lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:cc; bh=M3jje7RQrIhvKjySYgv3Wy3kL5+0uWZlOU67OMEUhoA=; b=X4DTdbzvyXkuOK2GssFaHocj7VNugcVMvP2pCV3GRxgvlgJID7qz92QCZTJLFK78Is o7DBem6MQp0+Tah/GgMrIYiUf7CE2fntHA4FNRLlk9g+xJLYTa1fHlSnGsB2RywkCJQi bTmDBoQFF7dQADLE6rQFk7/we+tenCJaecqgD3dOhGuGnhYbBWL5rPhFxL3SB/PudtSM kbqh4DzaxNMQe9LYwyZNs4c1sDV5hDUJYX0gZ55Fz0aGwEIW3OwqKi7ftgeXf+iTZPUm VJ2ZExfZLH2Kche/aWqo/Kl46nZ04k830YY9UbivipE5goT3eaEHSzJcf+y98BDf9Zph 4BPQ== X-Gm-Message-State: ALyK8tJwnHNPukN2vsR04tmVs2pNhy2FatTxCT8k2wBg/SNGsnbgSykKeScytEmZp74oIXy5E62GvTkg6sieng== X-Received: by 10.195.13.34 with SMTP id ev2mr500251wjd.117.1468838244510; Mon, 18 Jul 2016 03:37:24 -0700 (PDT) In-Reply-To: <402d8e8a-6acf-44e8-99e2-fa09da10d7bc@googlegroups.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: 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> <402d8e8a-6acf-44e8-99e2-fa09da10d7bc@googlegroups.com> Xref: csiph.com comp.lang.python:111602 On Mon, Jul 18, 2016 at 8:24 PM, Rustom Mody wrote: > I dont know what point you are trying to make > Here is behavior. Should one use == ?? > > Python 2.7.11+ (default, Apr 17 2016, 14:00:29) > [GCC 5.3.1 20160413] on linux2 > Type "help", "copyright", "credits" or "license" for more information. >>>> .1+.1+.1 == .3 > False > > EPSILON = 0.000001 # Adjust to control numeric accuracy > def is_equal(f1, f2, epsilon=EPSILON): > if abs(f1) > abs(f2): > f1, f2 = f2, f1 > return abs(f2-f1) < f1*epsilon >>>> ... ... ... ... > >>>> is_equal(.1+.1+.1, .3) > True >>>> Sure, simple equality hasn't given you an "intuitive" result - but that's because your source code is in decimal. So you need to properly understand it. If you work with something that can be represented precisely in binary, you have no problems with equality: >>> 7/4 + 9/4 + 11/4 + 13/4 == 10 True It's only the repeating values that have that problem. And if you disassemble your example, it's obvious why you get False: >>> dis.dis(lambda: .1+.1+.1 == .3) 1 0 LOAD_CONST 4 (0.30000000000000004) 2 LOAD_CONST 2 (0.3) 4 COMPARE_OP 2 (==) 6 RETURN_VALUE Of course those two values aren't the same. And it'd be just as obvious if you looked at them in binary. It'd look like this in decimal: 0.6667 + 0.6667 + 0.6667 == 2.0 Well, duh, all those intermediate values are rounded up, so you're getting the sum of the rounding, and of course it's not equal. That's why you need to get some comprehension of floating-point and how it *really* functions. ChrisA