Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Handle negative m value#78

Merged
wcampbell0x2a merged 1 commit into
rsadsb:masterfrom
amacd31:bugfix/handle-negative-m
Jan 4, 2022
Merged

Handle negative m value#78
wcampbell0x2a merged 1 commit into
rsadsb:masterfrom
amacd31:bugfix/handle-negative-m

Conversation

@amacd31
Copy link
Copy Markdown
Contributor

@amacd31 amacd31 commented Jan 3, 2022

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

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
Copy link
Copy Markdown
Member

Hey! Thanks for the fix.

@wcampbell0x2a wcampbell0x2a merged commit fa97bde into rsadsb:master Jan 4, 2022
@amacd31 amacd31 deleted the bugfix/handle-negative-m branch February 26, 2024 22:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants