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, skrah, steven.daprano, tim.peters
Date 2018-03-16.18:50:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
A need for a distance-between-two-points function arises frequently enough to warrant consideration for inclusion in the math module.   It shows-up throughout mathematics -- everywhere from simple homework problems for kids to machine learning and computer vision.

In the latter cases, the function is called frequently and would benefit from a fast C implementation that includes good error checking and is algorithmically smart about numerical issues such as overflow and loss-of-precision.

A simple implementation would be something like this:

    def dist(p, q):
        'Multi-dimensional Euclidean distance'
        # XXX needs error checking:  len(p) == len(q)
        return sqrt(sum((x0 - x1) ** 2 for x0, x1 in zip(p, q)))

The implementation could also include value added features such as hypot() style scaling to mitigate overflow during the squaring step:

    def dist2(p, q):
        diffs = [x0 - x1 for x0, x1 in zip(p, q)]
        scale = max(diffs, key=abs)
        return abs(scale) * sqrt(fsum((d/scale) ** 2 for d in diffs))
Date User Action Args
2018-03-16 18:50:58rhettingersetrecipients: + rhettinger, tim.peters, mark.dickinson, steven.daprano, skrah
2018-03-16 18:50:58rhettingersetmessageid: <>
2018-03-16 18:50:58rhettingerlinkissue33089 messages
2018-03-16 18:50:58rhettingercreate