Use safe_divide for the pole scaling in meters_to_degrees#37
Merged
Conversation
meters_to_degrees divides the zonal component by cos(lat), which is exactly zero only in the degenerate pole case. Route it through safe_divide so an exactly-zero cos yields 0 with a finite gradient instead of inf/nan. This is a corner-case guard: the flat-Earth longitude scaling genuinely diverges at the poles, and near (but not at) +-90 deg — where cos(lat) is tiny but nonzero — the zonal component is still legitimately huge. The docstring now states this limitation explicitly.
vadmbertr
marked this pull request as ready for review
July 8, 2026 12:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
meters_to_degreesdivides the zonal component bycos(lat), which is exactly zero only in the degenerate pole case.Scope, stated plainly: this is a corner-case guard, not a fix for the near-pole blowup. At
lat = 90°,cos(radians(90.0))is~6e-17in floating point — not zero — so the result is already a huge-but-finite number today, andsafe_dividedoes not change it (itsdenominator == 0branch never fires).safe_divideonly affects an exactly zero denominator (finite gradient, returns0), and the flat-Earth longitude scaling genuinely diverges at the poles regardless. The docstring now says this outright.Change
cos(lat)division throughsafe_divide.Tests
test_meters_to_degrees_finite_and_safe_at_pole: forward value and gradient finite atlat = 90°.test_meters_to_degrees_unchanged_at_normal_lat: identical to the naive division wherecos(lat) != 0(no regression from the swap).Full suite: 331 passed.