#measure #floating-point #math #value #deviation #representation #datatype

allowance

Rust 数据类型,用于表示度量的偏差,避免浮点数精度问题。

3 个版本 (破坏性更新)

0.10.0 2022年6月28日
0.9.0 2022年3月22日
0.8.0 2022年3月6日

算法 中排名 1873

MIT 许可证

70KB
2K SLoC

Allowance

在 Rust 中表示物理上需要的允许偏差的数学表示,避免浮点数精度问题。

基于类型 Measure,精度为 1/10 微米(= 0.1μ)。

示例

use allowance::AllowanceValue;

fn main() {
    let width1 = AllowanceValue::new(100.0, 0.05, -0.2);
    let width2 = AllowanceValue::with_sym(50.0, 0.05);

    // Adding two `AllowancesValue`s is strait-forth.
    assert_eq!(width1 + width2, AllowanceValue::new(150.0, 0.1, -0.25));

    // `!` inverts the direction of tolerance to /subtract/ measures.
    assert_eq!(!width1, AllowanceValue::new(-100.0, 0.2, -0.05));

    // Adding an inverted `AllowanceValue` wides the tolerance.
    assert_eq!(width1 + !width1, AllowanceValue::new(0.0, 0.25, -0.25));
}

限制

Allowance

128 位宽的值。基于 Measure(64 位)的值可以处理的大小为 +/-922_337_203 km,偏差为 +/-214 m(Measure32)。

Allowance64

基于 Measure32(32 位)的值可以处理的大小为 +/-214 m,偏差为 +/-3 mm(Measure16)。

无运行时依赖