modtype

Build Status codecov Crates.io

This crate provides modular arithmetic integer types.

Usage

[modtype::ModType]

```

[modtype::use_modtype]

type F = modtype::F<1_000_000_007u64>;

asserteq!((F(1000000006) + F(2)).to_string(), "1"); ```

[modtype::thread_local::ModType]

```

[allow(nonsnakecase)]

modtype::threadlocal::F::with(1000000007u64, |F| { asserteq!((F(1000000006) + F(2)).to_string(), "1"); }); ```

[modtype::non_static::ModType]

```

[allow(nonsnakecase)]

let F = modtype::nonstatic::F::factory(1000000007u64);

asserteq!((F(1000000006) + F(2)).to_string(), "1"); ```

Customization

ModTypes can be customized via [modtype::Cartridge].

[modtype::cartridges::AllowFlexibleRhs]

``` use modtype::cartridges::{AllowFlexibleRhs, Field}; use num::{BigInt, BigRational};

[modtype::use_modtype]

type F = modtype::ModType>, 1000000_007u64>;

let mut x = F(1); x += F(1); x += 1u64; x += 1i32; x += 1f64; x += BigInt::from(1u32); x += BigRational::new(BigInt::from(1u32), BigInt::from(1u32)); assert_eq!(x, F(7)); ```

[modtype::cartridges::Multiplicative]

``` use num::CheckedDiv as _;

[modtype::use_modtype]

type Z = modtype::ModType, 57u32>;

asserteq!(Z(56) * Z(56), Z(1)); asserteq!(Z(1).checked_div(&Z(13)), Some(Z(22))); // 13・22 ≡ 1 (mod 57) ```

[modtype::cartridges::Additive]

``` use num::CheckedAdd as _;

[modtype::use_modtype]

type Z = modtype::ModType, 1000000_007u64>;

let mut x = Z(1000000_006);

x += Z(1); asserteq!(*x.getmutunchecked(), 1000000007);

x += Z(u64::maxvalue() / 2 - 1000000007); asserteq!(*x.getmutunchecked(), u64::maxvalue() / 2);

x += Z(1); asserteq!(*x.getmutunchecked(), (u64::maxvalue() / 2 + 1) % 1000000_007); ```

[modtype::cartridges::ManuallyAdjust]

``` use num::CheckedAdd as _;

[modtype::use_modtype]

type Z = modtype::ModType, 1000000_007u64>;

let mut x = Z(1000000_006);

x += Z(u64::maxvalue() - 1000000006); asserteq!(*x.getmutunchecked(), u64::maxvalue());

x.adjust(); asserteq!(*x.getmutunchecked(), u64::maxvalue() % 1000000_007); ```