不想说啥,感觉直接看图比较好

难点1是求这个级数。
求出级数之后,就是这个和等于m的方程求解。

资料:维基百科
TIM图片20181201005650.jpg

fn solve(m: f64) -> f64 {
    let sum1: f64 = ((2f64 * m + 1f64) + (((2f64 * m + 1f64).powi(2) - 4f64 * (m.powi(2))) as f64).sqrt()) / (2f64 * m);
    let sum2: f64 = ((2f64 * m + 1f64) - (((2f64 * m + 1f64).powi(2) - 4f64 * (m.powi(2))) as f64).sqrt()) / (2f64 * m);

    if sum1.is_normal() && sum1 < 1f64 && sum1 > 0f64 {
        return sum1;
    } else {
        return sum2;
    }
}

fn main() {
    println!("Hello, world!");
}


#[cfg(test)]
mod tests {
    use super::*;

    fn assert_fuzzy(m: f64, expect: f64) -> () {
        let merr = 1e-12;
        println!("{:?}", m);
        let actual = solve(m);
        println!("Actual {:e}", actual);
        println!("Expect {:e}", expect);
        let inrange = (actual - expect).abs() <= merr;
        if inrange == false {
            println!("Expected value near: {:e} but got: {:e}", actual, expect);
        }
        assert_eq!(inrange, true);
    }


    #[test]
    fn basic_tests() {
        assert_fuzzy(2.00, 5.000000000000e-01);
        assert_fuzzy(4.00, 6.096117967978e-01);
        assert_fuzzy(5.00, 6.417424305044e-01);
    }
}

标签: none

添加新评论