Compare commits

..

No commits in common. "d6b0825b9a1a59aa4b98401f1b5aabbb4b959c67" and "cf262073dd27e6688368e6cbf7734a480dfbe4fe" have entirely different histories.

2 changed files with 18 additions and 76 deletions

View File

@ -6,15 +6,15 @@ use crate::num::*;
#[cfg(all(feature = "alloc", not(feature = "std")))]
extern crate alloc;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::string::*;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::*;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::string::*;
#[cfg(feature = "std")]
use std::prelude::v1::*;
use core::cmp::{Ordering, PartialEq, PartialOrd};
use core::convert::*;
use core::cmp::{Ordering, PartialOrd, PartialEq};
use core::convert::TryInto;
use core::mem::replace;
use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
@ -465,36 +465,7 @@ macro_rules! impl_from_larger {
impl From<$s> for BigInt {
/// Create a `BigInt` from a signed integer primitive
fn from(n: $s) -> Self {
use core::usize::MAX;
let target_radix: $s = (MAX as $s) + 1;
let sign = if n < 0 { Sign::Negative } else { Sign::Positive };
let n = n.abs();
let mut quotient = n / target_radix;
let mut rem = n % target_radix;
if quotient == 0 {
Self::from(rem as usize)
} else {
let mut inner: Vec<usize> = Vec::new();
inner.push(rem as usize);
loop {
rem = quotient % target_radix;
quotient = quotient / target_radix;
inner.push(rem as usize);
if (quotient == 0) {
break;
}
}
BigInt {
inner,
sign,
}
}
todo!();
}
}
@ -502,32 +473,14 @@ macro_rules! impl_from_larger {
/// Create a `BigInt` from an unsigned integer primitive
fn from(n: $u) -> Self {
use core::usize::MAX;
let target_radix: $u = (MAX as $u) + 1;
let mut quotient = n / target_radix;
let mut rem = n % target_radix;
let base_usize_value = n / MAX as $u;
let rem = n % MAX as $u;
if quotient == 0 {
if base_usize_value == 0 {
Self::from(rem as usize)
} else {
let mut inner: Vec<usize> = Vec::new();
inner.push(rem as usize);
loop {
rem = quotient % target_radix;
quotient = quotient / target_radix;
inner.push(rem as usize);
if (quotient == 0) {
break;
}
}
BigInt {
inner,
sign: Sign::Positive
}
todo!();
}
}
}
@ -576,16 +529,13 @@ impl_from_larger!((i128, u128));
impl_from_smaller!((i8, u8), (i16, u16), (i32, u32), (i64, u64));
// Implement PartialEq and PartialOrd to compare against BigInt values
impl_ord_literal!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
impl_ord_literal!(i8,u8,i16,u16,i32,u32,i64,u64,i128,u128);
#[cfg(test)]
#[cfg_attr(tarpaulin, skip)]
mod tests {
use super::*;
const RADIX: u128 = core::usize::MAX as u128 + 1;
const I_RADIX: i128 = core::usize::MAX as i128 + 1;
#[test]
fn sanity_checks() {
let int = BigInt::from(45u8);
@ -605,14 +555,14 @@ mod tests {
#[test]
fn test_trim_zeros() {
let mut lots_of_leading = BigInt {
let mut lotsoftrailing = BigInt {
inner: vec![1, 0, 0, 0, 0, 0, 0, 0, 0],
sign: Positive,
};
lots_of_leading.trim_zeros();
lotsoftrailing.trim_zeros();
assert_eq!(BigInt::from(1), lots_of_leading);
assert_eq!(BigInt::from(1), lotsoftrailing);
}
#[test]
@ -865,23 +815,15 @@ mod tests {
}
#[test]
#[should_panic]
fn test_from_large_unsigned() {
let big_num: u128 = 9 * RADIX + 8;
let res = BigInt::from(big_num);
assert_eq!(res.sign, Sign::Positive, "{:#?}", res);
assert_eq!(res.inner[0], 8usize, "{:#?}", res);
assert_eq!(res.inner[1], 9usize, "{:#?}", res);
BigInt::from(core::u128::MAX);
}
#[test]
#[should_panic]
fn test_from_large_signed() {
let big_num: i128 = 2 * I_RADIX + 3;
let res = BigInt::from(-big_num);
assert_eq!(res.sign, Sign::Negative, "{:#?}", res);
assert_eq!(res.inner[0], 3usize, "{:#?}", res);
assert_eq!(res.inner[1], 2usize, "{:#?}", res);
BigInt::from(128i128);
}
#[test]

View File

@ -65,7 +65,7 @@ impl PartialOrd for Sign {
Self::Negative => match other {
Self::Positive => Some(Ordering::Less),
Self::Negative => Some(Ordering::Equal),
},
}
}
}
}