Extract bigint

This commit is contained in:
Łukasz Jan Niemier 2016-03-01 11:47:10 +01:00
parent f1a80857ee
commit 2176b7048c
6 changed files with 5234 additions and 60 deletions

View File

@ -1,44 +1,52 @@
[package]
name = "num"
version = "0.1.31"
authors = ["The Rust Project Developers"]
license = "MIT/Apache-2.0"
homepage = "https://github.com/rust-num/num"
repository = "https://github.com/rust-num/num"
description = "A collection of numeric types and traits for Rust, including bigint,\ncomplex, rational, range iterators, generic integers, and more!\n"
documentation = "http://rust-num.github.io/num"
homepage = "https://github.com/rust-num/num"
keywords = ["mathematics", "numerics"]
description = """
A collection of numeric types and traits for Rust, including bigint,
complex, rational, range iterators, generic integers, and more!
"""
[dependencies]
rand = { version = "0.3.8", optional = true }
rustc-serialize = { version = "0.3.13", optional = true }
serde = { version = "^0.7.0", optional = true }
[dependencies.num-traits]
path = "./traits"
[dependencies.num-integer]
path = "./integer"
[dev-dependencies]
# Some tests of non-rand functionality still use rand because the tests
# themselves are randomized.
rand = { version = "0.3.8" }
[features]
complex = []
rational = []
bigint = []
default = ["bigint", "complex", "rand", "rational", "rustc-serialize"]
license = "MIT/Apache-2.0"
name = "num"
repository = "https://github.com/rust-num/num"
version = "0.1.31"
[[bench]]
name = "bigint"
[[bench]]
name = "shootout-pidigits"
harness = false
name = "shootout-pidigits"
[dependencies]
[dependencies.num-bigint]
optional = false
path = "bigint"
[dependencies.num-integer]
path = "./integer"
[dependencies.num-traits]
path = "./traits"
[dependencies.rand]
optional = true
version = "0.3.8"
[dependencies.rustc-serialize]
optional = true
version = "0.3.13"
[dependencies.serde]
optional = true
version = "^0.7.0"
[dev-dependencies]
[dev-dependencies.rand]
version = "0.3.8"
[features]
bigint = []
complex = []
default = ["bigint", "complex", "rand", "rational", "rustc-serialize"]
rational = []

22
bigint/Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
authors = ["Łukasz Jan Niemier <lukasz@niemier.pl>"]
name = "num-bigint"
version = "0.1.0"
[dependencies]
[dependencies.num-integer]
optional = false
path = "../integer"
[dependencies.num-traits]
optional = false
path = "../traits"
[dependencies.rand]
optional = true
version = "0.3.14"
[dependencies.serde]
optional = true
version = "0.7.0"

5129
bigint/src/lib.rs Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,11 +14,7 @@ extern crate num_traits as traits;
use traits::{Num, Signed};
pub trait Integer
: Sized
+ Num
+ PartialOrd + Ord + Eq
{
pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
/// Floored integer division.
///
/// # Examples
@ -162,19 +158,37 @@ pub trait Integer
}
/// Simultaneous integer division and modulus
#[inline] pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) { x.div_rem(&y) }
#[inline]
pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) {
x.div_rem(&y)
}
/// Floored integer division
#[inline] pub fn div_floor<T: Integer>(x: T, y: T) -> T { x.div_floor(&y) }
#[inline]
pub fn div_floor<T: Integer>(x: T, y: T) -> T {
x.div_floor(&y)
}
/// Floored integer modulus
#[inline] pub fn mod_floor<T: Integer>(x: T, y: T) -> T { x.mod_floor(&y) }
#[inline]
pub fn mod_floor<T: Integer>(x: T, y: T) -> T {
x.mod_floor(&y)
}
/// Simultaneous floored integer division and modulus
#[inline] pub fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) { x.div_mod_floor(&y) }
#[inline]
pub fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) {
x.div_mod_floor(&y)
}
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The
/// result is always positive.
#[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
#[inline(always)]
pub fn gcd<T: Integer>(x: T, y: T) -> T {
x.gcd(&y)
}
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
#[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
#[inline(always)]
pub fn lcm<T: Integer>(x: T, y: T) -> T {
x.lcm(&y)
}
macro_rules! impl_integer_for_isize {
($T:ty, $test_mod:ident) => (
@ -470,11 +484,11 @@ macro_rules! impl_integer_for_isize {
)
}
impl_integer_for_isize!(i8, test_integer_i8);
impl_integer_for_isize!(i16, test_integer_i16);
impl_integer_for_isize!(i32, test_integer_i32);
impl_integer_for_isize!(i64, test_integer_i64);
impl_integer_for_isize!(isize, test_integer_isize);
impl_integer_for_isize!(i8, test_integer_i8);
impl_integer_for_isize!(i16, test_integer_i16);
impl_integer_for_isize!(i32, test_integer_i32);
impl_integer_for_isize!(i64, test_integer_i64);
impl_integer_for_isize!(isize, test_integer_isize);
macro_rules! impl_integer_for_usize {
($T:ty, $test_mod:ident) => (
@ -641,8 +655,8 @@ macro_rules! impl_integer_for_usize {
)
}
impl_integer_for_usize!(u8, test_integer_u8);
impl_integer_for_usize!(u16, test_integer_u16);
impl_integer_for_usize!(u32, test_integer_u32);
impl_integer_for_usize!(u64, test_integer_u64);
impl_integer_for_usize!(u8, test_integer_u8);
impl_integer_for_usize!(u16, test_integer_u16);
impl_integer_for_usize!(u32, test_integer_u32);
impl_integer_for_usize!(u64, test_integer_u64);
impl_integer_for_usize!(usize, test_integer_usize);

View File

@ -59,6 +59,7 @@
extern crate num_traits;
extern crate num_integer;
extern crate num_bigint;
#[cfg(feature = "rustc-serialize")]
extern crate rustc_serialize;
@ -91,7 +92,7 @@ pub use traits::{Num, Zero, One, Signed, Unsigned, Bounded,
use std::ops::{Mul};
#[cfg(feature = "bigint")]
pub mod bigint;
pub mod bigint { pub use num_bigint::*; }
pub mod complex;
pub mod integer { pub use num_integer::*; }
pub mod iter;

View File

@ -34,16 +34,16 @@ pub trait Num: PartialEq + Zero + One
+ Add<Output = Self> + Sub<Output = Self>
+ Mul<Output = Self> + Div<Output = Self> + Rem<Output = Self>
{
type Error;
type FromStrRadixErr;
/// Convert from a string and radix <= 36.
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::Error>;
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>;
}
macro_rules! int_trait_impl {
($name:ident for $($t:ty)*) => ($(
impl $name for $t {
type Error = ::std::num::ParseIntError;
type FromStrRadixErr = ::std::num::ParseIntError;
fn from_str_radix(s: &str, radix: u32)
-> Result<Self, ::std::num::ParseIntError>
{
@ -65,10 +65,10 @@ pub struct ParseFloatError {
macro_rules! float_trait_impl {
($name:ident for $($t:ty)*) => ($(
impl $name for $t {
type Error = ParseFloatError;
type FromStrRadixErr = ParseFloatError;
fn from_str_radix(src: &str, radix: u32)
-> Result<Self, Self::Error>
-> Result<Self, Self::FromStrRadixErr>
{
use self::FloatErrorKind::*;
use self::ParseFloatError as PFE;