a more precise distance algorithm

Gary Herron gherron at digipen.edu
Tue May 26 01:09:53 EDT 2015


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!
>
> I'm curious: what about the sqrt() function being last is detrimental?
>  From a point of ignorance it seems like we are just producing errors sooner,
> and then multiplying them, with this alternative method.

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:

 >>> big = 100000000.0
 >>> small = 0.000000001
 >>> (big+small)-big # Should produce a value =small, but gives an exact 
zero instead.
0.0

The squaring of the two values in x*x+y*y just makes the addition even 
more error prone since the squares make large values even larger and 
small values even smaller.


Gary Herron.

-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418




More information about the Python-list mailing list