Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91212

Re: a more precise distance algorithm

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gary.herron@islandtraining.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.008
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'else:': 0.03; 'elif': 0.04; 'root': 0.04; '"""': 0.05; 'attributes': 0.07; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'underlying': 0.09; 'python': 0.11; 'def': 0.14; 'b):': 0.16; 'subject:distance': 0.16; 'such,': 0.16; 'thoughts?': 0.16; 'wrote:': 0.16; 'alternate': 0.18; 'python?': 0.18; 'received:emailsrvr.com': 0.22; 'comment:': 0.23; "i've": 0.24; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'objects': 0.29; 'received:(smtp server)': 0.31; 'point': 0.33; 'another': 0.34; 'to:addr:python-list': 0.35; 'handle': 0.36; 'statement': 0.36; 'should': 0.37; 'subject:: ': 0.37; 'pm,': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'your': 0.60; 'subject:more': 0.61; 'more': 0.62; 'distance': 0.63; 'course': 0.64; 'between': 0.65; 'charset:windows-1252': 0.65; 'matter.': 0.66; 'special': 0.72; 'lose': 0.76; 'square': 0.76; 'discovered': 0.83; 'calculations': 0.84; 'herron': 0.84; 'suffer': 0.93
X-Sender-Id gary.herron@islandtraining.com
Date Mon, 25 May 2015 13:20:07 -0700
From Gary Herron <gary.herron@islandtraining.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: a more precise distance algorithm
References <b2e66a94-7a89-4be9-bbf7-9434396cc178@googlegroups.com>
In-Reply-To <b2e66a94-7a89-4be9-bbf7-9434396cc178@googlegroups.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.42.1432585603.5151.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432585603 news.xs4all.nl 2854 [2001:888:2000:d::a6]:43585
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91212

Show key headers only | View raw


On 05/25/2015 12: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 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?

This is a statement about floating point numeric calculations on a 
computer,.  As such, it does apply to Python which uses the underlying 
hardware for floating point calculations.

Validity is another matter.  Where did you find the quote?

Gary Herron


> 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)

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 12:21 -0700
  Re: a more precise distance algorithm felix <felix@epepm.cupet.cu> - 2015-05-25 16:06 -0400
  Re: a more precise distance algorithm Christian Gollwitzer <auriocus@gmx.de> - 2015-05-25 22:27 +0200
    Re: a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 14:03 -0700
  Re: a more precise distance algorithm Gary Herron <gary.herron@islandtraining.com> - 2015-05-25 13:20 -0700
    Re: a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 14:05 -0700
  Re: a more precise distance algorithm Steven D'Aprano <steve@pearwood.info> - 2015-05-26 13:11 +1000
    Re: a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 21:13 -0700
      Re: a more precise distance algorithm Gary Herron <gherron@digipen.edu> - 2015-05-25 22:09 -0700
        Re: a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 22:49 -0700
    Re: a more precise distance algorithm Christian Gollwitzer <auriocus@gmx.de> - 2015-05-26 07:33 +0200
    Re: a more precise distance algorithm Brian Blais <bblais@gmail.com> - 2015-05-27 14:00 -0400
    Re: a more precise distance algorithm Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2015-05-27 23:03 +0100
    Re: a more precise distance algorithm Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-05-27 23:04 -0400
  Re: a more precise distance algorithm Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-25 22:42 -0600
    Re: a more precise distance algorithm ravas <ravas@outlook.com> - 2015-05-25 21:59 -0700
  Re: a more precise distance algorithm random832@fastmail.us - 2015-05-26 09:40 -0400
  Re: a more precise distance algorithm random832@fastmail.us - 2015-05-26 09:51 -0400
  Re: a more precise distance algorithm Robin Becker <robin@reportlab.com> - 2015-05-27 14:02 +0100

csiph-web