#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

#80 in Algorithms

Download history 25637/week @ 2024-07-30 23918/week @ 2024-08-06 21824/week @ 2024-08-13 25776/week @ 2024-08-20 28030/week @ 2024-08-27 33667/week @ 2024-09-03 24679/week @ 2024-09-10 23517/week @ 2024-09-17 28975/week @ 2024-09-24 27711/week @ 2024-10-01 27039/week @ 2024-10-08 32048/week @ 2024-10-15 29638/week @ 2024-10-22 32488/week @ 2024-10-29 30767/week @ 2024-11-05 25495/week @ 2024-11-12

124,018 downloads per month
Used in 171 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