Some renaming and minor style fixes

This commit is contained in:
Timothy Warren 2022-02-11 15:48:41 -05:00
parent 1f15ff93f5
commit 37d8b1f5e8
5 changed files with 47 additions and 39 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "rusty-numbers"
version = "0.2.0"
authors = ["Timothy J. Warren <tim@timshomepage.net>"]
authors = ["Timothy J. Warren <tim@timshome.page>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -13,10 +13,13 @@ clean:
rm cobertura.xml
rm coverage.html
fmt:
cargo +nightly fmt
test:
cargo test
test-no-std:
cargo test --no-default-features --features alloc
.PHONY: test test-no-std
.PHONY: test test-no-std fmt

7
rustfmt.toml Normal file
View File

@ -0,0 +1,7 @@
unstable_features = true
format_code_in_doc_comments = true
format_macro_matchers = true
format_strings = false
imports_granularity = "Module"
group_imports = "StdExternalCrate"
use_field_init_shorthand = true

View File

@ -1,4 +1,3 @@
#![allow(unused_variables)]
//! \[WIP\] Arbitrarily large integers
use crate::num::FracOp;
use crate::num::Sign::{self, Negative, Positive};
@ -9,19 +8,17 @@ extern crate alloc;
use alloc::string::*;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::*;
#[cfg(feature = "std")]
use std::prelude::v1::*;
use core::cmp::{Ordering, PartialEq, PartialOrd};
use core::convert::TryInto;
use core::mem::replace;
use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
};
use core::usize;
#[cfg(feature = "std")]
use std::prelude::v1::*;
#[derive(Clone, Debug, PartialEq)]
/// The representation of a `BigInt`
pub struct BigInt {
inner: Vec<usize>,
sign: Sign,
@ -73,11 +70,15 @@ impl BigInt {
///
/// The various `From` implementations are more useful in most cases
#[must_use]
pub fn new() -> Self {
pub fn zero() -> Self {
Self::default()
}
fn new_empty() -> Self {
pub fn new(initial: impl Into<BigInt>) -> Self {
initial.into()
}
fn empty() -> Self {
Self {
inner: Vec::new(),
sign: Sign::Positive,
@ -140,8 +141,7 @@ impl BigInt {
/// # Panics
/// * If radix is not between 1 and 36 inclusive
/// * Some branches are not yet implemented
#[allow(clippy::needless_pass_by_value)]
pub fn from_str_radix<T: ToString>(s: T, radix: usize) -> BigInt {
pub fn from_str_radix<T: ToString + ?Sized>(s: &T, radix: usize) -> BigInt {
// Two lines due to borrow checker complaints
let input = s.to_string().to_ascii_uppercase();
let input = input.trim();
@ -188,6 +188,9 @@ impl BigInt {
todo!();
}
/// Get the larger number of digits when comparing two `BigInt`s.
/// This is helpful to determine sizing for a `BigInt` after
/// a numeric operation.
fn get_ceil_digit_count(a: &Self, b: &Self) -> usize {
let a_digits = a.inner.len();
let b_digits = b.inner.len();
@ -356,8 +359,6 @@ impl Mul for BigInt {
#[must_use]
fn mul(self, rhs: Self) -> Self::Output {
let input_digits = Self::get_ceil_digit_count(&self, &rhs);
// Multiplication can result in twice the number of digits
let out_digits = Self::get_ceil_digit_count(&self, &rhs) * 2;
@ -369,7 +370,7 @@ impl Div for BigInt {
type Output = Self;
#[must_use]
fn div(self, rhs: Self) -> Self::Output {
fn div(self, _rhs: Self) -> Self::Output {
todo!()
}
}
@ -378,42 +379,42 @@ impl Rem for BigInt {
type Output = Self;
#[must_use]
fn rem(self, rhs: Self) -> Self::Output {
fn rem(self, _rhs: Self) -> Self::Output {
todo!()
}
}
impl AddAssign for BigInt {
fn add_assign(&mut self, rhs: Self) {
let this = replace(self, BigInt::new());
let this = core::mem::replace(self, BigInt::zero());
*self = this + rhs;
}
}
impl SubAssign for BigInt {
fn sub_assign(&mut self, rhs: Self) {
let this = replace(self, BigInt::new());
let this = core::mem::replace(self, BigInt::zero());
*self = this - rhs;
}
}
impl MulAssign for BigInt {
fn mul_assign(&mut self, rhs: Self) {
let this = replace(self, BigInt::new());
let this = core::mem::replace(self, BigInt::zero());
*self = this * rhs;
}
}
impl DivAssign for BigInt {
fn div_assign(&mut self, rhs: Self) {
let this = replace(self, BigInt::new());
let this = core::mem::replace(self, BigInt::zero());
*self = this / rhs;
}
}
impl RemAssign for BigInt {
fn rem_assign(&mut self, rhs: Self) {
let this = replace(self, BigInt::new());
let this = core::mem::replace(self, BigInt::zero());
*self = this % rhs;
}
}
@ -510,7 +511,7 @@ macro_rules! impl_from_smaller {
impl From<$u> for BigInt {
/// Create a `BigInt` from an unsigned integer primitive
fn from(n: $u) -> Self {
let mut new = Self::new_empty();
let mut new = Self::empty();
new.inner.push(n as usize);
new
@ -823,7 +824,7 @@ mod tests {
let a = BigInt::from(core::usize::MAX);
let b = BigInt::from(5);
let product = a * b;
let _product = a * b;
}
#[test]
@ -841,7 +842,7 @@ mod tests {
let a = BigInt::from(128);
let b = BigInt::from(32);
let quotient = a / b;
let _quotient = a / b;
}
#[test]
@ -859,7 +860,7 @@ mod tests {
let a = BigInt::from(5);
let b = BigInt::from(2);
let rem = a % b;
let _rem = a % b;
}
#[test]
@ -873,8 +874,8 @@ mod tests {
#[test]
fn test_zeros() {
let a = BigInt::new();
let b = BigInt::new();
let a = BigInt::zero();
let b = BigInt::zero();
let c = a.clone() - b.clone();
assert_eq!(a.clone(), b.clone());
@ -978,7 +979,7 @@ mod tests {
#[test]
fn test_from_str_radix_1() {
let s = "1".repeat(32);
let num = BigInt::from_str_radix(s, 1);
let num = BigInt::from_str_radix(&s, 1);
assert_eq!(num.inner[0], 32_usize);
}
@ -988,7 +989,7 @@ mod tests {
let zeroes = "0".repeat(24);
let s = ones + &zeroes;
let num = BigInt::from_str_radix(s, 1);
let num = BigInt::from_str_radix(&s, 1);
assert_eq!(num.inner[0], 32_usize);
}

View File

@ -1,9 +1,10 @@
//! # Rational Numbers (fractions)
use core::cmp::{Ord, Ordering, PartialOrd};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use crate::num::Sign::{Negative, Positive};
use crate::num::{FracOp, Int, Sign, Unsigned};
use core::cmp::{Ord, Ordering, PartialOrd};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
/// Type representing a fraction
///
@ -79,9 +80,7 @@ impl<T: Unsigned> Frac<T> {
/// # Panics
/// if `d` is 0, this constructor will panic
pub fn new_unreduced<N: Int<Un = T>>(n: N, d: N) -> Frac<T> {
if d.is_zero() {
panic!("Fraction can not have a zero denominator");
}
assert!(!d.is_zero(), "Fraction can not have a zero denominator");
let mut sign = Positive;
@ -104,14 +103,12 @@ impl<T: Unsigned> Frac<T> {
}
}
/// Create a new rational from all the raw parts
/// Create a new, reduced rational from all the raw parts
///
/// # Panics
/// if `d` is 0, this constructor will panic
fn raw(n: T, d: T, s: Sign) -> Frac<T> {
if d.is_zero() {
panic!("Fraction can not have a zero denominator");
}
assert!(!d.is_zero(), "Fraction can not have a zero denominator");
Frac {
numerator: n,