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


Groups > comp.lang.python > #91327

Re: a more precise distance algorithm

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'line:': 0.07; 'accuracy.': 0.09; 'def': 0.14; 'alpha,': 0.16; 'b):': 0.16; 'calculates': 0.16; 'cases:': 0.16; 'digits.': 0.16; 'examples:': 0.16; 'posted,': 0.16; 'skip:1 60': 0.16; 'skip:7 20': 0.16; 'subject:distance': 0.16; 'to:name:python list': 0.16; 'wrote:': 0.16; 'comparing': 0.18; '>>>': 0.20; 'compare': 0.20; '2015': 0.23; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'not.': 0.27; 'skip:f 40': 0.27; 'message-id:@mail.gmail.com': 0.28; 'decimal': 0.29; 'lies': 0.29; 'methods.': 0.29; 'point.': 0.29; 'correct': 0.29; 'print': 0.31; 'source': 0.31; "can't": 0.32; 'point': 0.33; "d'aprano": 0.33; 'steven': 0.33; 'add': 0.34; 'received:google.com': 0.34; 'to:addr:python-list': 0.35; 'but': 0.36; 'brian': 0.36; "let's": 0.36; 'two': 0.37; 'should': 0.37; 'subject:: ': 0.37; '(2)': 0.37; "won't": 0.38; 'difference': 0.38; 'ones': 0.38; '(1)': 0.38; 'pm,': 0.39; 'expect': 0.39; 'to:addr:python.org': 0.39; 'where': 0.40; 'free': 0.61; 'bottom': 0.61; 'fun': 0.61; 'subject:more': 0.61; 'show': 0.62; 'more': 0.62; 'between': 0.65; 'obvious': 0.72; 'oscar': 0.84
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; bh=8OAmiVvZ7VflEzz5Qw1XbcvUgDPV409jTqhy7/QtiGs=; b=rTsRkgVGxHUDOOryWy4iQ2GdAWv5+d7I2tPS/wiOZEdXIq4ow1Ay+U8bHWb9lmjWp2 qI12HrP77o/AuQxmaE3PzVtEwbyjC0uzUPB9PDB7N8jmu2UKTZ3XuEZYNR3q7vQNLzD3 QMXiNYeP3OsDO3ucnlXqGzEzvbBS9FfmiCq4r7MYhYkUT72SeV4F1RC48ZA+qCrYIsQX uuBoFy5R5DhurNC8VlntDtiFx/wi3IG/SIazTixDgyJXD+AsDZRMHboh+fMzLpJyDNlq OYe9I5ATgI6sKwaJbGbQyrjq7zFTxrVl0Y5B4oQX5tUhE5IEbj2ZltA0n6+cv5qRGd1C Ek9w==
X-Received by 10.194.118.167 with SMTP id kn7mr24693303wjb.113.1432764249698; Wed, 27 May 2015 15:04:09 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CADzuzGNggKfgBiq3RMtWk3GBF3ynbkjm2brdXj4cuUQwQ5yQkg@mail.gmail.com>
References <b2e66a94-7a89-4be9-bbf7-9434396cc178@googlegroups.com> <5563e453$0$12990$c3e8da3$5496439d@news.astraweb.com> <CADzuzGNggKfgBiq3RMtWk3GBF3ynbkjm2brdXj4cuUQwQ5yQkg@mail.gmail.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Wed, 27 May 2015 23:03:49 +0100
Subject Re: a more precise distance algorithm
To Python List <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 <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.102.1432764252.5151.python-list@python.org> (permalink)
Lines 84
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1432764252 news.xs4all.nl 2887 [2001:888:2000:d::a6]:37486
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91327

Show key headers only | View raw


On 27 May 2015 at 19:00, Brian Blais <bblais@gmail.com> wrote:
> On Mon, May 25, 2015 at 11:11 PM, Steven D'Aprano <steve@pearwood.info> 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.

So they have 12/13 correct digits after the decimal point. Including
the digits before the decimal point they all have 15/16 correct
decimal digits. This is exactly what you should expect when using
double precision floating point. Feel free to print as many digits as
you like but the extra ones won't add any more accuracy.

The difference between the answers you showed are just 1 ULP:
>>> a = 715.70320611153283607564
>>> b = 715.70320611153294976248
>>> a
715.7032061115328
>>> a + 5e-14
715.7032061115328
>>> a + 6e-14
715.703206111533
>>> a + 6e-14 == b
True

Since you don't show the source numbers we can't know whether the true
result lies between these two or not.


Oscar

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