Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'correct.': 0.07; '0.1': 0.09; 'ascii': 0.09; 'deemed': 0.09; "they've": 0.09; 'cc:addr:python-list': 0.11; '(these': 0.16; '24,': 0.16; 'columns': 0.16; 'elsewhere.': 0.16; 'enough.': 0.16; 'given,': 0.16; 'rounding': 0.16; 'subject:String': 0.16; 'thread,': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'possibly': 0.26; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; '(this': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'calculated': 0.31; 'decimal': 0.31; 'factor': 0.31; 'sep': 0.31; 'subject:numbers': 0.31; 'another': 0.32; 'third': 0.33; 'skip:d 20': 0.34; 'knows': 0.35; 'something': 0.35; 'definition': 0.35; 'equal': 0.35; 'point.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accuracy': 0.36; 'should': 0.36; 'two': 0.37; 'files': 0.38; 'that,': 0.38; 'full': 0.61; 'numbers': 0.61; 'strictly': 0.61; 'first': 0.61; 'times': 0.62; 'to:addr:gmail.com': 0.65; 'within': 0.65; 'hesitate': 0.70; 'therefore': 0.72; 'tolerance': 0.84; '"how': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=QovvJmrLnfS1lAJX2YriSysEyBu3BdtLL7wRA+m0X/Y=; b=J1wlXTBR3x1QdWau/pC1dSa2nzUnDWBgEYFUbM9viJ2wMZ0XLrohCtlfEyVVPSeXUJ Dq9ohuvrqR7bKegr0nz9hiPIZFcCZcALwPEC1c5UQ4ANR+OQsVugb1KZF47NRFwgnTZP EcG3jDgOnLfseRO6aGJNTH00aYt8j7XhacD3c27+koOdX8xHifewJxzvuCPgDzRfTduh j5D5LaWqZaPbu20RhtBBTPT0FPvY63TteIDL94hAO9gNP1qzmyQ9FpPtPiVL0t22PCoe 88b7ewKMW0F1YP22rfRqh2WWOV7CIkHz4k5Y1748yvSV4gN5MtGfxEg3vLjGV5O5Vbi3 RsLQ== MIME-Version: 1.0 X-Received: by 10.152.198.204 with SMTP id je12mr9731605lac.52.1411602076662; Wed, 24 Sep 2014 16:41:16 -0700 (PDT) In-Reply-To: References: Date: Wed, 24 Sep 2014 16:41:16 -0700 Subject: Re: String representations of numbers and approximate equality From: Dan Stromberg To: Chris Angelico Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411602084 news.xs4all.nl 2873 [2001:888:2000:d::a6]:41100 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78315 On Wed, Sep 24, 2014 at 9:54 AM, Chris Angelico wrote: > Yes, it's another "how can I see if two numbers are approximately > equal" thread, but this time I have a concrete definition of > "approximately equal"... and they're Decimals, not floats. > > I have a number of files (this is an ongoing thing) in which there are > two columns of numbers. One of them should be equal to the other times > some factor which the program knows from elsewhere. All the numbers > are represented as strings of ASCII decimal digits, possibly including > a U+002E decimal point. Something like this: > > # (these are calculated on factor 30) > ["0.75", "22.5"] > ["0.80", "24"] > ["4.73", "142"] > > The definition of valid is that, within the rounding they've been > given, the values are correct. The first two are strictly correct; the > third would be 141.9 with full accuracy but 142 is deemed good enough. > But this should fail: > > ["0.1", "10"] > > 0.1 * 30 should be 3, not 10; and 10 / 30 should be 0.333, not 0.1; > therefore this is wrong. tolerance = decimal.Decimal('0.1') # I hesitate to call it epsilon if abs(decimal1 - decimal2) <= tolerance: print('They are close') else: print('They are different')