Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'context': 0.07; 'correct.': 0.07; 'think,': 0.07; 'string': 0.09; '0.1': 0.09; 'ascii': 0.09; 'deemed': 0.09; "they've": 0.09; '(these': 0.16; 'appreciated!': 0.16; 'columns': 0.16; 'elsewhere.': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'given,': 0.16; 'rounding': 0.16; 'subject:String': 0.16; 'thread,': 0.16; 'typos': 0.16; 'to:name :python-list@python.org': 0.22; 'necessary.': 0.24; 'fairly': 0.24; 'fine': 0.24; 'possibly': 0.26; 'this:': 0.26; 'values': 0.27; '(this': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'easier': 0.31; 'too.': 0.31; 'calculated': 0.31; 'decimal': 0.31; 'factor': 0.31; 'subject:numbers': 0.31; 'such.': 0.31; 'probably': 0.32; 'another': 0.32; 'not.': 0.33; 'third': 0.33; 'actual': 0.34; 'maybe': 0.34; 'advice': 0.35; '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; 'data,': 0.36; 'should': 0.36; 'two': 0.37; 'performance': 0.37; 'to:addr:python-list': 0.38; 'files': 0.38; 'that,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'catch': 0.60; 'full': 0.61; 'numbers': 0.61; 'strictly': 0.61; 'first': 0.61; 'times': 0.62; 'more': 0.64; 'chance': 0.65; 'within': 0.65; 'techniques': 0.66; 'here': 0.66; '2-3': 0.68; '3-4': 0.68; 'therefore': 0.72; 'obvious': 0.74; '"how': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=aiQf3x00sTOOaxKatMenyaBUFus2eHzYpJpfcCSP5JE=; b=xXvYUYvtkkzifQap+MXS+HNjbM7EM6zB+X1GL42Vja4WDd6Kc6+/ZVKMFr13/aqctp ET3/f5upjC1m6UPQpbxhVdW18C1cVm06efJHFqA8V0me6o5To9rPKuh+uhConvqe49vR opWfPhAvwAwdsuJH1D7450pLePW53YiVg8PFLi11te/kdpC61Ljpv+BvTrsnM/l8BDj5 PgXreZiF6EcgHCw/hhyqIiFxTKNvDiGf4kIwtRaFAF0/qufmLTO7IartTU8K5M1rhT4T 5JBeOLPBbbtxH8HCzWyzmrp596EzVaUt8Xw0qvR9MPqlxXyHzIr3D7jdGZFsMAJyVfX7 q+FQ== MIME-Version: 1.0 X-Received: by 10.50.117.65 with SMTP id kc1mr14783457igb.34.1411577652225; Wed, 24 Sep 2014 09:54:12 -0700 (PDT) Date: Thu, 25 Sep 2014 02:54:12 +1000 Subject: String representations of numbers and approximate equality From: Chris Angelico To: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411577654 news.xs4all.nl 2857 [2001:888:2000:d::a6]:50053 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78300 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. This is a san-check for human-entered data, so sloppiness is necessary. I just want to have a chance to catch obvious typos and such. In theory, I think, a decimal.Decimal context should be able to do what I want here - but I'm not particularly familiar with it. Performance isn't critical, so stupid techniques that work on the actual string representation would be fine too. In general, the numbers will be fairly small. Maybe 3-4 digits before the decimal point, and probably not more than 2-3 after. Not sure if that makes things any easier or not. All advice appreciated! ChrisA