[New-bugs-announce] [issue41513] Scale by power of two in math.hypot()
report at bugs.python.org
Sun Aug 9 21:14:14 EDT 2020
New submission from Raymond Hettinger <raymond.hettinger at gmail.com>:
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.
components: Library (Lib)
nosy: mark.dickinson, rhettinger, serhiy.storchaka, tim.peters
title: Scale by power of two in math.hypot()
versions: Python 3.10
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce