a more precise distance algorithm

Christian Gollwitzer auriocus at gmx.de
Tue May 26 01:33:46 EDT 2015


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 not necessarily precision. A square root is a very 
precise operation in floating point math, the relative precision 
*increases* by sqrt. The big problem is overflow. Take e.g. a=3*10^160, 
b=4*10^160, then the exact result is c=5*10^160. But:

 >>> a=3e160
 >>> b=4e160
 >>> math.sqrt(a**2+b**2)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
OverflowError: (34, 'Result too large')
 >>> math.hypot(a,b)
5e+160


	Christian





More information about the Python-list mailing list