Skip to content

Commit

Permalink
Force mod to be positive
Browse files Browse the repository at this point in the history
* Replace rust modulo with positive forced modulo
  • Loading branch information
wcampbell0x2a committed Aug 14, 2024
1 parent a76ce10 commit b980c92
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions libadsb_deku/src/cpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ pub fn get_position(cpr_frames: (&Altitude, &Altitude)) -> Option<Position> {

let j = libm::floor(59.0 * cpr_lat_even - 60.0 * cpr_lat_odd + 0.5);

let mut lat_even = D_LAT_EVEN * (j % 60.0 + cpr_lat_even);
let mut lat_odd = D_LAT_ODD * (j % 59.0 + cpr_lat_odd);
let mut lat_even = D_LAT_EVEN * (positive_mod(j, 60.0) + cpr_lat_even);
let mut lat_odd = D_LAT_ODD * (positive_mod(j, 59.0) + cpr_lat_odd);

if lat_even >= 270.0 {
lat_even -= 360.0;
Expand All @@ -268,6 +268,14 @@ pub fn get_position(cpr_frames: (&Altitude, &Altitude)) -> Option<Position> {
Some(Position { latitude: lat, longitude: lon })
}

fn positive_mod(a: f64, b: f64) -> f64 {
let mut ret = a % b;
if ret < 0.0 {
ret += b
}
ret
}

fn get_lat_lon(
lat: f64,
cpr_lon_even: f64,
Expand All @@ -281,8 +289,7 @@ fn get_lat_lon(
);

// rem_euclid
let r = m % ni;
let r = if r < 0.0 { r + libm::fabs(ni) } else { r };
let r = positive_mod(m, ni);

let mut lon = (360.0 / ni) * (r + c);
if lon >= 180.0 {
Expand Down

1 comment on commit b980c92

@dividebysandwich
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This greatly improved the amount of decoded and accepted positions for me.

Please sign in to comment.