相关疑难解决方法(0)

如何在 Rust 中解码和编码浮点数?

我想在 Rust 中解码、存储和编码浮点数。我知道num::Float::integer_decode(),但我不想失去任何精度。也就是说,除非我编码的格式当然小于我编码的格式。

floating-point encode decode rust

4
推荐指数
1
解决办法
5661
查看次数

为什么相同的 32 位浮点数在 JavaScript 和 Rust 中不同?

在 JavaScript 中,38_579_240_960转换为 32 位浮点数时不会改变:

console.log(new Float32Array([38_579_240_960])[0]); // 38579240960
Run Code Online (Sandbox Code Playgroud)

在 Rust 中,它被四舍五入为38579240000. 怎么来的?

fn main() {
     println!("{}", 38_579_240_960_f32);` // 38579240000
}
Run Code Online (Sandbox Code Playgroud)

javascript floating-point precision rust

4
推荐指数
2
解决办法
183
查看次数

Rust 浮点数通过泛型格式化为整数

我有以下 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)

generics rust

0
推荐指数
1
解决办法
127
查看次数

标签 统计

rust ×3

floating-point ×2

decode ×1

encode ×1

generics ×1

javascript ×1

precision ×1