Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'root': 0.04; '"""': 0.05; 'attributes': 0.07; 'incorrect': 0.09; 'negative.': 0.09; 'def': 0.14; 'b):': 0.16; 'sign.': 0.16; 'subject:distance': 0.16; 'thoughts?': 0.16; 'wrote:': 0.16; 'alternate': 0.18; 'python?': 0.18; '2015': 0.23; 'comment:': 0.23; "i've": 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.28; 'division': 0.29; 'objects': 0.29; 'branch': 0.31; 'received:google.com': 0.34; 'wrong': 0.35; 'could': 0.35; 'to:addr:python-list': 0.35; 'handle': 0.36; 'there': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'subject:more': 0.61; 'more': 0.62; 'distance': 0.63; 'course': 0.64; 'between': 0.65; 'special': 0.72; 'lose': 0.76; 'square': 0.76; 'discovered': 0.83; 'to:name:python': 0.84; 'suffer': 0.93 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:to :content-type:content-transfer-encoding; bh=fgDhvGMLOBvQbxX1EIYgVY50R/7eGH3qX6GDqgDxbSU=; b=tm0sy5tjq+URdAkYc7OYEfrAnmoDwECz2uFU6U5Pw652BuMJThbhteOs+Mq3D5tlk2 Wva3FewB88S9VH5Xd422LgiPGQqGWnVCaU/u/WApll4R06Tiy8FCkseyA1UrVWdQMlhg nbzi3EsVbfVnX5szW7F2529isvG3aOyjW/JbOq7HiicaSpFhaAQzuy3DEE6z2bmThz4i 2anBxgPo1sV5jiZO9gVmDLGCLv2ACSBQ7PJASkIhOMyCRDRhYQyQpLBzDrB1dLOBIZZ8 PjCH+ahAoD09F4qiXDZDkinbQxHjkQx/l1ZLmXz71WPXca/xJVZddDyjKsXSoPcLJBOh hUcA== X-Received: by 10.107.6.136 with SMTP id f8mr23474232ioi.61.1432615412081; Mon, 25 May 2015 21:43:32 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 25 May 2015 22:42:51 -0600 Subject: Re: a more precise distance algorithm To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432615414 news.xs4all.nl 2872 [2001:888:2000:d::a6]:38114 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91228 On Mon, May 25, 2015 at 1:21 PM, ravas wrote: > I read an interesting comment: > """ > The coolest thing I've ever discovered about Pythagorean's Theorem is an = alternate way to calculate it. If you write a program that uses the distanc= e form c =3D sqrt(a^2 + b^2) you will suffer from the lose of half of your = available precision because the square root operation is last. A more accur= ate calculation is c =3D a * sqrt(1 + b^2 / a^2). If a is less than b, you = should swap them and of course handle the special case of a =3D 0. > """ > > Is this valid? Does it apply to python? > Any other thoughts? :D > > My imagining: > > def distance(A, B): > """ > A & B are objects with x and y attributes > :return: the distance between A and B > """ > dx =3D B.x - A.x > dy =3D B.y - A.y > a =3D min(dx, dy) > b =3D max(dx, dy) > if a =3D=3D 0: > return b > elif b =3D=3D 0: > return a This branch is incorrect because a could be negative. You don't need this anyway; the a =3D=3D 0 branch is only there because of the division by a in the else branch. > else: > return a * sqrt(1 + (b / a)**2) Same issue; if a is negative then the result will have the wrong sign.