Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider changes to ang_round #9

Open
stonylohr opened this issue Jul 17, 2020 · 1 comment
Open

consider changes to ang_round #9

stonylohr opened this issue Jul 17, 2020 · 1 comment

Comments

@stonylohr
Copy link
Contributor

stonylohr commented Jul 17, 2020

Declare "z" as const (renaming to "Z" to keep name idiomatic).
Move check for zero-value "x" earlier, to avoid calculating values that will be discarded.

Putting it together might look something like...

pub fn ang_round(x: f64) -> f64 {
    // The makes the smallest gap in x = 1/16 - nextafter(1/16, 0) = 1/2^57
    // for reals = 0.7 pm on the earth if x is an angle in degrees.  (This
    // is about 1000 times more resolution than we get with angles around 90
    // degrees.)  We use this to avoid having to deal with near singular
    // cases when x is non-zero but tiny (e.g., 1.0e-200).
    const Z: f64 = 1.0 / 16.0;
    if x == 0.0 {
        0.0
    } else {
        let mut y = x.abs();
        // The compiler mustn't "simplify" z - (z - y) to y
        if y < Z {
            y = Z - (Z - y);
        }
        if x < 0.0 {
            -y
        } else {
            y
        }
    }
}
@michaelkirk
Copy link
Member

SGTM

This was referenced Jan 23, 2021
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

No branches or pull requests

2 participants