fix(mathutil): preserve distances at extreme scales (#360)

This commit is contained in:
Yusuf Efe authored and GitHub committed 2026-10-23 22:43:47 +08:00
1 parent ec11fdf345
commit 9b0a0caa75
2 files changed
+10 -5

No files matched your search

+1 -5
View File
@@ -299,11 +299,7 @@ func RadianToAngle(radian float64) float64 {
// PointDistance get two points distance. // PointDistance get two points distance.
// Play: https://go.dev/play/p/RrG4JIaziM8 // Play: https://go.dev/play/p/RrG4JIaziM8
func PointDistance(x1, y1, x2, y2 float64) float64 { func PointDistance(x1, y1, x2, y2 float64) float64 {
a := x1 - x2 return math.Hypot(x1-x2, y1-y2)
b := y1 - y2
c := math.Pow(a, 2) + math.Pow(b, 2)
return math.Sqrt(c)
} }
// IsPrime checks if number is prime number. // IsPrime checks if number is prime number.
+9
View File
@@ -334,6 +334,15 @@ func TestRadianToAngle(t *testing.T) {
assert.Equal(float64(45), result3) 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) { func TestPointDistance(t *testing.T) {
t.Parallel() t.Parallel()