2个版本
0.1.1 | 2024年4月14日 |
---|---|
0.1.0 | 2024年4月14日 |
#1147 在 数学
12KB
307 行
fixedpoint
具有基础类型和精度除数的定点数。
通常这是10的幂,但您也可以做其他事情,比如256来使用低字节等。大概编译器会优化操作以使用移位而不是除法/乘法。
// use it directly
let x = Fixed<u8, 10>::from(5.3f64);
println!("{}", x * 2); // 10.6
// have a wrapper that controls what operations can be done
// useful for units of measurement that have special semantics
// for example, multiplying lengths gives an area.
struct Length(Fixed<u16, 100>);
struct Area(Fixed<u32, 100>);
impl Mul for Length {
type Output = Area;
fn mul(self, rhs: Self) -> Area {
Area((self.0 * rhs.0).into())
}
}
依赖关系
~150KB