Skip to content

fix: resolve precision loss and potential NaN in spherical coordinates conversion - #128

Open
vamshidharre wants to merge 1 commit into
helsing-ai:mainfrom
vamshidharre:fix/spherical-coordinates-atan2-precision
Open

vamshidharre wants to merge 1 commit into
helsing-ai:mainfrom
vamshidharre:fix/spherical-coordinates-atan2-precision

Conversation

@vamshidharre

Copy link
Copy Markdown

Summary

In Coordinate::spherical_coordinates (which powers Coordinate::bearing_from_origin and Vector::bearing_at_origin), polar and azimuth angles were previously calculated using inverse cosine:

let polar_value = (Length::new::<meter>(self.point.z) / r).get::<ratio>();
let polar = Angle::new::<radian>(FloatMath::acos(polar_value));
...
let azimuth_value = (Length::new::<meter>(self.point.x) / xy).get::<ratio>();
let azimuth = Angle::new::<radian>(FloatMath::copysign(
    FloatMath::acos(azimuth_value),
    self.point.y,
));

Problems:

  1. Catastrophic Precision Loss Near Axes / Small Angles:
    Because $\frac{d}{du}\arccos(u) = -\frac{1}{\sqrt{1 - u^2}} \to -\infty$ as $u \to 1$, evaluating $\arccos(x / \sqrt{x^2+y^2})$ loses significant precision for small angles:
    • For points near the X-axis ($|y| \le 10^{-8} \cdot x$), $x / \sqrt{x^2+y^2}$ rounds to 1.0 in double precision, causing acos(1.0) to return 0.0 (100% loss of azimuth angle!).
    • Similarly, points near the poles ($xy \le 10^{-8} \cdot |z|$) experienced the same collapse for polar elevation.
  2. Potential NaN / Panic:
    Due to floating point rounding in distance_from_origin() or sqrt(x^2 + y^2), if $z / r &gt; 1.0$ or $x / xy &gt; 1.0$, FloatMath::acos produces NaN, causing In::spherical_to_bearing to return None and panic at .expect(...).

Solution:

Replace the acos projections with standard, numerically stable FloatMath::atan2 formulations:

  • $\text{polar} = \text{atan2}(\text{hypot}(x, y), z)$. Since $\text{hypot}(x, y) \ge 0$, $\text{atan2}$ is strictly guaranteed to be in $[0, \pi]$ (0° to 180°), with no loss of precision near poles and zero possibility of NaN.
  • $\text{azimuth} = \text{atan2}(y, x)$ when $xy &gt; 0$, and 0 when $xy = 0$, preserving all 53 bits of floating-point precision down to subnormal magnitudes.
  • Added FloatMath::hypot across std and #![no_std] (libm).

Verification:

  • Added small_angle_azimuth_precision_preserved test confirming $10^{-8}$ rad azimuth resolution (which previously collapsed to 0).
  • Added small_angle_polar_elevation_precision_preserved test confirming polar deviation resolution near $\pm 90^\circ$ elevation.
  • Added spherical_coordinates_round_trip testing consistency with from_spherical across quadrants.
  • All 248 unit tests and 46 doctests passed.
  • Clippy passes cleanly with 0 warnings.
  • #![no_std] + libm verified via cargo check --no-default-features --features libm.
  • Code formatting verified via cargo fmt -- --check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant