rsadsb / adsb_deku

✈️ Rust ADS-B decoder + tui radar application
MIT License
622 stars 22 forks source link

Handle negative `m` value #78

Closed amacd31 closed 2 years ago

amacd31 commented 2 years ago

If the m value in the CPR calculation works out to be negative the use of % results in an incorrect (negative) value.

This commit uses rem_euclid instead of % to get the expected value when calculating longitude (at least for the test input used and within a few decimal places).

I'm not entirely sure that rem_euclid is the correct replacement but from the little bit of reading I've done to refresh myself on modulus and related math I think it is suitable (plus the existing test suite still passes in addtion to the new test for this case).

Example Rust code to show differences:

#![allow(unused)]
fn main() {
    let ni = 47.0;
    let m: f64 = -28.0;
    println!("ni = {}", ni);
    println!("m = {}", m);
    println!("m % ni = {}", (m % ni));
    println!("m.rem_euclid(ni) = {}", m.rem_euclid(ni));
}

Which yields:

ni = 47
m = -28
m % ni = -28
m.rem_euclid(ni) = 19

Which is the same as Python %:

In [1]: -28 % 47
Out[1]: 19
wcampbell0x2a commented 2 years ago

Hey! Thanks for the fix.