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


Groups > comp.lang.python > #91246

Re: a more precise distance algorithm

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <random832@fastmail.us>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'received:internal': 0.09; 'zero.': 0.09; 'python': 0.11; 'assume': 0.11; 'def': 0.14; 'algorithm.': 0.16; 'b):': 0.16; 'fallback': 0.16; 'hypot': 0.16; 'mean,': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'out?': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:messagingengine.com': 0.16; 'subject:distance': 0.16; 'thoughts?': 0.16; 'which,': 0.16; 'zero,': 0.16; 'wrote:': 0.16; 'odd': 0.18; 'say,': 0.18; 'python?': 0.18; 'library': 0.20; 'math': 0.20; 'function,': 0.22; 'absolute': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'equivalent': 0.27; 'wonder': 0.27; '(which': 0.29; 'function': 0.30; 'values': 0.30; 'code': 0.31; 'though,': 0.32; 'point': 0.33; 'needed': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'but': 0.36; 'being': 0.36; 'text': 0.36; 'there': 0.36; 'possible': 0.36; 'should': 0.37; 'received:10': 0.37; 'subject:: ': 0.37; 'tue,': 0.38; 'received:66': 0.38; 'version': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'your': 0.60; 'from:no real name:2**0': 0.61; 'subject:more': 0.61; 'header:Message-Id:1': 0.62; 'distance': 0.63; 'due': 0.65; 'concerns': 0.66; 'biggest': 0.67; 'choose': 0.68; '26,': 0.72; 'special': 0.72; 'flaw': 0.84; 'internally.': 0.84
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d=fastmail.us; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=7F4+ftekEbskhOmXVO1D1nHSS0M=; b=Z064Sm gnHgbCA+FPDdr4CnLJa95w5PMNL2O09gOpxIoYlWM/Hmqsrtgorjox64QQ32xTrU ZHNKASqwq/WBG8uvUw42Rq0+L4kDJwiUEGKfLrHo4/s7nUyGnbfLiwBj9h/ecJ+0 AhUBCWDPSGoMPFFv6QQI054X09EpEJPVdKhVs=
DKIM-Signature v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=7F4+ftekEbskhOm XVO1D1nHSS0M=; b=WQEETu373jN5cpT4LdCjIQhrw9lZKaHpvI6Lj7OX7z+IVId 9SqczTvWcR1USUbOSvi3GkFbTXZlFyzD6CAZtCrvm4hnJOb2XXansuHNjLkdVZXW RDiU7SpyPG8ufavF348xhgaqvAOjGBZP2A1na4imHOdO9/7OABhcnxOPpMk8=
X-Sasl-Enc qzYeFLK9rb0u9oMU2W8Ot00Tcsmaew6K8BySjaac2QA4 1432648317
From random832@fastmail.us
To python-list@python.org
MIME-Version 1.0
Content-Transfer-Encoding 7bit
Content-Type text/plain
X-Mailer MessagingEngine.com Webmail Interface - ajax-073992ec
In-Reply-To <1432647653.1149540.278304225.25D9F2E4@webmail.messagingengine.com>
References <b2e66a94-7a89-4be9-bbf7-9434396cc178@googlegroups.com> <1432647653.1149540.278304225.25D9F2E4@webmail.messagingengine.com>
Subject Re: a more precise distance algorithm
Date Tue, 26 May 2015 09:51:57 -0400
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.56.1432648319.5151.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432648319 news.xs4all.nl 2823 [2001:888:2000:d::a6]:54408
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91246

Show key headers only | View raw


On Tue, May 26, 2015, at 09:40, random832@fastmail.us wrote:
> On Mon, May 25, 2015, at 15:21, ravas wrote:
> > Is this valid? Does it apply to python?
> > Any other thoughts? :D
> 
> The math.hypot function uses the C library's function which should deal
> with such concerns internally. There is a fallback version in case the C
> library does not have this function, in Python/pymath.c - which,
> incidentally, does use your algorithm.

Well, I should say, not _precisely_ your algorithm.

The "0 special case" mentioned in the text you read was for both values
being zero, not just one. The biggest flaw in your function, though, was
the failure to take the absolute values of the differences. This defeats
the point of swapping them (which I assume is to get the magnitudes in
the order needed for best precision), and makes it possible for your
function to return a negative value when the other is zero.

Here's the equivalent python code for the hypot function in pymath.c,
and for your distance function.

from math import sqrt

def hypot(x, y):
    x = abs(x)
    y = abs(y)
    if x < y:
        x, y = y, x
    if x == 0:  # both are 0 due to the swap
        return 0.0
    else:
        return x*sqrt(1.0 + (y/x)**2)

def distance(A, B):
    return hypot(A.x-B.x, A.y-B.y)

What I wonder is if there's a best way to do it for three dimensions. I
mean, you could simply do hypot(hypot(dx, dy), dz), but should you
choose the largest, smallest, or middle value to be the odd one out?

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