Extract bigint
This commit is contained in:
parent
f1a80857ee
commit
2176b7048c
76
Cargo.toml
76
Cargo.toml
|
@ -1,44 +1,52 @@
|
||||||
[package]
|
[package]
|
||||||
|
|
||||||
name = "num"
|
|
||||||
version = "0.1.31"
|
|
||||||
authors = ["The Rust Project Developers"]
|
authors = ["The Rust Project Developers"]
|
||||||
license = "MIT/Apache-2.0"
|
description = "A collection of numeric types and traits for Rust, including bigint,\ncomplex, rational, range iterators, generic integers, and more!\n"
|
||||||
homepage = "https://github.com/rust-num/num"
|
|
||||||
repository = "https://github.com/rust-num/num"
|
|
||||||
documentation = "http://rust-num.github.io/num"
|
documentation = "http://rust-num.github.io/num"
|
||||||
|
homepage = "https://github.com/rust-num/num"
|
||||||
keywords = ["mathematics", "numerics"]
|
keywords = ["mathematics", "numerics"]
|
||||||
description = """
|
license = "MIT/Apache-2.0"
|
||||||
A collection of numeric types and traits for Rust, including bigint,
|
name = "num"
|
||||||
complex, rational, range iterators, generic integers, and more!
|
repository = "https://github.com/rust-num/num"
|
||||||
"""
|
version = "0.1.31"
|
||||||
|
|
||||||
[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"]
|
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "bigint"
|
name = "bigint"
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "shootout-pidigits"
|
|
||||||
harness = false
|
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 = []
|
||||||
|
|
|
@ -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"
|
File diff suppressed because it is too large
Load Diff
|
@ -14,11 +14,7 @@ extern crate num_traits as traits;
|
||||||
|
|
||||||
use traits::{Num, Signed};
|
use traits::{Num, Signed};
|
||||||
|
|
||||||
pub trait Integer
|
pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
|
||||||
: Sized
|
|
||||||
+ Num
|
|
||||||
+ PartialOrd + Ord + Eq
|
|
||||||
{
|
|
||||||
/// Floored integer division.
|
/// Floored integer division.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
|
@ -162,19 +158,37 @@ pub trait Integer
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Simultaneous integer division and modulus
|
/// 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
|
/// 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
|
/// 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
|
/// 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
|
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The
|
||||||
/// result is always positive.
|
/// 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`.
|
/// 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 {
|
macro_rules! impl_integer_for_isize {
|
||||||
($T:ty, $test_mod:ident) => (
|
($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!(i8, test_integer_i8);
|
||||||
impl_integer_for_isize!(i16, test_integer_i16);
|
impl_integer_for_isize!(i16, test_integer_i16);
|
||||||
impl_integer_for_isize!(i32, test_integer_i32);
|
impl_integer_for_isize!(i32, test_integer_i32);
|
||||||
impl_integer_for_isize!(i64, test_integer_i64);
|
impl_integer_for_isize!(i64, test_integer_i64);
|
||||||
impl_integer_for_isize!(isize, test_integer_isize);
|
impl_integer_for_isize!(isize, test_integer_isize);
|
||||||
|
|
||||||
macro_rules! impl_integer_for_usize {
|
macro_rules! impl_integer_for_usize {
|
||||||
($T:ty, $test_mod:ident) => (
|
($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!(u8, test_integer_u8);
|
||||||
impl_integer_for_usize!(u16, test_integer_u16);
|
impl_integer_for_usize!(u16, test_integer_u16);
|
||||||
impl_integer_for_usize!(u32, test_integer_u32);
|
impl_integer_for_usize!(u32, test_integer_u32);
|
||||||
impl_integer_for_usize!(u64, test_integer_u64);
|
impl_integer_for_usize!(u64, test_integer_u64);
|
||||||
impl_integer_for_usize!(usize, test_integer_usize);
|
impl_integer_for_usize!(usize, test_integer_usize);
|
||||||
|
|
|
@ -59,6 +59,7 @@
|
||||||
|
|
||||||
extern crate num_traits;
|
extern crate num_traits;
|
||||||
extern crate num_integer;
|
extern crate num_integer;
|
||||||
|
extern crate num_bigint;
|
||||||
|
|
||||||
#[cfg(feature = "rustc-serialize")]
|
#[cfg(feature = "rustc-serialize")]
|
||||||
extern crate rustc_serialize;
|
extern crate rustc_serialize;
|
||||||
|
@ -91,7 +92,7 @@ pub use traits::{Num, Zero, One, Signed, Unsigned, Bounded,
|
||||||
use std::ops::{Mul};
|
use std::ops::{Mul};
|
||||||
|
|
||||||
#[cfg(feature = "bigint")]
|
#[cfg(feature = "bigint")]
|
||||||
pub mod bigint;
|
pub mod bigint { pub use num_bigint::*; }
|
||||||
pub mod complex;
|
pub mod complex;
|
||||||
pub mod integer { pub use num_integer::*; }
|
pub mod integer { pub use num_integer::*; }
|
||||||
pub mod iter;
|
pub mod iter;
|
||||||
|
|
|
@ -34,16 +34,16 @@ pub trait Num: PartialEq + Zero + One
|
||||||
+ Add<Output = Self> + Sub<Output = Self>
|
+ Add<Output = Self> + Sub<Output = Self>
|
||||||
+ Mul<Output = Self> + Div<Output = Self> + Rem<Output = Self>
|
+ Mul<Output = Self> + Div<Output = Self> + Rem<Output = Self>
|
||||||
{
|
{
|
||||||
type Error;
|
type FromStrRadixErr;
|
||||||
|
|
||||||
/// Convert from a string and radix <= 36.
|
/// 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 {
|
macro_rules! int_trait_impl {
|
||||||
($name:ident for $($t:ty)*) => ($(
|
($name:ident for $($t:ty)*) => ($(
|
||||||
impl $name for $t {
|
impl $name for $t {
|
||||||
type Error = ::std::num::ParseIntError;
|
type FromStrRadixErr = ::std::num::ParseIntError;
|
||||||
fn from_str_radix(s: &str, radix: u32)
|
fn from_str_radix(s: &str, radix: u32)
|
||||||
-> Result<Self, ::std::num::ParseIntError>
|
-> Result<Self, ::std::num::ParseIntError>
|
||||||
{
|
{
|
||||||
|
@ -65,10 +65,10 @@ pub struct ParseFloatError {
|
||||||
macro_rules! float_trait_impl {
|
macro_rules! float_trait_impl {
|
||||||
($name:ident for $($t:ty)*) => ($(
|
($name:ident for $($t:ty)*) => ($(
|
||||||
impl $name for $t {
|
impl $name for $t {
|
||||||
type Error = ParseFloatError;
|
type FromStrRadixErr = ParseFloatError;
|
||||||
|
|
||||||
fn from_str_radix(src: &str, radix: u32)
|
fn from_str_radix(src: &str, radix: u32)
|
||||||
-> Result<Self, Self::Error>
|
-> Result<Self, Self::FromStrRadixErr>
|
||||||
{
|
{
|
||||||
use self::FloatErrorKind::*;
|
use self::FloatErrorKind::*;
|
||||||
use self::ParseFloatError as PFE;
|
use self::ParseFloatError as PFE;
|
||||||
|
|
Loading…
Reference in New Issue