Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #91316
| References | <b2e66a94-7a89-4be9-bbf7-9434396cc178@googlegroups.com> <5563e453$0$12990$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2015-05-27 14:00 -0400 |
| Subject | Re: a more precise distance algorithm |
| From | Brian Blais <bblais@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.92.1432749627.5151.python-list@python.org> (permalink) |
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.
bb
--
-----------------
bblais@gmail.com
http://web.bryant.edu/~bblais
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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