我想在 Rust 中解码、存储和编码浮点数。我知道关于num::Float::integer_decode() https://docs.rs/num/0.1.36/num/trait.Float.html#tymethod.integer_decode但我不想失去any精确。也就是说,除非我编码的格式当然小于我编码的格式。
Rust 的新版本提供了比其他一些答案建议更安全的选择:
- 从 Rust 1.20 开始,您可以使用to_bits https://doc.rust-lang.org/std/primitive.f64.html#method.to_bits and 来自位 https://doc.rust-lang.org/std/primitive.f64.html#method.from_bits转换为和从
u64
二进制表示。
- 从 Rust 1.40 开始,您可以使用待字节数 https://doc.rust-lang.org/std/primitive.f64.html#method.to_be_bytes and from_be_bytes https://doc.rust-lang.org/std/primitive.f64.html#method.from_be_bytes处理
[u8; 8]
。 (还有用于小端字节顺序和本机字节顺序的方法。)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)