Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'exit': 0.07; 'implements': 0.07; 'raises': 0.07; 'python': 0.09; 'calculating': 0.09; 'integers': 0.09; 'integral': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rounding': 0.09; 'zero.': 0.09; 'subject:python': 0.11; 'sat,': 0.15; "(i'm": 0.16; '1234': 0.16; 'comparison.': 0.16; 'equality.': 0.16; 'fine.': 0.16; 'range,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'two,': 0.16; 'accepting': 0.18; 'feb': 0.19; 'equivalent': 0.20; 'large,': 0.22; 'subject:problem': 0.22; 'absolute': 0.23; 'mention': 0.23; 'testing': 0.24; 'values': 0.26; 'implemented': 0.27; 'small,': 0.27; 'header:X-Complaints-To:1': 0.28; 'comparison': 0.29; "d'aprano": 0.29; 'division': 0.29; 'equality': 0.29; 'steven': 0.29; 'trigger': 0.29; "i'm": 0.29; 'relative': 0.30; 'point': 0.31; 'implement': 0.32; 'url:home': 0.33; 'zero': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'typically': 0.33; 'needed': 0.35; 'especially': 0.35; 'there': 0.35; 'received:org': 0.36; 'compare': 0.36; 'test': 0.36; 'should': 0.36; 'too': 0.36; 'charset:us-ascii': 0.36; 'problems': 0.36; 'correctly': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'short': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'range': 0.60; 'evaluate': 0.62; 'close': 0.63; 'skip:n 10': 0.63; 'therefore': 0.65; 'risk': 0.66; 'applying': 0.69; 'analysis': 0.70; 'college': 0.71; 'million': 0.72; 'introduce': 0.80; '2013': 0.84; 'compare:': 0.84; 'excessive': 0.84; 'fine...': 0.84; 'worms': 0.84; '"how': 0.91; 'dennis': 0.91; 'involved.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Dennis Lee Bieber Subject: Re: python math problem Date: Sat, 16 Feb 2013 13:56:42 -0500 Organization: > Bestiaria Support Staff < References: <511f0fc9$0$29967$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: adsl-76-253-102-210.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 3.3/32.846 X-No-Archive: YES 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: 240 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361041010 news.xs4all.nl 6891 [2001:888:2000:d::a6]:49248 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39009 On Sat, 16 Feb 2013 15:49:12 +1100, Steven D'Aprano declaimed the following in gmane.comp.python.general: > Consider: > > - Python floating point integers are exact for entire range of -2**53 > to 2**53, or about -9 million million to +9 million million; if you > are working with floats that have integral values in this range, > testing for equality is perfectly fine. > > - If you work exclusively with fractional powers of two, such as 1/2, > 1/4, 1/8, 1/16, etc. floats are typically exact. > > - Testing against an epsilon raises as many problems as it solves: > > + What epsilon should I pick? How do I know if my epsilon is too small, > and therefore I'm rejecting values that I should accept, or too large, > and so I'm accepting values I should reject? > > + If my epsilon is too small, calculating "abs(x - y) <= epsilon" is > exactly equivalent to "x == y", only slower. > > + Should I test for absolute error, or relative error? > > + If relative error, how do I deal with values around zero where > division is likely to introduce excessive rounding error? > > + Not to mention the risk of dividing by zero. > > - And how do I deal with INFs? Fine... The take-away then becomes: One must know how floating point is implemented in the computer in use (granted, practically everything is now using IEEE specifications vs my college mainframe with its radix-16 format); and one must be cognizant of their problem domain to analyze when guards must be taken for comparison of equality. Since my "real world" experience has been in applications which are not integral or powers-of-two number-crunching then an epsilon comparison is pretty much a requirement -- especially when transcendental functions are involved. Yes, one has to then evaluate the problem domain to determine "how close is close enough". The recommendation to always use an epsilon comparison for floating point equality is a short phrase, and should trigger the needed analysis to determine what epsilon is suitable for that comparison. Or should Python implement REXX's NUMERIC statement? There is a can of worms (I'm not even sure Regina REXX implements it correctly -- unless it is rounding to "digits" before applying "fuzz") /* */ do D = 3 to 6 numeric digits D do F = 0 to 3 if D <> F then do numeric fuzz F call compare end end end exit compare: say "Digits:" digits() "Fuzz:" fuzz() say say '12345 = 12346 ' (12345 = 12346) say '12345 = 12356 ' (12345 = 12356) say '12345 = 12335 ' (12345 = 12335) say '1234 = 1235 ' (1234 = 1235) say '123.45 = 123.46 ' (123.45 = 123.46) say '123.45 = 123.56 ' (123.45 = 123.56) say say E:\UserData\Wulfraed\My Documents>regina test.rx Digits: 3 Fuzz: 0 12345 = 12346 1 12345 = 12356 0 12345 = 12335 1 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 3 Fuzz: 1 12345 = 12346 1 12345 = 12356 1 12345 = 12335 1 1234 = 1235 1 123.45 = 123.46 1 123.45 = 123.56 1 Digits: 3 Fuzz: 2 12345 = 12346 1 12345 = 12356 1 12345 = 12335 1 1234 = 1235 1 123.45 = 123.46 1 123.45 = 123.56 1 Digits: 4 Fuzz: 0 12345 = 12346 1 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 4 Fuzz: 1 12345 = 12346 1 12345 = 12356 0 12345 = 12335 1 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 4 Fuzz: 2 12345 = 12346 1 12345 = 12356 1 12345 = 12335 1 1234 = 1235 1 123.45 = 123.46 1 123.45 = 123.56 1 Digits: 4 Fuzz: 3 12345 = 12346 1 12345 = 12356 1 12345 = 12335 1 1234 = 1235 1 123.45 = 123.46 1 123.45 = 123.56 1 Digits: 5 Fuzz: 0 12345 = 12346 0 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 0 123.45 = 123.56 0 Digits: 5 Fuzz: 1 12345 = 12346 1 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 5 Fuzz: 2 12345 = 12346 1 12345 = 12356 0 12345 = 12335 1 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 5 Fuzz: 3 12345 = 12346 1 12345 = 12356 1 12345 = 12335 1 1234 = 1235 1 123.45 = 123.46 1 123.45 = 123.56 1 Digits: 6 Fuzz: 0 12345 = 12346 0 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 0 123.45 = 123.56 0 Digits: 6 Fuzz: 1 12345 = 12346 0 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 0 123.45 = 123.56 0 Digits: 6 Fuzz: 2 12345 = 12346 1 12345 = 12356 0 12345 = 12335 0 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 Digits: 6 Fuzz: 3 12345 = 12346 1 12345 = 12356 0 12345 = 12335 1 1234 = 1235 0 123.45 = 123.46 1 123.45 = 123.56 0 E:\UserData\Wulfraed\My Documents> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/