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

后端 未结 4 2006
长情又很酷
长情又很酷 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:27

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


    You can use the to_string function in std::f32 (not to be confused with the to_string method):

    fn main() {
        println!("{}", std::f32::to_string(unsafe { std::mem::transmute::(1) }));
        println!("{}", std::f32::to_string(unsafe { std::mem::transmute::(16) }));
        println!("{}", std::f32::to_string(std::f32::MIN_POS_VALUE));
        println!("{}", std::f32::to_string(std::f32::MAX_VALUE));
        println!("{}", std::f32::to_string(std::f32::consts::PI));
    }
    

    Output:

    0.00000000000000000000000000000000000000000000140129852294921875
    0.000000000000000000000000000000000000000000022420775890350341796875
    0.000000000000000000000000000000000000011754944324493408203125
    340282368002860660002286082464244022240
    3.1415927410125732421875
    

提交回复
热议问题