Update docs, more tests
All checks were successful
timw4mail/rusty-numbers/pipeline/head This commit looks good

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() {
let param = format!("{},{}", input.0, input.1);
group.bench_with_input(BenchmarkId::new("Binary", &param), input, |b, input| {
b.iter(|| u128::gcd(black_box(input.0), black_box(input.1)))
group.bench_with_input(BenchmarkId::new("Binary", &param), input, |bench, input| {
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| {
b.iter(|| u128::e_gcd(black_box(input.0), black_box(input.1)))
group.bench_with_input(BenchmarkId::new("Euclid", &param), input, |bench, input| {
let (a, b) = input;
bench.iter(|| u128::e_gcd(black_box(*a), black_box(*b)))
});
}
group.finish();

View File

@ -9,19 +9,19 @@ pub mod num;
pub mod rational;
/// 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.
///
/// Example:
/// ```rust
/// use rusty_numbers::fibonacci;
/// use rusty_numbers::mem_fibonacci;
///
/// let valid = fibonacci(45); // Some(1134903170)
/// # assert_eq!(1134903170, fibonacci(45).unwrap());
/// let valid = mem_fibonacci(45); // Some(1134903170)
/// # assert_eq!(1134903170, mem_fibonacci(45).unwrap());
/// # assert!(valid.is_some());
///
/// let invalid = fibonacci(187); // None
/// let invalid = mem_fibonacci(187); // None
/// # assert!(invalid.is_none());
/// ```
#[inline]
@ -31,12 +31,9 @@ pub fn mem_fibonacci(n: usize) -> Option<u128> {
table[1] = 1;
table[2] = 1;
_mem_fibonacci(n, &mut table)
}
/// Actual calculating function for `fibonacci`
#[inline]
fn _mem_fibonacci(n: usize, table: &mut [u128]) -> Option<u128> {
fn f(n: usize, table: &mut [u128]) -> Option<u128> {
if n < 2 {
// The first values are predefined.
return Some(table[n]);
@ -50,8 +47,8 @@ fn _mem_fibonacci(n: usize, table: &mut [u128]) -> Option<u128> {
// The lookup array starts out zeroed, so a zero
// is a not yet calculated value
0 => {
let a = _mem_fibonacci(n - 1, table)?;
let b = _mem_fibonacci(n - 2, table)?;
let a = f(n - 1, table)?;
let b = f(n - 2, table)?;
table[n] = a + b;
@ -61,6 +58,9 @@ fn _mem_fibonacci(n: usize, table: &mut [u128]) -> Option<u128> {
}
}
f(n, &mut table)
}
/// Calculate a number in the fibonacci sequence,
/// using naive recursion
///
@ -82,9 +82,20 @@ pub fn rec_fibonacci(n: usize) -> Option<u128> {
}
/// 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]
pub fn fibonacci(n: usize) -> Option<u128> {
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
/// 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);
/// Convert to an unsigned number

View File

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