astronomy: azimuth no longer collapses at the geographic poles
- Commit
125965d937d78d289ac4e8efc263730d2a74e943- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 05:06 UTC (5 days ago)
- Parents
7f44b99
raDecToAltAz divided by cos(lat): at latitude exactly ±90 (which the app explicitly accepts as a manual location), cos(lat) ~ 6.1e-17 amplifies numerator rounding error into an essentially arbitrary azimuth — for LST 100°, RA 45°, Dec +20° the north pole returned 220.33° where the continuous limit is 235°, and the round trip came back 19.85° away. The inverse had the mirror flaw, both atan2 arguments scaled by cos(lat), so the transforms stopped being inverses: stars render wrong and tap/selection round-trips miss the object. Both transforms now use the division-free atan2 form — (sinAz, cosAz) scaled by the non-negative cosAlt (and the hour-angle form scaled by cosDec) — algebraically identical off-pole, tending to the correct pole limits, with the old zenith behaviour kept. The Rust batch port is mirrored so the native path cannot diverge (scalar tests included). Four new tests failed pre-fix with the numbers above. Rise/set, projection, constellations and search were audited and cleared. Verified: :astronomy:testDevUnitTest 132 tests and cargo test 7/7 pass; lint errors are pre-existing on untouched files.
Diff
astronomy/src/main/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransforms.kt
diff --git a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransforms.kt b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransforms.ktindex e52d7c829f..2b528a2241 100644--- a/astronomy/src/main/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransforms.kt+++ b/astronomy/src/main/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransforms.kt@@ -1,7 +1,6 @@ package com.vayunmathur.astronomy.domain.engine import com.vayunmathur.astronomy.domain.AstronomyNative-import kotlin.math.abs import kotlin.math.acos import kotlin.math.asin import kotlin.math.atan2@@ -30,14 +29,23 @@ object CoordinateTransforms { fun raDecToAltAz(raDec: RaDec, lstRad: Double, latRad: Double): AltAz { val ha = (lstRad - raDec.raRad).normalizePi()- val sinAlt = sin(raDec.decRad) * sin(latRad) + cos(raDec.decRad) * cos(latRad) * cos(ha)+ val sinDec = sin(raDec.decRad)+ val cosDec = cos(raDec.decRad)+ val sinAlt = sinDec * sin(latRad) + cosDec * cos(latRad) * cos(ha) val alt = asin(sinAlt.coerceIn(-1.0, 1.0))- val cosAlt = cos(alt)- val az = if (abs(cosAlt) < 1e-10) 0.0 else {- val cosAz = (sin(raDec.decRad) - sinAlt * sin(latRad)) / (cosAlt * cos(latRad))- val sinAz = -cos(raDec.decRad) * sin(ha) / cosAlt- atan2(sinAz, cosAz).normalize2Pi()- }+ // Azimuth via atan2 of (sinAz, cosAz) scaled by the non-negative+ // cosAlt: sinAz*cosAlt = -cosDec*sinHA and+ // cosAz*cosAlt = sinDec*cosLat - cosDec*cosHA*sinLat. The old form+ // divided by cosAlt*cosLat, so at the geographic poles (cosLat ~+ // 6e-17) the cancelled numerator's rounding error was amplified into+ // azimuths off by ~15 degrees and the transform stopped inverting.+ // This form tends to the continuous pole limit (HA + 180deg at the+ // north pole); at the zenith both arguments vanish and atan2 gives+ // 0, as before.+ val az = atan2(+ -cosDec * sin(ha),+ sinDec * cos(latRad) - cosDec * cos(ha) * sin(latRad),+ ).normalize2Pi() return AltAz(az, alt) } @@ -46,13 +54,15 @@ object CoordinateTransforms { cos(altAz.altRad) * cos(latRad) * cos(altAz.azRad) val dec = asin(sinDec.coerceIn(-1.0, 1.0)) // Inverts raDecToAltAz exactly: from sinH = -sinAz*cosAlt/cosDec and- // cosH = (sinAlt-sinDec*sinLat)/(cosDec*cosLat), multiply both by the- // positive factor cosDec*cosLat so no division can blow up. The old- // second argument mixed the azimuth convention and returned hour- // angles off by up to hundreds of degrees.+ // cosH = (sinAlt*cosLat - cosAlt*cosAz*sinLat)/cosDec, multiply both+ // by the positive factor cosDec so no division can blow up. Scaling+ // by cosLat as well (the previous form) collapses both arguments to+ // ~0 at the geographic poles, where atan2 then returns a rounding-+ // noise hour angle; this form keeps the pole limit (HA = Az - 180deg+ // at the north pole). val ha = atan2(- -sin(altAz.azRad) * cos(altAz.altRad) * cos(latRad),- sin(altAz.altRad) - sinDec * sin(latRad),+ -sin(altAz.azRad) * cos(altAz.altRad),+ sin(altAz.altRad) * cos(latRad) - cos(altAz.altRad) * cos(altAz.azRad) * sin(latRad), ) val ra = (lstRad - ha).normalize2Pi() return RaDec(ra, dec)
astronomy/src/main/rust/src/coords.rs
diff --git a/astronomy/src/main/rust/src/coords.rs b/astronomy/src/main/rust/src/coords.rsindex 518827bc30..4af967662b 100644--- a/astronomy/src/main/rust/src/coords.rs+++ b/astronomy/src/main/rust/src/coords.rs@@ -28,7 +28,7 @@ fn normalize_2pi(x: f64) -> f64 { /// Batch RaDec -> AltAz. `radec` is interleaved `[ra0, dec0, ra1, dec1, ...]` /// in radians; returns interleaved `[az0, alt0, az1, alt1, ...]` in radians. /// Mirrors `CoordinateTransforms.batchRaDecToAltAz` exactly, including hoisting-/// sin/cos of the latitude out of the loop and the `abs(cosAlt) < 1e-10` guard.+/// sin/cos of the latitude out of the loop. pub fn batch_radec_to_altaz(radec: &[f64], lst_rad: f64, lat_rad: f64) -> Vec<f64> { let sin_lat = lat_rad.sin(); let cos_lat = lat_rad.cos();@@ -42,14 +42,13 @@ pub fn batch_radec_to_altaz(radec: &[f64], lst_rad: f64, lat_rad: f64) -> Vec<f6 let cos_dec = dec.cos(); let sin_alt = sin_dec * sin_lat + cos_dec * cos_lat * ha.cos(); let alt = sin_alt.clamp(-1.0, 1.0).asin();- let cos_alt = alt.cos();- let az = if cos_alt.abs() < 1e-10 {- 0.0- } else {- let cos_az = (sin_dec - sin_alt * sin_lat) / (cos_alt * cos_lat);- let sin_az = -cos_dec * ha.sin() / cos_alt;- normalize_2pi(sin_az.atan2(cos_az))- };+ // (sinAz, cosAz) scaled by the non-negative cosAlt, mirroring the+ // Kotlin scalar form: no division by cosLat, so the geographic+ // poles keep the continuous azimuth limit instead of amplifying+ // the cancelled numerator's rounding error.+ let az = normalize_2pi(+ (-cos_dec * ha.sin()).atan2(sin_dec * cos_lat - cos_dec * ha.cos() * sin_lat),+ ); out.push(az); out.push(alt); }
astronomy/src/main/rust/src/lib.rs
diff --git a/astronomy/src/main/rust/src/lib.rs b/astronomy/src/main/rust/src/lib.rsindex 353bf374ae..d02b80b416 100644--- a/astronomy/src/main/rust/src/lib.rs+++ b/astronomy/src/main/rust/src/lib.rs@@ -34,14 +34,9 @@ mod tests { let ha = normalize_pi(lst - ra); let sin_alt = dec.sin() * lat.sin() + dec.cos() * lat.cos() * ha.cos(); let alt = sin_alt.clamp(-1.0, 1.0).asin();- let cos_alt = alt.cos();- let az = if cos_alt.abs() < 1e-10 {- 0.0- } else {- let cos_az = (dec.sin() - sin_alt * lat.sin()) / (cos_alt * lat.cos());- let sin_az = -dec.cos() * ha.sin() / cos_alt;- normalize_2pi(sin_az.atan2(cos_az))- };+ let az = normalize_2pi(+ (-dec.cos() * ha.sin()).atan2(dec.sin() * lat.cos() - dec.cos() * ha.cos() * lat.sin()),+ ); (az, alt) } @@ -61,6 +56,18 @@ mod tests { } } + #[test]+ fn batch_altaz_at_north_pole_uses_continuous_azimuth() {+ // LST 100deg, RA 45deg, Dec 20deg at the pole: alt = Dec and+ // az = HA + 180deg = 235deg (the pre-fix division by cos(lat)+ // returned 220.334deg here).+ let lat = PI / 2.0;+ let lst = 100.0_f64.to_radians();+ let out = batch_radec_to_altaz(&[45.0_f64.to_radians(), 20.0_f64.to_radians()], lst, lat);+ assert!((out[1].to_degrees() - 20.0).abs() < 1e-9, "pole alt {}", out[1].to_degrees());+ assert!((out[0].to_degrees() - 235.0).abs() < 1e-9, "pole az {}", out[0].to_degrees());+ }+ #[test] fn batch_altaz_hand_checked_zenith() { // A star on the meridian (ha=0) at the same dec as latitude sits at the
astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransformsTest.kt
diff --git a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransformsTest.kt b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransformsTest.ktindex 6592eb9fa6..44a209a5ef 100644--- a/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransformsTest.kt+++ b/astronomy/src/test/java/com/vayunmathur/astronomy/domain/engine/CoordinateTransformsTest.kt@@ -40,6 +40,65 @@ class CoordinateTransformsTest { } } + @Test+ fun raDec_to_AltAz_at_north_pole_uses_continuous_azimuth() {+ // At the geographic pole altitude equals declination and azimuth is+ // the continuous limit HA + 180deg (HA = LST - RA = 55deg here), not+ // whatever the division by cos(lat) ~ 6e-17 happens to amplify.+ val lat = 90.0.toRad()+ val lst = 100.0.toRad()+ val aa = CoordinateTransforms.raDecToAltAz(RaDec(45.0.toRad(), 20.0.toRad()), lst, lat)+ assertTrue(abs(aa.altDeg - 20.0) < 1e-6, "pole alt ${aa.altDeg}")+ assertTrue(abs(aa.azDeg - 235.0) < 1e-6, "pole az ${aa.azDeg}, expected 235")+ val aa2 = CoordinateTransforms.raDecToAltAz(RaDec(200.0.toRad(), (-30.0).toRad()), lst, lat)+ assertTrue(abs(aa2.altDeg - (-30.0)) < 1e-6, "pole alt ${aa2.altDeg}")+ assertTrue(abs(aa2.azDeg - 80.0) < 1e-6, "pole az ${aa2.azDeg}, expected 80")+ }++ @Test+ fun raDec_to_AltAz_at_south_pole_uses_continuous_azimuth() {+ val lat = (-90.0).toRad()+ val lst = 100.0.toRad()+ val aa = CoordinateTransforms.raDecToAltAz(RaDec(45.0.toRad(), 20.0.toRad()), lst, lat)+ assertTrue(abs(aa.altDeg - (-20.0)) < 1e-6, "pole alt ${aa.altDeg}")+ assertTrue(abs(aa.azDeg - 305.0) < 1e-6, "pole az ${aa.azDeg}, expected 305")+ }++ @Test+ fun altAz_roundTrip_at_both_poles() {+ val lst = 100.0.toRad()+ for (latDeg in listOf(90.0, -90.0)) {+ val lat = latDeg.toRad()+ for ((raDeg, decDeg) in listOf(45.0 to 20.0, 200.0 to -30.0, 45.0 to 80.0, 320.0 to -60.0)) {+ val original = RaDec(raDeg.toRad(), decDeg.toRad())+ val altAz = CoordinateTransforms.raDecToAltAz(original, lst, lat)+ val back = CoordinateTransforms.altAzToRaDec(altAz, lst, lat)+ val dist = CoordinateTransforms.angularDistanceRad(original, back)+ assertTrue(dist < 1e-6, "lat $latDeg ra $raDeg dec $decDeg roundtrip error $dist rad")+ }+ }+ }++ @Test+ fun batchMatchesSingleAtPoles() {+ // The batch path is the Rust port of the scalar transform; it must+ // not diverge from it exactly where the old formula broke.+ val lst = 100.0.toRad()+ val list = listOf(+ RaDec(45.0.toRad(), 20.0.toRad()),+ RaDec(200.0.toRad(), (-30.0).toRad()),+ )+ for (latDeg in listOf(90.0, -90.0)) {+ val lat = latDeg.toRad()+ val batch = CoordinateTransforms.batchRaDecToAltAz(list, lst, lat)+ list.forEachIndexed { i, rd ->+ val single = CoordinateTransforms.raDecToAltAz(rd, lst, lat)+ assertTrue(abs(single.altRad - batch[i].altRad) < 1e-9, "lat $latDeg alt[$i]")+ assertTrue(abs(single.azRad - batch[i].azRad) < 1e-9, "lat $latDeg az[$i]")+ }+ }+ }+ @Test fun ecliptic_roundTrip() { val eps = 23.44.toRad()