From 9b0a0caa7597e12f4b5045805f46df42006c57f5 Mon Sep 17 00:00:00 2001 From: Yusuf Efe <120668197+yuefdev@users.noreply.github.com> Date: Fri, 23 Oct 2026 17:43:47 +0300 Subject: [PATCH] fix(mathutil): preserve distances at extreme scales (#360) --- mathutil/mathutil.go | 6 +----- mathutil/mathutil_test.go | 9 +++++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/mathutil/mathutil.go b/mathutil/mathutil.go index e27f430..ac933be 100644 --- a/mathutil/mathutil.go +++ b/mathutil/mathutil.go @@ -299,11 +299,7 @@ func RadianToAngle(radian float64) float64 { // PointDistance get two points distance. // Play: https://go.dev/play/p/RrG4JIaziM8 func PointDistance(x1, y1, x2, y2 float64) float64 { - a := x1 - x2 - b := y1 - y2 - c := math.Pow(a, 2) + math.Pow(b, 2) - - return math.Sqrt(c) + return math.Hypot(x1-x2, y1-y2) } // IsPrime checks if number is prime number. diff --git a/mathutil/mathutil_test.go b/mathutil/mathutil_test.go index 869dbc9..4350608 100644 --- a/mathutil/mathutil_test.go +++ b/mathutil/mathutil_test.go @@ -334,6 +334,15 @@ func TestRadianToAngle(t *testing.T) { assert.Equal(float64(45), result3) } +func TestPointDistanceExtremeScales(t *testing.T) { + for _, scale := range []float64{1e200, 1e-200} { + got := PointDistance(0, 0, 3*scale, 4*scale) + if math.Abs(got/(5*scale)-1) > 1e-15 { + t.Errorf("scale %g: got %g, want %g", scale, got, 5*scale) + } + } +} + func TestPointDistance(t *testing.T) { t.Parallel()