Skip to content

Commit

Permalink
use hypot for magnitude to reduce over/underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
thehappycheese committed Aug 8, 2023
1 parent 360d5f0 commit c03a1f9
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion geo/src/algorithm/vector_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ where
}

fn magnitude(self) -> Self::Scalar {
(self.x * self.x + self.y * self.y).sqrt()
// Note uses cmath::hypot which avoids 'undue overflow and underflow'
// This also increases the range of values for which `.try_normalize()` works
Self::Scalar::hypot(self.x, self.y)
}

fn magnitude_squared(self) -> Self::Scalar {
Expand Down

0 comments on commit c03a1f9

Please sign in to comment.