Re: a more precise distance algorithm

2015-05-27 Thread Oscar Benjamin
On 27 May 2015 at 19:00, Brian Blais wrote: > On Mon, May 25, 2015 at 11:11 PM, Steven D'Aprano 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

Re: a more precise distance algorithm

2015-05-27 Thread Brian Blais
On Mon, May 25, 2015 at 11:11 PM, Steven D'Aprano 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 /

Re: a more precise distance algorithm

2015-05-27 Thread Robin Becker
A minor point is that if you just need to compare distances you don't need to compute the hypotenuse, its square will do so no subtractions etc etc. -- Robin Becker -- https://mail.python.org/mailman/listinfo/python-list

Re: a more precise distance algorithm

2015-05-26 Thread random832
On Tue, May 26, 2015, at 09:40, random...@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

Re: a more precise distance algorithm

2015-05-26 Thread random832
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 P

Re: a more precise distance algorithm

2015-05-25 Thread ravas
On Monday, May 25, 2015 at 10:16:02 PM UTC-7, Gary Herron wrote: > It's probably not the square root that's causing the inaccuracies. In > many other cases, and probably here also, it's the summing of two > numbers that have vastly different values that loses precision. A > demonstration: > >

Re: a more precise distance algorithm

2015-05-25 Thread Christian Gollwitzer
Am 26.05.15 um 05:11 schrieb Steven D'Aprano: mismatch after 3 trials naive: 767.3916150255787 alternate: 767.3916150255789 hypot: 767.3916150255787 which shows that: (1) It's not hard to find mismatches; (2) It's not obvious which of the three methods is more accurate. The main problem is

Re: a more precise distance algorithm

2015-05-25 Thread Gary Herron
On 05/25/2015 09:13 PM, ravas wrote: On Monday, May 25, 2015 at 8:11:25 PM UTC-7, Steven D'Aprano wrote: Let's compare three methods. ... which shows that: (1) It's not hard to find mismatches; (2) It's not obvious which of the three methods is more accurate. Thank you; that is very helpful!

Re: a more precise distance algorithm

2015-05-25 Thread ravas
Oh ya... true >_< Thanks :D On Monday, May 25, 2015 at 9:43:47 PM UTC-7, Ian wrote: > > 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)

Re: a more precise distance algorithm

2015-05-25 Thread Ian Kelly
On Mon, May 25, 2015 at 1: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

Re: a more precise distance algorithm

2015-05-25 Thread ravas
On Monday, May 25, 2015 at 8:11:25 PM UTC-7, Steven D'Aprano wrote: > Let's compare three methods. > ... > which shows that: > > (1) It's not hard to find mismatches; > (2) It's not obvious which of the three methods is more accurate. Thank you; that is very helpful! I'm curious: what about the

Re: a more precise distance algorithm

2015-05-25 Thread Steven D'Aprano
On Tue, 26 May 2015 05:21 am, 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 ha

Re: a more precise distance algorithm

2015-05-25 Thread ravas
On Monday, May 25, 2015 at 1:27:43 PM UTC-7, Gary Herron wrote: > 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 yo

Re: a more precise distance algorithm

2015-05-25 Thread ravas
On Monday, May 25, 2015 at 1:27:24 PM UTC-7, Christian Gollwitzer wrote: > Wrong. Just use the built-in function Math.hypot() - it should handle > these cases and also overflow, infinity etc. in the best possible way. > > Apfelkiste:~ chris$ python > Python 2.7.2 (default, Oct 11 2012, 20:14:37)

Re: a more precise distance algorithm

2015-05-25 Thread Christian Gollwitzer
Am 25.05.15 um 21:21 schrieb ravas: 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 av

Re: a more precise distance algorithm

2015-05-25 Thread Gary Herron
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 a

Re: a more precise distance algorithm

2015-05-25 Thread felix
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 ava

a more precise distance algorithm

2015-05-25 Thread ravas
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 r