#no-std #numeric

no-std upcast-arithmetic

溢出时进行类型提升的算术

6个版本

0.1.2 2022年8月13日
0.1.1 2022年8月13日
0.0.2 2022年7月11日

1716 in 数学

MIT许可证

19KB
292

upcast-arithmetic

通过提升到具有更高限制的类型来处理类型限制算术的实用库。

示例

不使用 upcast_arithmetic

let a = u8::MAX;
let b = 2u8;

let modulo = u8::MAX;

let res = (a + b) % modulo;
assert_eq!(res, 2);

使用 upcast_arithmetic

use upcast_arithmetic::*;

let a = u8::MAX;
let b = 2u8;

let modulo = u8::MAX;

let res = a.upcast_add_mod(b, modulo);
assert_eq!(res, 2);

许可证:MIT


lib.rs:

Crates.io docs.rs GitHub license Rust

通过提升到具有更高限制的类型来处理类型限制算术的实用库。

示例

不使用 upcast_arithmetic(会panic)

let a = u8::MAX;
let b = 2u8;

let modulo = u8::MAX;

let res = (a + b) % modulo;
assert_eq!(res, 2);

使用 upcast_arithmetic

use upcast_arithmetic::*;

let a = u8::MAX;
let b = 2u8;

let modulo = u8::MAX;

let res = a.upcast_add_mod(b, modulo);
assert_eq!(res, 2);

性能

性能开销非常小。在基准测试中,与假设没有溢出相比,似乎只有可以忽略不计的性能损失。

no_std

该Crate完全兼容#![no_std]

不安全

没有不安全代码,并且设置了标志#![deny(unsafe_code)]

无运行时依赖