Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!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.039 X-Spam-Evidence: '*H*': 0.92; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'root': 0.04; '"""': 0.05; 'attributes': 0.07; 'def': 0.14; 'b):': 0.16; 'escribi\xf3:': 0.16; 'fits': 0.16; 'inverse': 0.16; 'subject:distance': 0.16; 'thoughts?': 0.16; 'alternate': 0.18; 'python?': 0.18; 'comment:': 0.23; "i've": 0.24; 'header:In-Reply- To:1': 0.24; 'second': 0.24; 'header:User-Agent:1': 0.26; 'objects': 0.29; 'function': 0.30; 'header:Received:8': 0.31; 'to:addr:python-list': 0.35; 'handle': 0.36; 'but': 0.36; 'should': 0.37; 'received:10': 0.37; 'subject:: ': 0.37; 'does': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'subject:more': 0.61; 'here.': 0.61; 'more': 0.62; 'distance': 0.63; 'gave': 0.63; 'course': 0.64; 'between': 0.65; 'here': 0.66; 'received:200': 0.69; 'special': 0.72; 'applying': 0.73; 'lose': 0.76; 'square': 0.76; 'introduce': 0.78; 'discovered': 0.83; 'suffer': 0.93 X-Virus-Scanned: Debian amavisd-new at libra1.minem.cu Date: Mon, 25 May 2015 16:06:06 -0400 From: felix User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: a more precise distance algorithm References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------050905020903090302080100" 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: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432584129 news.xs4all.nl 2871 [2001:888:2000:d::a6]:56308 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:91209 This is a multi-part message in MIME format. --------------050905020903090302080100 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit El 25/05/15 15:21, ravas escribió: > 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 distance form c = 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 accurate calculation is c = 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 = 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 = B.x - A.x > dy = B.y - A.y > a = min(dx, dy) > b = max(dx, dy) > if a == 0: > return b > elif b == 0: > return a > else: > return a * sqrt(1 + (b / a)**2) I don't know if precision lose fits here but the second way you gave to calculate c is just Math. Nothing extraordinary here. c = a * sqrt(1 + b^2 / a^2) c = sqrt(a^2(1 + b^2 / a^2)) applying the inverse function to introduce a inside the square root c = sqrt(a^2 + a^2*b^2/a^2) then just simplify c = sqrt(a^2 + b^2) --------------050905020903090302080100 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit
El 25/05/15 15:21, ravas escribió:
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 distance form c = 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 accurate calculation is c = 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 = 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 = B.x - A.x
    dy = B.y - A.y
    a = min(dx, dy)
    b = max(dx, dy)
    if a == 0:
        return b
    elif b == 0:
        return a
    else:
        return a * sqrt(1 + (b / a)**2)
I don't know if precision lose fits here but the second way you gave to calculate c is just Math. Nothing extraordinary here.

c = a * sqrt(1 + b^2 / a^2)
c = sqrt(a^2(1 + b^2 / a^2)) applying the inverse function to introduce a inside the square root
c = sqrt(a^2 + a^2*b^2/a^2) then just simplify
c = sqrt(a^2 + b^2)

--------------050905020903090302080100--