How do I print a Rust floating-point number with all available precision?

后端 未结 4 1972
长情又很酷
长情又很酷 2021-01-17 17:47

I am implementing the CORDIC algorithm for the sin trigonometric function. In order to do this, I need to hardcode/calculate a bunch of arctangent values. Right

4条回答
  •  离开以前
    2021-01-17 18:02

    This answer was written for Rust 0.12.0 and doesn't apply to Rust 1.x.


    You can use std::f32::to_string to print all the digits.

    use std::f32;
    
    fn main() {
        let pi: f32 = 3.1415926536897932384626;
        let k1: f32 = 0.6072529350088812561694; // 1/k
    
        println!("pi is {}", f32::to_string(pi));
        println!("k1 is {}", f32::to_string(k1));
    }
    

    Output:

    pi is 3.1415927410125732421875
    k1 is 0.607252979278564453125
    

提交回复
热议问题