Skip to content

Commit

Permalink
Add atan2 tests, fix implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlaltf2 committed Mar 17, 2024
1 parent 498ea14 commit e7c2fce
Showing 1 changed file with 29 additions and 9 deletions.
38 changes: 29 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub fn fast_atan2<F: Float>(y: F, x: F) -> F {
return (y / x).atan();
} else if x == zero {
if y < zero {
return -pi;
return NumCast::from(-std::f64::consts::PI / 2.).unwrap();
} else if y == zero {
return zero;
} else if y > zero {
return pi;
return NumCast::from(std::f64::consts::PI / 2.).unwrap();
}
} else if x < zero {
if y < zero {
Expand Down Expand Up @@ -162,10 +162,6 @@ pub fn solaris_base_impl(
};
}

pub fn add(left: usize, right: usize) -> usize {
left + right
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -175,11 +171,11 @@ mod tests {
// 2000-01-01 as julian day number
const jd2000_epoch: f64 = 2451544.5;
// ~2024-03-17T14:48
const now: f64 = 2460387.1166667;
const now: f64 = 2460387.166666667;

// Somewhere on the road in central PA where I'm running this code :)
const obs_lat: f64 = 41.127323 * (std::f64::consts::PI / 180.);
const obs_lon: f64 = -77.438413 * (std::f64::consts::PI / 180.);
const obs_lat: f64 = 40.884305 * (std::f64::consts::PI / 180.);
const obs_lon: f64 = -77.779766 * (std::f64::consts::PI / 180.);

const t_Jd: f64 = now - jd2000_epoch;
const t_Jc: f64 = t_Jd / (365.25 * 100.);
Expand All @@ -191,4 +187,28 @@ mod tests {
sun_pos.azimuth * (180. / std::f64::consts::PI)
);
}

#[test]
fn fast_atan2_test() {
let approx_eq = |x: f64, y: f64| -> bool {
let diff = (y - x).abs();
println!("diff({}, {}) = {}", x, y, diff);
diff < 1e-6
};

// x < 0
assert!(approx_eq(fast_atan2::<f64>(-3., -4.), -2.4980915448));
assert!(approx_eq(fast_atan2::<f64>(0., -4.), 3.14159265359));
assert!(approx_eq(fast_atan2::<f64>(3., -4.), 2.4980915448));

// x == 0
assert!(approx_eq(fast_atan2::<f64>(-3., 0.), -1.57079632679));
assert!(approx_eq(fast_atan2::<f64>(0., 0.), 0.));
assert!(approx_eq(fast_atan2::<f64>(3., 0.), 1.57079632679));

// x > 0
assert!(approx_eq(fast_atan2::<f64>(-3., 4.), -0.643501108793));
assert!(approx_eq(fast_atan2::<f64>(0., 4.), 0.));
assert!(approx_eq(fast_atan2::<f64>(3., 4.), 0.643501108793));
}
}

0 comments on commit e7c2fce

Please sign in to comment.