Update docs, more tests
timw4mail/rusty-numbers/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2020-02-24 13:49:09 -05:00
parent bf09388604
commit 074f2bf662
4 changed files with 58 additions and 37 deletions

View File

@ -53,11 +53,13 @@ fn bench_gcd(c: &mut Criterion) {
for input in [(small_1, small), (med_1, med), (max_1, max)].iter() { for input in [(small_1, small), (med_1, med), (max_1, max)].iter() {
let param = format!("{},{}", input.0, input.1); let param = format!("{},{}", input.0, input.1);
group.bench_with_input(BenchmarkId::new("Binary", &param), input, |b, input| { group.bench_with_input(BenchmarkId::new("Binary", &param), input, |bench, input| {
b.iter(|| u128::gcd(black_box(input.0), black_box(input.1))) let (a, b) = input;
bench.iter(|| u128::gcd(black_box(*a), black_box(*b)))
}); });
group.bench_with_input(BenchmarkId::new("Euclid", &param), input, |b, input| { group.bench_with_input(BenchmarkId::new("Euclid", &param), input, |bench, input| {
b.iter(|| u128::e_gcd(black_box(input.0), black_box(input.1))) let (a, b) = input;
bench.iter(|| u128::e_gcd(black_box(*a), black_box(*b)))
}); });
} }
group.finish(); group.finish();

View File

@ -9,19 +9,19 @@ pub mod num;
pub mod rational; pub mod rational;
/// Calculate a number in the fibonacci sequence, /// Calculate a number in the fibonacci sequence,
/// using a lookup table for better worst-case performance. /// using recursion and a lookup table
/// ///
/// Can calculate up to 186 using native unsigned 128 bit integers. /// Can calculate up to 186 using native unsigned 128 bit integers.
/// ///
/// Example: /// Example:
/// ```rust /// ```rust
/// use rusty_numbers::fibonacci; /// use rusty_numbers::mem_fibonacci;
/// ///
/// let valid = fibonacci(45); // Some(1134903170) /// let valid = mem_fibonacci(45); // Some(1134903170)
/// # assert_eq!(1134903170, fibonacci(45).unwrap()); /// # assert_eq!(1134903170, mem_fibonacci(45).unwrap());
/// # assert!(valid.is_some()); /// # assert!(valid.is_some());
/// ///
/// let invalid = fibonacci(187); // None /// let invalid = mem_fibonacci(187); // None
/// # assert!(invalid.is_none()); /// # assert!(invalid.is_none());
/// ``` /// ```
#[inline] #[inline]
@ -31,34 +31,34 @@ pub fn mem_fibonacci(n: usize) -> Option<u128> {
table[1] = 1; table[1] = 1;
table[2] = 1; table[2] = 1;
_mem_fibonacci(n, &mut table) /// Actual calculating function for `fibonacci`
} #[inline]
fn f(n: usize, table: &mut [u128]) -> Option<u128> {
/// Actual calculating function for `fibonacci` if n < 2 {
#[inline] // The first values are predefined.
fn _mem_fibonacci(n: usize, table: &mut [u128]) -> Option<u128> { return Some(table[n]);
if n < 2 { }
// The first values are predefined.
return Some(table[n]); if n > 186 {
} return None;
}
if n > 186 {
return None; match table[n] {
} // The lookup array starts out zeroed, so a zero
// is a not yet calculated value
match table[n] { 0 => {
// The lookup array starts out zeroed, so a zero let a = f(n - 1, table)?;
// is a not yet calculated value let b = f(n - 2, table)?;
0 => {
let a = _mem_fibonacci(n - 1, table)?; table[n] = a + b;
let b = _mem_fibonacci(n - 2, table)?;
Some(table[n])
table[n] = a + b; }
x => Some(x),
Some(table[n])
} }
x => Some(x),
} }
f(n, &mut table)
} }
/// Calculate a number in the fibonacci sequence, /// Calculate a number in the fibonacci sequence,
@ -82,9 +82,20 @@ pub fn rec_fibonacci(n: usize) -> Option<u128> {
} }
/// Calculate a number in the fibonacci sequence, /// Calculate a number in the fibonacci sequence,
/// using iteration
/// ///
/// Can calculate up to 186 /// Can calculate up to 186 using native unsigned 128 bit integers.
///
/// Example:
/// ```rust
/// use rusty_numbers::fibonacci;
///
/// let valid = fibonacci(45); // Some(1134903170)
/// # assert_eq!(1134903170, fibonacci(45).unwrap());
/// # assert!(valid.is_some());
///
/// let invalid = fibonacci(187); // None
/// # assert!(invalid.is_none());
/// ```
#[inline] #[inline]
pub fn fibonacci(n: usize) -> Option<u128> { pub fn fibonacci(n: usize) -> Option<u128> {
let mut a: u128 = 0; let mut a: u128 = 0;

View File

@ -91,6 +91,8 @@ pub trait Int:
/// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic /// Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic
/// overflow would occur. If an overflow would have occurred then the wrapped value is returned. /// overflow would occur. If an overflow would have occurred then the wrapped value is returned.
///
/// The wrapped value is the amount less than the minimum value as a positive number
fn left_overflowing_sub(self, rhs: Self) -> (Self, bool); fn left_overflowing_sub(self, rhs: Self) -> (Self, bool);
/// Convert to an unsigned number /// Convert to an unsigned number

View File

@ -1,6 +1,7 @@
#![cfg_attr(tarpaulin, skip)] #![cfg_attr(tarpaulin, skip)]
use rusty_numbers::frac; use rusty_numbers::frac;
use rusty_numbers::rational::Frac;
#[test] #[test]
fn mul_test() { fn mul_test() {
@ -64,6 +65,11 @@ fn zero_denom() {
frac!(1 / 0); frac!(1 / 0);
} }
#[test]
fn fraction_reducing() {
assert_eq!(frac!(1u8/2), Frac::new_unreduced(48u8, 96u8).reduce());
}
#[test] #[test]
fn op_assign() { fn op_assign() {
// Addition // Addition