#integer-arithmetic #arithmetic #integer #numeric

no-std muldiv

Provides a trait for numeric types to perform combined multiplication and division with overflow protection

6 releases (2 stable)

1.0.1 Oct 28, 2022
1.0.0 Oct 13, 2020
0.2.1 Dec 29, 2019
0.2.0 May 11, 2018
0.1.0 Feb 25, 2016

#67 in Algorithms

Download history 21116/week @ 2024-06-16 21261/week @ 2024-06-23 26817/week @ 2024-06-30 27336/week @ 2024-07-07 27007/week @ 2024-07-14 25781/week @ 2024-07-21 26159/week @ 2024-07-28 24405/week @ 2024-08-04 22444/week @ 2024-08-11 24430/week @ 2024-08-18 26691/week @ 2024-08-25 33901/week @ 2024-09-01 26792/week @ 2024-09-08 22613/week @ 2024-09-15 28009/week @ 2024-09-22 25969/week @ 2024-09-29

106,866 downloads per month
Used in 168 crates (5 directly)

MIT license

18KB
300 lines

rust-muldiv crates.io Actions Status docs.rs

Provides a trait for numeric types to perform combined multiplication and division with overflow protection.

LICENSE

rust-muldiv is licensed under the MIT license (LICENSE or http://opensource.org/licenses/MIT).

Contribution

Any kinds of contributions are welcome as a pull request.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in rust-muldiv by you shall be licensed under the MIT license as above, without any additional terms or conditions.


lib.rs:

Provides a trait for numeric types to perform combined multiplication and division with overflow protection.

The MulDiv trait provides functions for performing combined multiplication and division for numeric types and comes with implementations for all the primitive integer types. Three variants with different rounding characteristics are provided: mul_div_floor(), mul_div_round() and mul_div_ceil().

Example

extern crate muldiv;
use muldiv::MulDiv;
// Calculates 127 * 23 / 42 rounded down
let x = 127u8.mul_div_floor(23, 42);
assert_eq!(x, Some(69));

No runtime deps