我想在 Rust 中解码、存储和编码浮点数。我知道num::Float::integer_decode(),但我不想失去任何精度。也就是说,除非我编码的格式当然小于我编码的格式。
在 JavaScript 中,38_579_240_960转换为 32 位浮点数时不会改变:
console.log(new Float32Array([38_579_240_960])[0]); // 38579240960Run Code Online (Sandbox Code Playgroud)
但在 Rust 中,它被四舍五入为38579240000. 怎么来的?
fn main() {
println!("{}", 38_579_240_960_f32);` // 38579240000
}
Run Code Online (Sandbox Code Playgroud) 我有以下 Rust 代码:
#[derive(Debug)]
struct Point<T, U> {
x: T,
y: U
}
impl<T: std::fmt::Display, U: std::fmt::Display> Point<T, U> {
fn print(&self) {
println!("{} {}", &self.x, &self.y );
}
}
impl<X1, Y1> Point<X1, Y1> {
fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> {
Point {
x: self.x,
y: other.y,
}
}
}
fn main() {
let integer = Point {x: 1, y:2};
let float = Point{x: 2.0, y:3.0};
let floaty = integer.mixup(float);
println!("{:?}", floaty); // output: Point { …Run Code Online (Sandbox Code Playgroud)