Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!news2.arglkargh.de!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'line:': 0.07; 'cc:addr :python-list': 0.10; 'def': 0.14; 'alpha,': 0.16; 'b):': 0.16; 'calculates': 0.16; 'cases:': 0.16; 'examples:': 0.16; 'posted,': 0.16; 'skip:1 60': 0.16; 'skip:7 20': 0.16; 'subject:distance': 0.16; 'to:addr:pearwood.info': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.16; 'comparing': 0.18; 'compare': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'cc:no real name:2**0': 0.23; '2015': 0.23; 'header:In-Reply-To:1': 0.24; 'url:edu': 0.24; 'mon,': 0.24; 'skip:f 40': 0.27; 'message- id:@mail.gmail.com': 0.28; 'decimal': 0.29; 'methods.': 0.29; 'print': 0.31; "d'aprano": 0.33; 'steven': 0.33; 'received:google.com': 0.34; 'skip:- 10': 0.34; 'but': 0.36; "let's": 0.36; 'two': 0.37; 'subject:: ': 0.37; '(2)': 0.37; '(1)': 0.38; 'pm,': 0.39; 'where': 0.40; 'bottom': 0.61; 'fun': 0.61; 'subject:more': 0.61; 'more': 0.62; 'email addr:gmail.com': 0.64; 'obvious': 0.72 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=14AvgHebok2qaIRBqSiIgrkwrfY9yerNGligOKVDJz8=; b=M/5SUY5PPuxsHhDnI/5bZD4jdnzqqdBakgNHAezmiF8f6hHMcFDEbAM/s9ywVOOmL5 JhFfcEZYsHsoAQgZ9MBtjm1AZiJbyJYx6w9fKPF6rre3+vvSUsS5J0EwSxv83OcdxwTd ka3cXkOqTNNAlcQHmSbSQc1SY0e40ilZey92FMcHNQVG1HAtK8N3uqiUA6F8HPEcEmDM L9XHFVlsBoOxup+SX0xJN5VbQoSTB112NXtr5v4oGyHmMUSlKy70Ktjw059jvYSoQ1zy HzglYxfBstIASvW5f9Ehojuy86HCnGSQeCI1dDjTCdo4KFpcz4Rs9+n+l98IW6VkTCdd ju0Q== MIME-Version: 1.0 X-Received: by 10.140.99.65 with SMTP id p59mr41947198qge.46.1432749625083; Wed, 27 May 2015 11:00:25 -0700 (PDT) In-Reply-To: <5563e453$0$12990$c3e8da3$5496439d@news.astraweb.com> References: <5563e453$0$12990$c3e8da3$5496439d@news.astraweb.com> Date: Wed, 27 May 2015 14:00:24 -0400 Subject: Re: a more precise distance algorithm From: Brian Blais To: "Steven D'Aprano" Cc: python-list@python.org Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 70 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432749627 news.xs4all.nl 2822 [2001:888:2000:d::a6]:34550 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91316 On Mon, May 25, 2015 at 11:11 PM, Steven D'Aprano wrote: > > Let's compare three methods. > > def naive(a, b): > return math.sqrt(a**2 + b**2) > > def alternate(a, b): > a, b = min(a, b), max(a, b) > if a == 0: return b > if b == 0: return a > return a * math.sqrt(1 + b**2 / a**2) > d1 = naive(a, b) > d2 = alternate(a, b) > d3 = math.hypot(a, b) > > which shows that: > > (1) It's not hard to find mismatches; > (2) It's not obvious which of the three methods is more accurate. > Bottom line: they all suck. :) I ran the program you posted, and, like you, got the following two examples: for fun in [naive, alternate, math.hypot]: print '%.20f' % fun(222.44802484683657,680.255801504161) 715.70320611153294976248 715.70320611153283607564 715.70320611153283607564 and for fun in [naive, alternate, math.hypot]: print '%.20f' % fun(376.47153302262484,943.1877995550265) 1015.54617837194291496417 1015.54617837194280127733 1015.54617837194291496417 but when comparing to Wolfram Alpha, which calculates these out many more decimal places, we have for the two cases: 715.7032061115328768204988784125331443593766145937358347357252... 715.70320611153294976248 715.70320611153283607564 715.70320611153283607564 1015.546178371942943007625196455666280385821355370154991424749... 1015.54617837194291496417 1015.54617837194280127733 1015.54617837194291496417 where all of the methods deviate at the 13/14 decimal place. bb -- ----------------- bblais@gmail.com http://web.bryant.edu/~bblais