1 个稳定版本
1.0.0 | 2019年2月2日 |
---|
#1404 在 算法 中
每月 下载 52 次
11KB
128 行
模运算
版本:Rust-2018
模运算是一种整数算术系统,其中数字在达到某个特定值时会“环绕” —— 这被称为 模数。一个常见的例子是时钟上的值,它们会环绕模数 12(对于12小时制时钟)。
有关模运算的更多信息,请参阅 维基百科 上的文章。
此crate允许创建和使用此类数字。
1. 使用方法
要使用此crate,从crates.io或github.com导入它,并按以下示例使用。
Cargo.toml
[dependencies]
modulo = "~1"
main.rs
// Create a new modulo number from an integer, given a modulus
let mod_num = 76.to_modulo(7);
let mod_num2 = 45.to_modulo(16);
// This is equivalent to using the modulo! macro
let mod_mac = modulo!(76, 7);
let mod_mac2 = modulo!(78, 16);
// Check equality
assert!(mod_num == mod_mac);
assert!(mod_num2 != mod_mac2);
// Addition
assert_eq!(mod_num + mod_mac, modulo!(5, 7));
// Subtraction
assert_eq!(mod_mac2 - mod_num2, modulo!(0, 16));
// Multipication
assert_eq!(mod_num * mod_mac, modulo!(1, 7));
// Congruence
assert!(76.is_congruent(41, 7));
更多示例可以在示例文件夹中找到。
2. 安装
对于开发,您可以在此仓库上进行分叉,或直接从github/gitlab克隆。仓库中包含在 /examples
目录中的使用示例。
$> cd <work_dir>
$> git clone [email protected]:rust-algorithms/modular.git
$> cd modulo
$> cargo build && cargo test
$> cargo run --example fib
3. 许可证
MIT