➜

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients mark.dickinson, rhettinger, serhiy.storchaka, tim.peters
Date 2020-08-10.01:14:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1597022054.77.0.549983904563.issue41513@roundup.psfhosted.org>
In-reply-to
Content
I'd like to resurrect Serhiy's idea for using power-of-two scaling in math.hypot() rather than dividing each coordinate by the maximum value.

I drafted a patch.  It looks to be a little faster than what we have now and is generally (but not always) more accurate.

For accuracy, the current approach has the advantage that for the largest coordinate, (x/max)**2 is exactly 1.0.   If that element is much larger than the others, it overpowers the 1/2 ulp error in each of the other divisions.

In contrast, scaling by a power of two is always exact, but the squaring of the largest scaled coordinate is no longer exact.

The two approaches each have some cases that are more accurate than the other.  I generated 10,000 samples.  In 66% of the cases, the two results agree.  In 26% of the cases, scaling by a power of two was more accurate.  In the remaining 8%, the division by max was more accurate.   The standard deviation of relative errors was better using power-of-two scaling.  These results were consistent whether using 2, 3, or 20 dimensions.

As expected, multiplying by a power of two was a bit faster than dividing by an arbitrary float.
History
Date User Action Args
2020-08-10 01:14:14rhettingersetrecipients: + rhettinger, tim.peters, mark.dickinson, serhiy.storchaka
2020-08-10 01:14:14rhettingersetmessageid: <1597022054.77.0.549983904563.issue41513@roundup.psfhosted.org>
2020-08-10 01:14:14rhettingerlinkissue41513 messages
2020-08-10 01:14:14rhettingercreate