georust / geographiclib-rs

A port of geographiclib in Rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

consider changes to ang_round

stonylohr opened this issue · comments

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
        }
    }
}