Skip to content

fix (ship): clamp arcsin argument to [-1, 1] with np.clip to prevent NaN - #202

Open
vaaiiibbhav wants to merge 1 commit into
52North:mainfrom
vaaiiibbhav:vaibhav/fix-arcsin-nan
Open

fix (ship): clamp arcsin argument to [-1, 1] with np.clip to prevent NaN#202
vaaiiibbhav wants to merge 1 commit into
52North:mainfrom
vaaiiibbhav:vaibhav/fix-arcsin-nan

Conversation

@vaaiiibbhav

@vaaiiibbhav vaaiiibbhav commented Jul 18, 2026

Copy link
Copy Markdown

Floating-point rounding could push arg_arcsin in get_apparent_wind slightly outside the valid domain of np.arcsin. The previous manual check only clamped the upper bound, so values marginally below -1 produced silent NaNs that corrupted apparent wind angles and downstream fuel/route metrics. Replace the one-sided check with a two-sided np.clip.

Fixes #170

Related Issue / Discussion:

Fixes Issue #170

Changes:

  • Modified WeatherRoutingTool/ship/direct_power_boat.py — Replaced the manual one-sided upper bound check for arg_arcsin inside get_apparent_wind() with a robust two-sided np.clip.

Further Details:

Summary:

  • Motivation & Context: In get_apparent_wind(), floating-point rounding errors occasionally pushed the argument for np.arcsin() slightly outside the strict math domain of [-1, 1]. While an existing manual check handled the upper threshold, values slightly falling below -1 bypassed it, generating silent NaN values that cascaded down into critical ship routing and fuel consumption metrics.
  • Before: The code evaluated a manual difference condition (diff_to_one) that only protected against exceeding the upper bound of 1.0.
  • After: Replaced with np.clip(arg_arcsin, -1 * u.radian, 1 * u.radian), ensuring absolute two-sided domain safety while cleanly preserving the astropy.units Quantity wrapper.

Dependencies:

  • No new dependencies are required for this modification.

PR Checklist:

Floating-point rounding could push arg_arcsin in get_apparent_wind slightly
outside the valid domain of np.arcsin. The previous manual check only clamped
the upper bound, so values marginally below -1 produced silent NaNs that
corrupted apparent wind angles and downstream fuel/route metrics. Replace the
one-sided check with a two-sided np.clip.

Fixes 52North#170

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@vaaiiibbhav
vaaiiibbhav force-pushed the vaibhav/fix-arcsin-nan branch from ecd9d72 to 5241d23 Compare July 18, 2026 14:41
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.

Bug: arcsin lower bound not clamped in get_apparent_wind(), causes silent NaN

1 participant