quickmaths/
lib.rs

1//! A collection of algorithims for generic mathematics in Rust.
2
3#![cfg_attr(not(feature = "std"), no_std)]
4
5mod digits;
6pub use digits::Digits;
7
8pub mod factor;
9pub use factor::Factor;
10
11pub mod fraction;
12
13pub mod integral;
14
15pub mod series;
16pub mod stats;
17
18pub mod poly;
19
20use num::{traits::real::Real, FromPrimitive, Integer, One};
21
22pub fn ldexp(x: u32, exp: u32) -> u32 {
23    x * 2u32.pow(exp)
24}
25
26pub fn epsilon<T: One + FromPrimitive + Real>() -> T {
27    T::one() * T::from_u8(2).unwrap().powi(1 - (0f64.digits() as i32))
28}
29
30/// ```
31/// use quickmaths::gcd;
32///
33/// assert_eq!(gcd([1, 2]), 1);
34/// assert_eq!(gcd([3, 6]), 3);
35/// ```
36pub fn gcd<I>(integers: I) -> I::Item
37where
38    I: IntoIterator,
39    I::Item: Integer,
40{
41    let mut iter = integers.into_iter();
42    iter.next()
43        .map(|i| iter.fold(i, |acc, i| acc.gcd(&i)))
44        .unwrap_or_else(|| I::Item::one())
45}