commit 482f0e0b7421415f9961234d9aa6e1b8e8c3159a Author: Aaron Turon Date: Tue Sep 16 10:35:35 2014 -0700 Initial seeding from rust repo diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4fffb2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +/Cargo.lock diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7dba5d6 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +install: + - curl https://static.rust-lang.org/rustup.sh | sudo sh - +script: + - cargo build --verbose + - cargo test --verbose +env: + - LD_LIBRARY_PATH=/usr/local/lib diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3e4bae5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] + +name = "num" +version = "0.0.1" +authors = ["The Rust Project Developers"] + +[lib] + +name = "num" +plugin = true diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..16fe87b --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..39d4bdb --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2014 The Rust Project Developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/src/bigint.rs b/src/bigint.rs new file mode 100644 index 0000000..c97f951 --- /dev/null +++ b/src/bigint.rs @@ -0,0 +1,2956 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! A Big integer (signed version: `BigInt`, unsigned version: `BigUint`). +//! +//! A `BigUint` is represented as an array of `BigDigit`s. +//! A `BigInt` is a combination of `BigUint` and `Sign`. +//! +//! Common numerical operations are overloaded, so we can treat them +//! the same way we treat other numbers. +//! +//! ## Example +//! +//! ```rust +//! use num::bigint::BigUint; +//! use std::num::{Zero, One}; +//! use std::mem::replace; +//! +//! // Calculate large fibonacci numbers. +//! fn fib(n: uint) -> BigUint { +//! let mut f0: BigUint = Zero::zero(); +//! let mut f1: BigUint = One::one(); +//! for _ in range(0, n) { +//! let f2 = f0 + f1; +//! // This is a low cost way of swapping f0 with f1 and f1 with f2. +//! f0 = replace(&mut f1, f2); +//! } +//! f0 +//! } +//! +//! // This is a very large number. +//! println!("fib(1000) = {}", fib(1000)); +//! ``` +//! +//! It's easy to generate large random numbers: +//! +//! ```rust +//! use num::bigint::{ToBigInt, RandBigInt}; +//! use std::rand; +//! +//! let mut rng = rand::task_rng(); +//! let a = rng.gen_bigint(1000u); +//! +//! let low = -10000i.to_bigint().unwrap(); +//! let high = 10000i.to_bigint().unwrap(); +//! let b = rng.gen_bigint_range(&low, &high); +//! +//! // Probably an even larger number. +//! println!("{}", a * b); +//! ``` + +use Integer; +use rand::Rng; + +use std::{cmp, fmt, hash}; +use std::default::Default; +use std::from_str::FromStr; +use std::num::CheckedDiv; +use std::num::{ToPrimitive, FromPrimitive}; +use std::num::{Zero, One, ToStrRadix, FromStrRadix}; +use std::string::String; +use std::{uint, i64, u64}; + +/// A `BigDigit` is a `BigUint`'s composing element. +pub type BigDigit = u32; + +/// A `DoubleBigDigit` is the internal type used to do the computations. Its +/// size is the double of the size of `BigDigit`. +pub type DoubleBigDigit = u64; + +pub static ZERO_BIG_DIGIT: BigDigit = 0; +static ZERO_VEC: [BigDigit, ..1] = [ZERO_BIG_DIGIT]; + +#[allow(non_snake_case)] +pub mod BigDigit { + use super::BigDigit; + use super::DoubleBigDigit; + + // `DoubleBigDigit` size dependent + pub static bits: uint = 32; + + pub static base: DoubleBigDigit = 1 << bits; + static lo_mask: DoubleBigDigit = (-1 as DoubleBigDigit) >> bits; + + #[inline] + fn get_hi(n: DoubleBigDigit) -> BigDigit { (n >> bits) as BigDigit } + #[inline] + fn get_lo(n: DoubleBigDigit) -> BigDigit { (n & lo_mask) as BigDigit } + + /// Split one `DoubleBigDigit` into two `BigDigit`s. + #[inline] + pub fn from_doublebigdigit(n: DoubleBigDigit) -> (BigDigit, BigDigit) { + (get_hi(n), get_lo(n)) + } + + /// Join two `BigDigit`s into one `DoubleBigDigit` + #[inline] + pub fn to_doublebigdigit(hi: BigDigit, lo: BigDigit) -> DoubleBigDigit { + (lo as DoubleBigDigit) | ((hi as DoubleBigDigit) << bits) + } +} + +/// A big unsigned integer type. +/// +/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number +/// `(a + b * BigDigit::base + c * BigDigit::base^2)`. +#[deriving(Clone)] +pub struct BigUint { + data: Vec +} + +impl PartialEq for BigUint { + #[inline] + fn eq(&self, other: &BigUint) -> bool { + match self.cmp(other) { Equal => true, _ => false } + } +} +impl Eq for BigUint {} + +impl PartialOrd for BigUint { + #[inline] + fn partial_cmp(&self, other: &BigUint) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for BigUint { + #[inline] + fn cmp(&self, other: &BigUint) -> Ordering { + let (s_len, o_len) = (self.data.len(), other.data.len()); + if s_len < o_len { return Less; } + if s_len > o_len { return Greater; } + + for (&self_i, &other_i) in self.data.iter().rev().zip(other.data.iter().rev()) { + if self_i < other_i { return Less; } + if self_i > other_i { return Greater; } + } + return Equal; + } +} + +impl Default for BigUint { + #[inline] + fn default() -> BigUint { Zero::zero() } +} + +impl hash::Hash for BigUint { + fn hash(&self, state: &mut S) { + // hash 0 in case it's all 0's + 0u32.hash(state); + + let mut found_first_value = false; + for elem in self.data.iter().rev() { + // don't hash any leading 0's, they shouldn't affect the hash + if found_first_value || *elem != 0 { + found_first_value = true; + elem.hash(state); + } + } + } +} + +impl fmt::Show for BigUint { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_str_radix(10)) + } +} + +impl FromStr for BigUint { + #[inline] + fn from_str(s: &str) -> Option { + FromStrRadix::from_str_radix(s, 10) + } +} + +impl Num for BigUint {} + +impl BitAnd for BigUint { + fn bitand(&self, other: &BigUint) -> BigUint { + BigUint::new(self.data.iter().zip(other.data.iter()).map(|(ai, bi)| *ai & *bi).collect()) + } +} + +impl BitOr for BigUint { + fn bitor(&self, other: &BigUint) -> BigUint { + let zeros = ZERO_VEC.iter().cycle(); + let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; + let ored = a.data.iter().zip(b.data.iter().chain(zeros)).map( + |(ai, bi)| *ai | *bi + ).collect(); + return BigUint::new(ored); + } +} + +impl BitXor for BigUint { + fn bitxor(&self, other: &BigUint) -> BigUint { + let zeros = ZERO_VEC.iter().cycle(); + let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; + let xored = a.data.iter().zip(b.data.iter().chain(zeros)).map( + |(ai, bi)| *ai ^ *bi + ).collect(); + return BigUint::new(xored); + } +} + +impl Shl for BigUint { + #[inline] + fn shl(&self, rhs: &uint) -> BigUint { + let n_unit = *rhs / BigDigit::bits; + let n_bits = *rhs % BigDigit::bits; + return self.shl_unit(n_unit).shl_bits(n_bits); + } +} + +impl Shr for BigUint { + #[inline] + fn shr(&self, rhs: &uint) -> BigUint { + let n_unit = *rhs / BigDigit::bits; + let n_bits = *rhs % BigDigit::bits; + return self.shr_unit(n_unit).shr_bits(n_bits); + } +} + +impl Zero for BigUint { + #[inline] + fn zero() -> BigUint { BigUint::new(Vec::new()) } + + #[inline] + fn is_zero(&self) -> bool { self.data.is_empty() } +} + +impl One for BigUint { + #[inline] + fn one() -> BigUint { BigUint::new(vec!(1)) } +} + +impl Unsigned for BigUint {} + +impl Add for BigUint { + fn add(&self, other: &BigUint) -> BigUint { + let zeros = ZERO_VEC.iter().cycle(); + let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; + + let mut carry = 0; + let mut sum: Vec = a.data.iter().zip(b.data.iter().chain(zeros)).map(|(ai, bi)| { + let (hi, lo) = BigDigit::from_doublebigdigit( + (*ai as DoubleBigDigit) + (*bi as DoubleBigDigit) + (carry as DoubleBigDigit)); + carry = hi; + lo + }).collect(); + if carry != 0 { sum.push(carry); } + return BigUint::new(sum); + } +} + +impl Sub for BigUint { + fn sub(&self, other: &BigUint) -> BigUint { + let new_len = cmp::max(self.data.len(), other.data.len()); + let zeros = ZERO_VEC.iter().cycle(); + let (a, b) = (self.data.iter().chain(zeros.clone()), other.data.iter().chain(zeros)); + + let mut borrow = 0i; + let diff: Vec = a.take(new_len).zip(b).map(|(ai, bi)| { + let (hi, lo) = BigDigit::from_doublebigdigit( + BigDigit::base + + (*ai as DoubleBigDigit) + - (*bi as DoubleBigDigit) + - (borrow as DoubleBigDigit) + ); + /* + hi * (base) + lo == 1*(base) + ai - bi - borrow + => ai - bi - borrow < 0 <=> hi == 0 + */ + borrow = if hi == 0 { 1 } else { 0 }; + lo + }).collect(); + + assert!(borrow == 0, + "Cannot subtract other from self because other is larger than self."); + return BigUint::new(diff); + } +} + +impl Mul for BigUint { + fn mul(&self, other: &BigUint) -> BigUint { + if self.is_zero() || other.is_zero() { return Zero::zero(); } + + let (s_len, o_len) = (self.data.len(), other.data.len()); + if s_len == 1 { return mul_digit(other, self.data.as_slice()[0]); } + if o_len == 1 { return mul_digit(self, other.data.as_slice()[0]); } + + // Using Karatsuba multiplication + // (a1 * base + a0) * (b1 * base + b0) + // = a1*b1 * base^2 + + // (a1*b1 + a0*b0 - (a1-b0)*(b1-a0)) * base + + // a0*b0 + let half_len = cmp::max(s_len, o_len) / 2; + let (s_hi, s_lo) = cut_at(self, half_len); + let (o_hi, o_lo) = cut_at(other, half_len); + + let ll = s_lo * o_lo; + let hh = s_hi * o_hi; + let mm = { + let (s1, n1) = sub_sign(s_hi, s_lo); + let (s2, n2) = sub_sign(o_hi, o_lo); + match (s1, s2) { + (Equal, _) | (_, Equal) => hh + ll, + (Less, Greater) | (Greater, Less) => hh + ll + (n1 * n2), + (Less, Less) | (Greater, Greater) => hh + ll - (n1 * n2) + } + }; + + return ll + mm.shl_unit(half_len) + hh.shl_unit(half_len * 2); + + + fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint { + if n == 0 { return Zero::zero(); } + if n == 1 { return (*a).clone(); } + + let mut carry = 0; + let mut prod: Vec = a.data.iter().map(|ai| { + let (hi, lo) = BigDigit::from_doublebigdigit( + (*ai as DoubleBigDigit) * (n as DoubleBigDigit) + (carry as DoubleBigDigit) + ); + carry = hi; + lo + }).collect(); + if carry != 0 { prod.push(carry); } + return BigUint::new(prod); + } + + #[inline] + fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { + let mid = cmp::min(a.data.len(), n); + return (BigUint::from_slice(a.data.slice(mid, a.data.len())), + BigUint::from_slice(a.data.slice(0, mid))); + } + + #[inline] + fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) { + match a.cmp(&b) { + Less => (Less, b - a), + Greater => (Greater, a - b), + _ => (Equal, Zero::zero()) + } + } + } +} + +impl Div for BigUint { + #[inline] + fn div(&self, other: &BigUint) -> BigUint { + let (q, _) = self.div_rem(other); + return q; + } +} + +impl Rem for BigUint { + #[inline] + fn rem(&self, other: &BigUint) -> BigUint { + let (_, r) = self.div_rem(other); + return r; + } +} + +impl Neg for BigUint { + #[inline] + fn neg(&self) -> BigUint { fail!() } +} + +impl CheckedAdd for BigUint { + #[inline] + fn checked_add(&self, v: &BigUint) -> Option { + return Some(self.add(v)); + } +} + +impl CheckedSub for BigUint { + #[inline] + fn checked_sub(&self, v: &BigUint) -> Option { + if *self < *v { + return None; + } + return Some(self.sub(v)); + } +} + +impl CheckedMul for BigUint { + #[inline] + fn checked_mul(&self, v: &BigUint) -> Option { + return Some(self.mul(v)); + } +} + +impl CheckedDiv for BigUint { + #[inline] + fn checked_div(&self, v: &BigUint) -> Option { + if v.is_zero() { + return None; + } + return Some(self.div(v)); + } +} + +impl Integer for BigUint { + #[inline] + fn div_rem(&self, other: &BigUint) -> (BigUint, BigUint) { + self.div_mod_floor(other) + } + + #[inline] + fn div_floor(&self, other: &BigUint) -> BigUint { + let (d, _) = self.div_mod_floor(other); + return d; + } + + #[inline] + fn mod_floor(&self, other: &BigUint) -> BigUint { + let (_, m) = self.div_mod_floor(other); + return m; + } + + fn div_mod_floor(&self, other: &BigUint) -> (BigUint, BigUint) { + if other.is_zero() { fail!() } + if self.is_zero() { return (Zero::zero(), Zero::zero()); } + if *other == One::one() { return ((*self).clone(), Zero::zero()); } + + match self.cmp(other) { + Less => return (Zero::zero(), (*self).clone()), + Equal => return (One::one(), Zero::zero()), + Greater => {} // Do nothing + } + + let mut shift = 0; + let mut n = *other.data.last().unwrap(); + while n < (1 << BigDigit::bits - 2) { + n <<= 1; + shift += 1; + } + assert!(shift < BigDigit::bits); + let (d, m) = div_mod_floor_inner(self << shift, other << shift); + return (d, m >> shift); + + + fn div_mod_floor_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) { + let mut m = a; + let mut d: BigUint = Zero::zero(); + let mut n = 1; + while m >= b { + let (d0, d_unit, b_unit) = div_estimate(&m, &b, n); + let mut d0 = d0; + let mut prod = b * d0; + while prod > m { + // FIXME(#5992): assignment operator overloads + // d0 -= d_unit + d0 = d0 - d_unit; + // FIXME(#5992): assignment operator overloads + // prod -= b_unit; + prod = prod - b_unit + } + if d0.is_zero() { + n = 2; + continue; + } + n = 1; + // FIXME(#5992): assignment operator overloads + // d += d0; + d = d + d0; + // FIXME(#5992): assignment operator overloads + // m -= prod; + m = m - prod; + } + return (d, m); + } + + + fn div_estimate(a: &BigUint, b: &BigUint, n: uint) + -> (BigUint, BigUint, BigUint) { + if a.data.len() < n { + return (Zero::zero(), Zero::zero(), (*a).clone()); + } + + let an = a.data.tailn(a.data.len() - n); + let bn = *b.data.last().unwrap(); + let mut d = Vec::with_capacity(an.len()); + let mut carry = 0; + for elt in an.iter().rev() { + let ai = BigDigit::to_doublebigdigit(carry, *elt); + let di = ai / (bn as DoubleBigDigit); + assert!(di < BigDigit::base); + carry = (ai % (bn as DoubleBigDigit)) as BigDigit; + d.push(di as BigDigit) + } + d.reverse(); + + let shift = (a.data.len() - an.len()) - (b.data.len() - 1); + if shift == 0 { + return (BigUint::new(d), One::one(), (*b).clone()); + } + let one: BigUint = One::one(); + return (BigUint::new(d).shl_unit(shift), + one.shl_unit(shift), + b.shl_unit(shift)); + } + } + + /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. + /// + /// The result is always positive. + #[inline] + fn gcd(&self, other: &BigUint) -> BigUint { + // Use Euclid's algorithm + let mut m = (*self).clone(); + let mut n = (*other).clone(); + while !m.is_zero() { + let temp = m; + m = n % temp; + n = temp; + } + return n; + } + + /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. + #[inline] + fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other)) } + + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &BigUint) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() } + + /// Returns `true` if the number is divisible by `2`. + #[inline] + fn is_even(&self) -> bool { + // Considering only the last digit. + match self.data.as_slice().head() { + Some(x) => x.is_even(), + None => true + } + } + + /// Returns `true` if the number is not divisible by `2`. + #[inline] + fn is_odd(&self) -> bool { !self.is_even() } +} + +impl ToPrimitive for BigUint { + #[inline] + fn to_i64(&self) -> Option { + self.to_u64().and_then(|n| { + // If top bit of u64 is set, it's too large to convert to i64. + if n >> 63 == 0 { + Some(n as i64) + } else { + None + } + }) + } + + // `DoubleBigDigit` size dependent + #[inline] + fn to_u64(&self) -> Option { + match self.data.len() { + 0 => Some(0), + 1 => Some(self.data.as_slice()[0] as u64), + 2 => Some(BigDigit::to_doublebigdigit(self.data.as_slice()[1], self.data.as_slice()[0]) + as u64), + _ => None + } + } +} + +impl FromPrimitive for BigUint { + #[inline] + fn from_i64(n: i64) -> Option { + if n > 0 { + FromPrimitive::from_u64(n as u64) + } else if n == 0 { + Some(Zero::zero()) + } else { + None + } + } + + // `DoubleBigDigit` size dependent + #[inline] + fn from_u64(n: u64) -> Option { + let n = match BigDigit::from_doublebigdigit(n) { + (0, 0) => Zero::zero(), + (0, n0) => BigUint::new(vec!(n0)), + (n1, n0) => BigUint::new(vec!(n0, n1)) + }; + Some(n) + } +} + +/// A generic trait for converting a value to a `BigUint`. +pub trait ToBigUint { + /// Converts the value of `self` to a `BigUint`. + fn to_biguint(&self) -> Option; +} + +impl ToBigUint for BigInt { + #[inline] + fn to_biguint(&self) -> Option { + if self.sign == Plus { + Some(self.data.clone()) + } else if self.sign == Zero { + Some(Zero::zero()) + } else { + None + } + } +} + +impl ToBigUint for BigUint { + #[inline] + fn to_biguint(&self) -> Option { + Some(self.clone()) + } +} + +macro_rules! impl_to_biguint( + ($T:ty, $from_ty:path) => { + impl ToBigUint for $T { + #[inline] + fn to_biguint(&self) -> Option { + $from_ty(*self) + } + } + } +) + +impl_to_biguint!(int, FromPrimitive::from_int) +impl_to_biguint!(i8, FromPrimitive::from_i8) +impl_to_biguint!(i16, FromPrimitive::from_i16) +impl_to_biguint!(i32, FromPrimitive::from_i32) +impl_to_biguint!(i64, FromPrimitive::from_i64) +impl_to_biguint!(uint, FromPrimitive::from_uint) +impl_to_biguint!(u8, FromPrimitive::from_u8) +impl_to_biguint!(u16, FromPrimitive::from_u16) +impl_to_biguint!(u32, FromPrimitive::from_u32) +impl_to_biguint!(u64, FromPrimitive::from_u64) + +impl ToStrRadix for BigUint { + fn to_str_radix(&self, radix: uint) -> String { + assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]"); + let (base, max_len) = get_radix_base(radix); + if base == BigDigit::base { + return fill_concat(self.data.as_slice(), radix, max_len) + } + return fill_concat(convert_base(self, base).as_slice(), radix, max_len); + + fn convert_base(n: &BigUint, base: DoubleBigDigit) -> Vec { + let divider = base.to_biguint().unwrap(); + let mut result = Vec::new(); + let mut m = n.clone(); + while m >= divider { + let (d, m0) = m.div_mod_floor(÷r); + result.push(m0.to_uint().unwrap() as BigDigit); + m = d; + } + if !m.is_zero() { + result.push(m.to_uint().unwrap() as BigDigit); + } + return result; + } + + fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> String { + if v.is_empty() { + return "0".to_string() + } + let mut s = String::with_capacity(v.len() * l); + for n in v.iter().rev() { + let ss = (*n as uint).to_str_radix(radix); + s.push_str("0".repeat(l - ss.len()).as_slice()); + s.push_str(ss.as_slice()); + } + s.as_slice().trim_left_chars('0').to_string() + } + } +} + +impl FromStrRadix for BigUint { + /// Creates and initializes a `BigUint`. + #[inline] + fn from_str_radix(s: &str, radix: uint) -> Option { + BigUint::parse_bytes(s.as_bytes(), radix) + } +} + +impl BigUint { + /// Creates and initializes a `BigUint`. + /// + /// The digits are be in base 2^32. + #[inline] + pub fn new(mut digits: Vec) -> BigUint { + // omit trailing zeros + let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1); + digits.truncate(new_len); + BigUint { data: digits } + } + + /// Creates and initializes a `BigUint`. + /// + /// The digits are be in base 2^32. + #[inline] + pub fn from_slice(slice: &[BigDigit]) -> BigUint { + BigUint::new(Vec::from_slice(slice)) + } + + /// Creates and initializes a `BigUint`. + pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { + let (base, unit_len) = get_radix_base(radix); + let base_num = match base.to_biguint() { + Some(base_num) => base_num, + None => { return None; } + }; + + let mut end = buf.len(); + let mut n: BigUint = Zero::zero(); + let mut power: BigUint = One::one(); + loop { + let start = cmp::max(end, unit_len) - unit_len; + match uint::parse_bytes(buf.slice(start, end), radix) { + Some(d) => { + let d: Option = FromPrimitive::from_uint(d); + match d { + Some(d) => { + // FIXME(#5992): assignment operator overloads + // n += d * power; + n = n + d * power; + } + None => { return None; } + } + } + None => { return None; } + } + if end <= unit_len { + return Some(n); + } + end -= unit_len; + // FIXME(#5992): assignment operator overloads + // power *= base_num; + power = power * base_num; + } + } + + #[inline] + fn shl_unit(&self, n_unit: uint) -> BigUint { + if n_unit == 0 || self.is_zero() { return (*self).clone(); } + + BigUint::new(Vec::from_elem(n_unit, ZERO_BIG_DIGIT).append(self.data.as_slice())) + } + + #[inline] + fn shl_bits(&self, n_bits: uint) -> BigUint { + if n_bits == 0 || self.is_zero() { return (*self).clone(); } + + let mut carry = 0; + let mut shifted: Vec = self.data.iter().map(|elem| { + let (hi, lo) = BigDigit::from_doublebigdigit( + (*elem as DoubleBigDigit) << n_bits | (carry as DoubleBigDigit) + ); + carry = hi; + lo + }).collect(); + if carry != 0 { shifted.push(carry); } + return BigUint::new(shifted); + } + + #[inline] + fn shr_unit(&self, n_unit: uint) -> BigUint { + if n_unit == 0 { return (*self).clone(); } + if self.data.len() < n_unit { return Zero::zero(); } + return BigUint::from_slice( + self.data.slice(n_unit, self.data.len()) + ); + } + + #[inline] + fn shr_bits(&self, n_bits: uint) -> BigUint { + if n_bits == 0 || self.data.is_empty() { return (*self).clone(); } + + let mut borrow = 0; + let mut shifted_rev = Vec::with_capacity(self.data.len()); + for elem in self.data.iter().rev() { + shifted_rev.push((*elem >> n_bits) | borrow); + borrow = *elem << (BigDigit::bits - n_bits); + } + let shifted = { shifted_rev.reverse(); shifted_rev }; + return BigUint::new(shifted); + } + + /// Determines the fewest bits necessary to express the `BigUint`. + pub fn bits(&self) -> uint { + if self.is_zero() { return 0; } + let zeros = self.data.last().unwrap().leading_zeros(); + return self.data.len()*BigDigit::bits - zeros; + } +} + +// `DoubleBigDigit` size dependent +#[inline] +fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) { + match radix { + 2 => (4294967296, 32), + 3 => (3486784401, 20), + 4 => (4294967296, 16), + 5 => (1220703125, 13), + 6 => (2176782336, 12), + 7 => (1977326743, 11), + 8 => (1073741824, 10), + 9 => (3486784401, 10), + 10 => (1000000000, 9), + 11 => (2357947691, 9), + 12 => (429981696, 8), + 13 => (815730721, 8), + 14 => (1475789056, 8), + 15 => (2562890625, 8), + 16 => (4294967296, 8), + _ => fail!("The radix must be within (1, 16]") + } +} + +/// A Sign is a `BigInt`'s composing element. +#[deriving(PartialEq, PartialOrd, Eq, Ord, Clone, Show)] +pub enum Sign { Minus, Zero, Plus } + +impl Neg for Sign { + /// Negate Sign value. + #[inline] + fn neg(&self) -> Sign { + match *self { + Minus => Plus, + Zero => Zero, + Plus => Minus + } + } +} + +/// A big signed integer type. +#[deriving(Clone)] +pub struct BigInt { + sign: Sign, + data: BigUint +} + +impl PartialEq for BigInt { + #[inline] + fn eq(&self, other: &BigInt) -> bool { + self.cmp(other) == Equal + } +} + +impl Eq for BigInt {} + +impl PartialOrd for BigInt { + #[inline] + fn partial_cmp(&self, other: &BigInt) -> Option { + Some(self.cmp(other)) + } +} + +impl Ord for BigInt { + #[inline] + fn cmp(&self, other: &BigInt) -> Ordering { + let scmp = self.sign.cmp(&other.sign); + if scmp != Equal { return scmp; } + + match self.sign { + Zero => Equal, + Plus => self.data.cmp(&other.data), + Minus => other.data.cmp(&self.data), + } + } +} + +impl Default for BigInt { + #[inline] + fn default() -> BigInt { Zero::zero() } +} + +impl fmt::Show for BigInt { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_str_radix(10)) + } +} + +impl hash::Hash for BigInt { + fn hash(&self, state: &mut S) { + (self.sign == Plus).hash(state); + self.data.hash(state); + } +} + +impl FromStr for BigInt { + #[inline] + fn from_str(s: &str) -> Option { + FromStrRadix::from_str_radix(s, 10) + } +} + +impl Num for BigInt {} + +impl Shl for BigInt { + #[inline] + fn shl(&self, rhs: &uint) -> BigInt { + BigInt::from_biguint(self.sign, self.data << *rhs) + } +} + +impl Shr for BigInt { + #[inline] + fn shr(&self, rhs: &uint) -> BigInt { + BigInt::from_biguint(self.sign, self.data >> *rhs) + } +} + +impl Zero for BigInt { + #[inline] + fn zero() -> BigInt { + BigInt::from_biguint(Zero, Zero::zero()) + } + + #[inline] + fn is_zero(&self) -> bool { self.sign == Zero } +} + +impl One for BigInt { + #[inline] + fn one() -> BigInt { + BigInt::from_biguint(Plus, One::one()) + } +} + +impl Signed for BigInt { + #[inline] + fn abs(&self) -> BigInt { + match self.sign { + Plus | Zero => self.clone(), + Minus => BigInt::from_biguint(Plus, self.data.clone()) + } + } + + #[inline] + fn abs_sub(&self, other: &BigInt) -> BigInt { + if *self <= *other { Zero::zero() } else { *self - *other } + } + + #[inline] + fn signum(&self) -> BigInt { + match self.sign { + Plus => BigInt::from_biguint(Plus, One::one()), + Minus => BigInt::from_biguint(Minus, One::one()), + Zero => Zero::zero(), + } + } + + #[inline] + fn is_positive(&self) -> bool { self.sign == Plus } + + #[inline] + fn is_negative(&self) -> bool { self.sign == Minus } +} + +impl Add for BigInt { + #[inline] + fn add(&self, other: &BigInt) -> BigInt { + match (self.sign, other.sign) { + (Zero, _) => other.clone(), + (_, Zero) => self.clone(), + (Plus, Plus) => BigInt::from_biguint(Plus, self.data + other.data), + (Plus, Minus) => self - (-*other), + (Minus, Plus) => other - (-*self), + (Minus, Minus) => -((-self) + (-*other)) + } + } +} + +impl Sub for BigInt { + #[inline] + fn sub(&self, other: &BigInt) -> BigInt { + match (self.sign, other.sign) { + (Zero, _) => -other, + (_, Zero) => self.clone(), + (Plus, Plus) => match self.data.cmp(&other.data) { + Less => BigInt::from_biguint(Minus, other.data - self.data), + Greater => BigInt::from_biguint(Plus, self.data - other.data), + Equal => Zero::zero() + }, + (Plus, Minus) => self + (-*other), + (Minus, Plus) => -((-self) + *other), + (Minus, Minus) => (-other) - (-*self) + } + } +} + +impl Mul for BigInt { + #[inline] + fn mul(&self, other: &BigInt) -> BigInt { + match (self.sign, other.sign) { + (Zero, _) | (_, Zero) => Zero::zero(), + (Plus, Plus) | (Minus, Minus) => { + BigInt::from_biguint(Plus, self.data * other.data) + }, + (Plus, Minus) | (Minus, Plus) => { + BigInt::from_biguint(Minus, self.data * other.data) + } + } + } +} + +impl Div for BigInt { + #[inline] + fn div(&self, other: &BigInt) -> BigInt { + let (q, _) = self.div_rem(other); + q + } +} + +impl Rem for BigInt { + #[inline] + fn rem(&self, other: &BigInt) -> BigInt { + let (_, r) = self.div_rem(other); + r + } +} + +impl Neg for BigInt { + #[inline] + fn neg(&self) -> BigInt { + BigInt::from_biguint(self.sign.neg(), self.data.clone()) + } +} + +impl CheckedAdd for BigInt { + #[inline] + fn checked_add(&self, v: &BigInt) -> Option { + return Some(self.add(v)); + } +} + +impl CheckedSub for BigInt { + #[inline] + fn checked_sub(&self, v: &BigInt) -> Option { + return Some(self.sub(v)); + } +} + +impl CheckedMul for BigInt { + #[inline] + fn checked_mul(&self, v: &BigInt) -> Option { + return Some(self.mul(v)); + } +} + +impl CheckedDiv for BigInt { + #[inline] + fn checked_div(&self, v: &BigInt) -> Option { + if v.is_zero() { + return None; + } + return Some(self.div(v)); + } +} + + +impl Integer for BigInt { + #[inline] + fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) { + // r.sign == self.sign + let (d_ui, r_ui) = self.data.div_mod_floor(&other.data); + let d = BigInt::from_biguint(Plus, d_ui); + let r = BigInt::from_biguint(Plus, r_ui); + match (self.sign, other.sign) { + (_, Zero) => fail!(), + (Plus, Plus) | (Zero, Plus) => ( d, r), + (Plus, Minus) | (Zero, Minus) => (-d, r), + (Minus, Plus) => (-d, -r), + (Minus, Minus) => ( d, -r) + } + } + + #[inline] + fn div_floor(&self, other: &BigInt) -> BigInt { + let (d, _) = self.div_mod_floor(other); + d + } + + #[inline] + fn mod_floor(&self, other: &BigInt) -> BigInt { + let (_, m) = self.div_mod_floor(other); + m + } + + fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) { + // m.sign == other.sign + let (d_ui, m_ui) = self.data.div_rem(&other.data); + let d = BigInt::from_biguint(Plus, d_ui); + let m = BigInt::from_biguint(Plus, m_ui); + match (self.sign, other.sign) { + (_, Zero) => fail!(), + (Plus, Plus) | (Zero, Plus) => (d, m), + (Plus, Minus) | (Zero, Minus) => if m.is_zero() { + (-d, Zero::zero()) + } else { + (-d - One::one(), m + *other) + }, + (Minus, Plus) => if m.is_zero() { + (-d, Zero::zero()) + } else { + (-d - One::one(), other - m) + }, + (Minus, Minus) => (d, -m) + } + } + + /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. + /// + /// The result is always positive. + #[inline] + fn gcd(&self, other: &BigInt) -> BigInt { + BigInt::from_biguint(Plus, self.data.gcd(&other.data)) + } + + /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. + #[inline] + fn lcm(&self, other: &BigInt) -> BigInt { + BigInt::from_biguint(Plus, self.data.lcm(&other.data)) + } + + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &BigInt) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) } + + /// Returns `true` if the number is divisible by `2`. + #[inline] + fn is_even(&self) -> bool { self.data.is_even() } + + /// Returns `true` if the number is not divisible by `2`. + #[inline] + fn is_odd(&self) -> bool { self.data.is_odd() } +} + +impl ToPrimitive for BigInt { + #[inline] + fn to_i64(&self) -> Option { + match self.sign { + Plus => self.data.to_i64(), + Zero => Some(0), + Minus => { + self.data.to_u64().and_then(|n| { + let m: u64 = 1 << 63; + if n < m { + Some(-(n as i64)) + } else if n == m { + Some(i64::MIN) + } else { + None + } + }) + } + } + } + + #[inline] + fn to_u64(&self) -> Option { + match self.sign { + Plus => self.data.to_u64(), + Zero => Some(0), + Minus => None + } + } +} + +impl FromPrimitive for BigInt { + #[inline] + fn from_i64(n: i64) -> Option { + if n > 0 { + FromPrimitive::from_u64(n as u64).and_then(|n| { + Some(BigInt::from_biguint(Plus, n)) + }) + } else if n < 0 { + FromPrimitive::from_u64(u64::MAX - (n as u64) + 1).and_then( + |n| { + Some(BigInt::from_biguint(Minus, n)) + }) + } else { + Some(Zero::zero()) + } + } + + #[inline] + fn from_u64(n: u64) -> Option { + if n == 0 { + Some(Zero::zero()) + } else { + FromPrimitive::from_u64(n).and_then(|n| { + Some(BigInt::from_biguint(Plus, n)) + }) + } + } +} + +/// A generic trait for converting a value to a `BigInt`. +pub trait ToBigInt { + /// Converts the value of `self` to a `BigInt`. + fn to_bigint(&self) -> Option; +} + +impl ToBigInt for BigInt { + #[inline] + fn to_bigint(&self) -> Option { + Some(self.clone()) + } +} + +impl ToBigInt for BigUint { + #[inline] + fn to_bigint(&self) -> Option { + if self.is_zero() { + Some(Zero::zero()) + } else { + Some(BigInt { sign: Plus, data: self.clone() }) + } + } +} + +macro_rules! impl_to_bigint( + ($T:ty, $from_ty:path) => { + impl ToBigInt for $T { + #[inline] + fn to_bigint(&self) -> Option { + $from_ty(*self) + } + } + } +) + +impl_to_bigint!(int, FromPrimitive::from_int) +impl_to_bigint!(i8, FromPrimitive::from_i8) +impl_to_bigint!(i16, FromPrimitive::from_i16) +impl_to_bigint!(i32, FromPrimitive::from_i32) +impl_to_bigint!(i64, FromPrimitive::from_i64) +impl_to_bigint!(uint, FromPrimitive::from_uint) +impl_to_bigint!(u8, FromPrimitive::from_u8) +impl_to_bigint!(u16, FromPrimitive::from_u16) +impl_to_bigint!(u32, FromPrimitive::from_u32) +impl_to_bigint!(u64, FromPrimitive::from_u64) + +impl ToStrRadix for BigInt { + #[inline] + fn to_str_radix(&self, radix: uint) -> String { + match self.sign { + Plus => self.data.to_str_radix(radix), + Zero => "0".to_string(), + Minus => format!("-{}", self.data.to_str_radix(radix)), + } + } +} + +impl FromStrRadix for BigInt { + /// Creates and initializes a BigInt. + #[inline] + fn from_str_radix(s: &str, radix: uint) -> Option { + BigInt::parse_bytes(s.as_bytes(), radix) + } +} + +pub trait RandBigInt { + /// Generate a random `BigUint` of the given bit size. + fn gen_biguint(&mut self, bit_size: uint) -> BigUint; + + /// Generate a random BigInt of the given bit size. + fn gen_bigint(&mut self, bit_size: uint) -> BigInt; + + /// Generate a random `BigUint` less than the given bound. Fails + /// when the bound is zero. + fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint; + + /// Generate a random `BigUint` within the given range. The lower + /// bound is inclusive; the upper bound is exclusive. Fails when + /// the upper bound is not greater than the lower bound. + fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; + + /// Generate a random `BigInt` within the given range. The lower + /// bound is inclusive; the upper bound is exclusive. Fails when + /// the upper bound is not greater than the lower bound. + fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; +} + +impl RandBigInt for R { + fn gen_biguint(&mut self, bit_size: uint) -> BigUint { + let (digits, rem) = bit_size.div_rem(&BigDigit::bits); + let mut data = Vec::with_capacity(digits+1); + for _ in range(0, digits) { + data.push(self.gen()); + } + if rem > 0 { + let final_digit: BigDigit = self.gen(); + data.push(final_digit >> (BigDigit::bits - rem)); + } + BigUint::new(data) + } + + fn gen_bigint(&mut self, bit_size: uint) -> BigInt { + // Generate a random BigUint... + let biguint = self.gen_biguint(bit_size); + // ...and then randomly assign it a Sign... + let sign = if biguint.is_zero() { + // ...except that if the BigUint is zero, we need to try + // again with probability 0.5. This is because otherwise, + // the probability of generating a zero BigInt would be + // double that of any other number. + if self.gen() { + return self.gen_bigint(bit_size); + } else { + Zero + } + } else if self.gen() { + Plus + } else { + Minus + }; + BigInt::from_biguint(sign, biguint) + } + + fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint { + assert!(!bound.is_zero()); + let bits = bound.bits(); + loop { + let n = self.gen_biguint(bits); + if n < *bound { return n; } + } + } + + fn gen_biguint_range(&mut self, + lbound: &BigUint, + ubound: &BigUint) + -> BigUint { + assert!(*lbound < *ubound); + return *lbound + self.gen_biguint_below(&(*ubound - *lbound)); + } + + fn gen_bigint_range(&mut self, + lbound: &BigInt, + ubound: &BigInt) + -> BigInt { + assert!(*lbound < *ubound); + let delta = (*ubound - *lbound).to_biguint().unwrap(); + return *lbound + self.gen_biguint_below(&delta).to_bigint().unwrap(); + } +} + +impl BigInt { + /// Creates and initializes a BigInt. + /// + /// The digits are be in base 2^32. + #[inline] + pub fn new(sign: Sign, digits: Vec) -> BigInt { + BigInt::from_biguint(sign, BigUint::new(digits)) + } + + /// Creates and initializes a `BigInt`. + /// + /// The digits are be in base 2^32. + #[inline] + pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt { + if sign == Zero || data.is_zero() { + return BigInt { sign: Zero, data: Zero::zero() }; + } + BigInt { sign: sign, data: data } + } + + /// Creates and initializes a `BigInt`. + #[inline] + pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt { + BigInt::from_biguint(sign, BigUint::from_slice(slice)) + } + + /// Creates and initializes a `BigInt`. + pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { + if buf.is_empty() { return None; } + let mut sign = Plus; + let mut start = 0; + if buf[0] == b'-' { + sign = Minus; + start = 1; + } + return BigUint::parse_bytes(buf.slice(start, buf.len()), radix) + .map(|bu| BigInt::from_biguint(sign, bu)); + } + + /// Converts this `BigInt` into a `BigUint`, if it's not negative. + #[inline] + pub fn to_biguint(&self) -> Option { + match self.sign { + Plus => Some(self.data.clone()), + Zero => Some(Zero::zero()), + Minus => None + } + } +} + +#[cfg(test)] +mod biguint_tests { + use Integer; + use super::{BigDigit, BigUint, ToBigUint}; + use super::{Plus, BigInt, RandBigInt, ToBigInt}; + + use std::cmp::{Less, Equal, Greater}; + use std::from_str::FromStr; + use std::i64; + use std::num::{Zero, One, FromStrRadix, ToStrRadix}; + use std::num::{ToPrimitive, FromPrimitive}; + use std::num::CheckedDiv; + use std::rand::task_rng; + use std::u64; + use std::hash::hash; + + #[test] + fn test_from_slice() { + fn check(slice: &[BigDigit], data: &[BigDigit]) { + assert!(data == BigUint::from_slice(slice).data.as_slice()); + } + check([1], [1]); + check([0, 0, 0], []); + check([1, 2, 0, 0], [1, 2]); + check([0, 0, 1, 2], [0, 0, 1, 2]); + check([0, 0, 1, 2, 0, 0], [0, 0, 1, 2]); + check([-1], [-1]); + } + + #[test] + fn test_cmp() { + let data: [&[_], ..7] = [ &[], &[1], &[2], &[-1], &[0, 1], &[2, 1], &[1, 1, 1] ]; + let data: Vec = data.iter().map(|v| BigUint::from_slice(*v)).collect(); + for (i, ni) in data.iter().enumerate() { + for (j0, nj) in data.slice(i, data.len()).iter().enumerate() { + let j = j0 + i; + if i == j { + assert_eq!(ni.cmp(nj), Equal); + assert_eq!(nj.cmp(ni), Equal); + assert_eq!(ni, nj); + assert!(!(ni != nj)); + assert!(ni <= nj); + assert!(ni >= nj); + assert!(!(ni < nj)); + assert!(!(ni > nj)); + } else { + assert_eq!(ni.cmp(nj), Less); + assert_eq!(nj.cmp(ni), Greater); + + assert!(!(ni == nj)); + assert!(ni != nj); + + assert!(ni <= nj); + assert!(!(ni >= nj)); + assert!(ni < nj); + assert!(!(ni > nj)); + + assert!(!(nj <= ni)); + assert!(nj >= ni); + assert!(!(nj < ni)); + assert!(nj > ni); + } + } + } + } + + #[test] + fn test_hash() { + let a = BigUint::new(vec!()); + let b = BigUint::new(vec!(0)); + let c = BigUint::new(vec!(1)); + let d = BigUint::new(vec!(1,0,0,0,0,0)); + let e = BigUint::new(vec!(0,0,0,0,0,1)); + assert!(hash(&a) == hash(&b)); + assert!(hash(&b) != hash(&c)); + assert!(hash(&c) == hash(&d)); + assert!(hash(&d) != hash(&e)); + } + + #[test] + fn test_bitand() { + fn check(left: &[BigDigit], + right: &[BigDigit], + expected: &[BigDigit]) { + assert_eq!(BigUint::from_slice(left) & BigUint::from_slice(right), + BigUint::from_slice(expected)); + } + check([], [], []); + check([268, 482, 17], + [964, 54], + [260, 34]); + } + + #[test] + fn test_bitor() { + fn check(left: &[BigDigit], + right: &[BigDigit], + expected: &[BigDigit]) { + assert_eq!(BigUint::from_slice(left) | BigUint::from_slice(right), + BigUint::from_slice(expected)); + } + check([], [], []); + check([268, 482, 17], + [964, 54], + [972, 502, 17]); + } + + #[test] + fn test_bitxor() { + fn check(left: &[BigDigit], + right: &[BigDigit], + expected: &[BigDigit]) { + assert_eq!(BigUint::from_slice(left) ^ BigUint::from_slice(right), + BigUint::from_slice(expected)); + } + check([], [], []); + check([268, 482, 17], + [964, 54], + [712, 468, 17]); + } + + #[test] + fn test_shl() { + fn check(s: &str, shift: uint, ans: &str) { + let opt_biguint: Option = FromStrRadix::from_str_radix(s, 16); + let bu = (opt_biguint.unwrap() << shift).to_str_radix(16); + assert_eq!(bu.as_slice(), ans); + } + + check("0", 3, "0"); + check("1", 3, "8"); + + check("1\ + 0000\ + 0000\ + 0000\ + 0001\ + 0000\ + 0000\ + 0000\ + 0001", + 3, + "8\ + 0000\ + 0000\ + 0000\ + 0008\ + 0000\ + 0000\ + 0000\ + 0008"); + check("1\ + 0000\ + 0001\ + 0000\ + 0001", + 2, + "4\ + 0000\ + 0004\ + 0000\ + 0004"); + check("1\ + 0001\ + 0001", + 1, + "2\ + 0002\ + 0002"); + + check("\ + 4000\ + 0000\ + 0000\ + 0000", + 3, + "2\ + 0000\ + 0000\ + 0000\ + 0000"); + check("4000\ + 0000", + 2, + "1\ + 0000\ + 0000"); + check("4000", + 2, + "1\ + 0000"); + + check("4000\ + 0000\ + 0000\ + 0000", + 67, + "2\ + 0000\ + 0000\ + 0000\ + 0000\ + 0000\ + 0000\ + 0000\ + 0000"); + check("4000\ + 0000", + 35, + "2\ + 0000\ + 0000\ + 0000\ + 0000"); + check("4000", + 19, + "2\ + 0000\ + 0000"); + + check("fedc\ + ba98\ + 7654\ + 3210\ + fedc\ + ba98\ + 7654\ + 3210", + 4, + "f\ + edcb\ + a987\ + 6543\ + 210f\ + edcb\ + a987\ + 6543\ + 2100"); + check("88887777666655554444333322221111", 16, + "888877776666555544443333222211110000"); + } + + #[test] + fn test_shr() { + fn check(s: &str, shift: uint, ans: &str) { + let opt_biguint: Option = + FromStrRadix::from_str_radix(s, 16); + let bu = (opt_biguint.unwrap() >> shift).to_str_radix(16); + assert_eq!(bu.as_slice(), ans); + } + + check("0", 3, "0"); + check("f", 3, "1"); + + check("1\ + 0000\ + 0000\ + 0000\ + 0001\ + 0000\ + 0000\ + 0000\ + 0001", + 3, + "2000\ + 0000\ + 0000\ + 0000\ + 2000\ + 0000\ + 0000\ + 0000"); + check("1\ + 0000\ + 0001\ + 0000\ + 0001", + 2, + "4000\ + 0000\ + 4000\ + 0000"); + check("1\ + 0001\ + 0001", + 1, + "8000\ + 8000"); + + check("2\ + 0000\ + 0000\ + 0000\ + 0001\ + 0000\ + 0000\ + 0000\ + 0001", + 67, + "4000\ + 0000\ + 0000\ + 0000"); + check("2\ + 0000\ + 0001\ + 0000\ + 0001", + 35, + "4000\ + 0000"); + check("2\ + 0001\ + 0001", + 19, + "4000"); + + check("1\ + 0000\ + 0000\ + 0000\ + 0000", + 1, + "8000\ + 0000\ + 0000\ + 0000"); + check("1\ + 0000\ + 0000", + 1, + "8000\ + 0000"); + check("1\ + 0000", + 1, + "8000"); + check("f\ + edcb\ + a987\ + 6543\ + 210f\ + edcb\ + a987\ + 6543\ + 2100", + 4, + "fedc\ + ba98\ + 7654\ + 3210\ + fedc\ + ba98\ + 7654\ + 3210"); + + check("888877776666555544443333222211110000", 16, + "88887777666655554444333322221111"); + } + + // `DoubleBigDigit` size dependent + #[test] + fn test_convert_i64() { + fn check(b1: BigUint, i: i64) { + let b2: BigUint = FromPrimitive::from_i64(i).unwrap(); + assert!(b1 == b2); + assert!(b1.to_i64().unwrap() == i); + } + + check(Zero::zero(), 0); + check(One::one(), 1); + check(i64::MAX.to_biguint().unwrap(), i64::MAX); + + check(BigUint::new(vec!( )), 0); + check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::bits))); + check(BigUint::new(vec!(-1 )), (1 << (1*BigDigit::bits)) - 1); + check(BigUint::new(vec!( 0, 1 )), (1 << (1*BigDigit::bits))); + check(BigUint::new(vec!(-1, -1 >> 1)), i64::MAX); + + assert_eq!(i64::MIN.to_biguint(), None); + assert_eq!(BigUint::new(vec!(-1, -1 )).to_i64(), None); + assert_eq!(BigUint::new(vec!( 0, 0, 1)).to_i64(), None); + assert_eq!(BigUint::new(vec!(-1, -1, -1)).to_i64(), None); + } + + // `DoubleBigDigit` size dependent + #[test] + fn test_convert_u64() { + fn check(b1: BigUint, u: u64) { + let b2: BigUint = FromPrimitive::from_u64(u).unwrap(); + assert!(b1 == b2); + assert!(b1.to_u64().unwrap() == u); + } + + check(Zero::zero(), 0); + check(One::one(), 1); + check(u64::MIN.to_biguint().unwrap(), u64::MIN); + check(u64::MAX.to_biguint().unwrap(), u64::MAX); + + check(BigUint::new(vec!( )), 0); + check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::bits))); + check(BigUint::new(vec!(-1 )), (1 << (1*BigDigit::bits)) - 1); + check(BigUint::new(vec!( 0, 1)), (1 << (1*BigDigit::bits))); + check(BigUint::new(vec!(-1, -1)), u64::MAX); + + assert_eq!(BigUint::new(vec!( 0, 0, 1)).to_u64(), None); + assert_eq!(BigUint::new(vec!(-1, -1, -1)).to_u64(), None); + } + + #[test] + fn test_convert_to_bigint() { + fn check(n: BigUint, ans: BigInt) { + assert_eq!(n.to_bigint().unwrap(), ans); + assert_eq!(n.to_bigint().unwrap().to_biguint().unwrap(), n); + } + check(Zero::zero(), Zero::zero()); + check(BigUint::new(vec!(1,2,3)), + BigInt::from_biguint(Plus, BigUint::new(vec!(1,2,3)))); + } + + static sum_triples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] = &[ + (&[], &[], &[]), + (&[], &[ 1], &[ 1]), + (&[ 1], &[ 1], &[ 2]), + (&[ 1], &[ 1, 1], &[ 2, 1]), + (&[ 1], &[-1], &[ 0, 1]), + (&[ 1], &[-1, -1], &[ 0, 0, 1]), + (&[-1, -1], &[-1, -1], &[-2, -1, 1]), + (&[ 1, 1, 1], &[-1, -1], &[ 0, 1, 2]), + (&[ 2, 2, 1], &[-1, -2], &[ 1, 1, 2]) + ]; + + #[test] + fn test_add() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(a + b == c); + assert!(b + a == c); + } + } + + #[test] + fn test_sub() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(c - a == b); + assert!(c - b == a); + } + } + + #[test] + #[should_fail] + fn test_sub_fail_on_underflow() { + let (a, b) : (BigUint, BigUint) = (Zero::zero(), One::one()); + a - b; + } + + static mul_triples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] = &[ + (&[], &[], &[]), + (&[], &[ 1], &[]), + (&[ 2], &[], &[]), + (&[ 1], &[ 1], &[1]), + (&[ 2], &[ 3], &[ 6]), + (&[ 1], &[ 1, 1, 1], &[1, 1, 1]), + (&[ 1, 2, 3], &[ 3], &[ 3, 6, 9]), + (&[ 1, 1, 1], &[-1], &[-1, -1, -1]), + (&[ 1, 2, 3], &[-1], &[-1, -2, -2, 2]), + (&[ 1, 2, 3, 4], &[-1], &[-1, -2, -2, -2, 3]), + (&[-1], &[-1], &[ 1, -2]), + (&[-1, -1], &[-1], &[ 1, -1, -2]), + (&[-1, -1, -1], &[-1], &[ 1, -1, -1, -2]), + (&[-1, -1, -1, -1], &[-1], &[ 1, -1, -1, -1, -2]), + (&[-1/2 + 1], &[ 2], &[ 0, 1]), + (&[0, -1/2 + 1], &[ 2], &[ 0, 0, 1]), + (&[ 1, 2], &[ 1, 2, 3], &[1, 4, 7, 6]), + (&[-1, -1], &[-1, -1, -1], &[1, 0, -1, -2, -1]), + (&[-1, -1, -1], &[-1, -1, -1, -1], &[1, 0, 0, -1, -2, -1, -1]), + (&[ 0, 0, 1], &[ 1, 2, 3], &[0, 0, 1, 2, 3]), + (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) + ]; + + static div_rem_quadruples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] + = &[ + (&[ 1], &[ 2], &[], &[1]), + (&[ 1, 1], &[ 2], &[-1/2+1], &[1]), + (&[ 1, 1, 1], &[ 2], &[-1/2+1, -1/2+1], &[1]), + (&[ 0, 1], &[-1], &[1], &[1]), + (&[-1, -1], &[-2], &[2, 1], &[3]) + ]; + + #[test] + fn test_mul() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(a * b == c); + assert!(b * a == c); + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + let d = BigUint::from_slice(d_vec); + + assert!(a == b * c + d); + assert!(a == c * b + d); + } + } + + #[test] + fn test_div_rem() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + if !a.is_zero() { + assert_eq!(c.div_rem(&a), (b.clone(), Zero::zero())); + } + if !b.is_zero() { + assert_eq!(c.div_rem(&b), (a.clone(), Zero::zero())); + } + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + let d = BigUint::from_slice(d_vec); + + if !b.is_zero() { assert!(a.div_rem(&b) == (c, d)); } + } + } + + #[test] + fn test_checked_add() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(a.checked_add(&b).unwrap() == c); + assert!(b.checked_add(&a).unwrap() == c); + } + } + + #[test] + fn test_checked_sub() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(c.checked_sub(&a).unwrap() == b); + assert!(c.checked_sub(&b).unwrap() == a); + + if a > c { + assert!(a.checked_sub(&c).is_none()); + } + if b > c { + assert!(b.checked_sub(&c).is_none()); + } + } + } + + #[test] + fn test_checked_mul() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + assert!(a.checked_mul(&b).unwrap() == c); + assert!(b.checked_mul(&a).unwrap() == c); + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + let d = BigUint::from_slice(d_vec); + + assert!(a == b.checked_mul(&c).unwrap() + d); + assert!(a == c.checked_mul(&b).unwrap() + d); + } + } + + #[test] + fn test_checked_div() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigUint::from_slice(a_vec); + let b = BigUint::from_slice(b_vec); + let c = BigUint::from_slice(c_vec); + + if !a.is_zero() { + assert!(c.checked_div(&a).unwrap() == b); + } + if !b.is_zero() { + assert!(c.checked_div(&b).unwrap() == a); + } + + assert!(c.checked_div(&Zero::zero()).is_none()); + } + } + + #[test] + fn test_gcd() { + fn check(a: uint, b: uint, c: uint) { + let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); + let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); + let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); + + assert_eq!(big_a.gcd(&big_b), big_c); + } + + check(10, 2, 2); + check(10, 3, 1); + check(0, 3, 3); + check(3, 3, 3); + check(56, 42, 14); + } + + #[test] + fn test_lcm() { + fn check(a: uint, b: uint, c: uint) { + let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); + let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); + let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); + + assert_eq!(big_a.lcm(&big_b), big_c); + } + + check(1, 0, 0); + check(0, 1, 0); + check(1, 1, 1); + check(8, 9, 72); + check(11, 5, 55); + check(99, 17, 1683); + } + + #[test] + fn test_is_even() { + let one: BigUint = FromStr::from_str("1").unwrap(); + let two: BigUint = FromStr::from_str("2").unwrap(); + let thousand: BigUint = FromStr::from_str("1000").unwrap(); + let big: BigUint = FromStr::from_str("1000000000000000000000").unwrap(); + let bigger: BigUint = FromStr::from_str("1000000000000000000001").unwrap(); + assert!(one.is_odd()); + assert!(two.is_even()); + assert!(thousand.is_even()); + assert!(big.is_even()); + assert!(bigger.is_odd()); + assert!((one << 64).is_even()); + assert!(((one << 64) + one).is_odd()); + } + + fn to_str_pairs() -> Vec<(BigUint, Vec<(uint, String)>)> { + let bits = BigDigit::bits; + vec!(( Zero::zero(), vec!( + (2, "0".to_string()), (3, "0".to_string()) + )), ( BigUint::from_slice([ 0xff ]), vec!( + (2, "11111111".to_string()), + (3, "100110".to_string()), + (4, "3333".to_string()), + (5, "2010".to_string()), + (6, "1103".to_string()), + (7, "513".to_string()), + (8, "377".to_string()), + (9, "313".to_string()), + (10, "255".to_string()), + (11, "212".to_string()), + (12, "193".to_string()), + (13, "168".to_string()), + (14, "143".to_string()), + (15, "120".to_string()), + (16, "ff".to_string()) + )), ( BigUint::from_slice([ 0xfff ]), vec!( + (2, "111111111111".to_string()), + (4, "333333".to_string()), + (16, "fff".to_string()) + )), ( BigUint::from_slice([ 1, 2 ]), vec!( + (2, + format!("10{}1", "0".repeat(bits - 1))), + (4, + format!("2{}1", "0".repeat(bits / 2 - 1))), + (10, match bits { + 32 => "8589934593".to_string(), + 16 => "131073".to_string(), + _ => fail!() + }), + (16, + format!("2{}1", "0".repeat(bits / 4 - 1))) + )), ( BigUint::from_slice([ 1, 2, 3 ]), vec!( + (2, + format!("11{}10{}1", + "0".repeat(bits - 2), + "0".repeat(bits - 1))), + (4, + format!("3{}2{}1", + "0".repeat(bits / 2 - 1), + "0".repeat(bits / 2 - 1))), + (10, match bits { + 32 => "55340232229718589441".to_string(), + 16 => "12885032961".to_string(), + _ => fail!() + }), + (16, + format!("3{}2{}1", + "0".repeat(bits / 4 - 1), + "0".repeat(bits / 4 - 1))) + )) ) + } + + #[test] + fn test_to_str_radix() { + let r = to_str_pairs(); + for num_pair in r.iter() { + let &(ref n, ref rs) = num_pair; + for str_pair in rs.iter() { + let &(ref radix, ref str) = str_pair; + assert_eq!(n.to_str_radix(*radix).as_slice(), + str.as_slice()); + } + } + } + + #[test] + fn test_from_str_radix() { + let r = to_str_pairs(); + for num_pair in r.iter() { + let &(ref n, ref rs) = num_pair; + for str_pair in rs.iter() { + let &(ref radix, ref str) = str_pair; + assert_eq!(n, + &FromStrRadix::from_str_radix(str.as_slice(), + *radix).unwrap()); + } + } + + let zed: Option = FromStrRadix::from_str_radix("Z", 10); + assert_eq!(zed, None); + let blank: Option = FromStrRadix::from_str_radix("_", 2); + assert_eq!(blank, None); + let minus_one: Option = FromStrRadix::from_str_radix("-1", + 10); + assert_eq!(minus_one, None); + } + + #[test] + fn test_factor() { + fn factor(n: uint) -> BigUint { + let mut f: BigUint = One::one(); + for i in range(2, n + 1) { + // FIXME(#5992): assignment operator overloads + // f *= FromPrimitive::from_uint(i); + f = f * FromPrimitive::from_uint(i).unwrap(); + } + return f; + } + + fn check(n: uint, s: &str) { + let n = factor(n); + let ans = match FromStrRadix::from_str_radix(s, 10) { + Some(x) => x, None => fail!() + }; + assert_eq!(n, ans); + } + + check(3, "6"); + check(10, "3628800"); + check(20, "2432902008176640000"); + check(30, "265252859812191058636308480000000"); + } + + #[test] + fn test_bits() { + assert_eq!(BigUint::new(vec!(0,0,0,0)).bits(), 0); + let n: BigUint = FromPrimitive::from_uint(0).unwrap(); + assert_eq!(n.bits(), 0); + let n: BigUint = FromPrimitive::from_uint(1).unwrap(); + assert_eq!(n.bits(), 1); + let n: BigUint = FromPrimitive::from_uint(3).unwrap(); + assert_eq!(n.bits(), 2); + let n: BigUint = FromStrRadix::from_str_radix("4000000000", 16).unwrap(); + assert_eq!(n.bits(), 39); + let one: BigUint = One::one(); + assert_eq!((one << 426).bits(), 427); + } + + #[test] + fn test_rand() { + let mut rng = task_rng(); + let _n: BigUint = rng.gen_biguint(137); + assert!(rng.gen_biguint(0).is_zero()); + } + + #[test] + fn test_rand_range() { + let mut rng = task_rng(); + + for _ in range(0u, 10) { + assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), + &FromPrimitive::from_uint(237).unwrap()), + FromPrimitive::from_uint(236).unwrap()); + } + + let l = FromPrimitive::from_uint(403469000 + 2352).unwrap(); + let u = FromPrimitive::from_uint(403469000 + 3513).unwrap(); + for _ in range(0u, 1000) { + let n: BigUint = rng.gen_biguint_below(&u); + assert!(n < u); + + let n: BigUint = rng.gen_biguint_range(&l, &u); + assert!(n >= l); + assert!(n < u); + } + } + + #[test] + #[should_fail] + fn test_zero_rand_range() { + task_rng().gen_biguint_range(&FromPrimitive::from_uint(54).unwrap(), + &FromPrimitive::from_uint(54).unwrap()); + } + + #[test] + #[should_fail] + fn test_negative_rand_range() { + let mut rng = task_rng(); + let l = FromPrimitive::from_uint(2352).unwrap(); + let u = FromPrimitive::from_uint(3513).unwrap(); + // Switching u and l should fail: + let _n: BigUint = rng.gen_biguint_range(&u, &l); + } +} + +#[cfg(test)] +mod bigint_tests { + use Integer; + use super::{BigDigit, BigUint, ToBigUint}; + use super::{Sign, Minus, Zero, Plus, BigInt, RandBigInt, ToBigInt}; + + use std::cmp::{Less, Equal, Greater}; + use std::i64; + use std::num::CheckedDiv; + use std::num::{Zero, One, FromStrRadix, ToStrRadix}; + use std::num::{ToPrimitive, FromPrimitive}; + use std::rand::task_rng; + use std::u64; + use std::hash::hash; + + #[test] + fn test_from_biguint() { + fn check(inp_s: Sign, inp_n: uint, ans_s: Sign, ans_n: uint) { + let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_uint(inp_n).unwrap()); + let ans = BigInt { sign: ans_s, data: FromPrimitive::from_uint(ans_n).unwrap()}; + assert_eq!(inp, ans); + } + check(Plus, 1, Plus, 1); + check(Plus, 0, Zero, 0); + check(Minus, 1, Minus, 1); + check(Zero, 1, Zero, 0); + } + + #[test] + fn test_cmp() { + let vs: [&[BigDigit], ..4] = [ &[2 as BigDigit], &[1, 1], &[2, 1], &[1, 1, 1] ]; + let mut nums = Vec::new(); + for s in vs.iter().rev() { + nums.push(BigInt::from_slice(Minus, *s)); + } + nums.push(Zero::zero()); + nums.extend(vs.iter().map(|s| BigInt::from_slice(Plus, *s))); + + for (i, ni) in nums.iter().enumerate() { + for (j0, nj) in nums.slice(i, nums.len()).iter().enumerate() { + let j = i + j0; + if i == j { + assert_eq!(ni.cmp(nj), Equal); + assert_eq!(nj.cmp(ni), Equal); + assert_eq!(ni, nj); + assert!(!(ni != nj)); + assert!(ni <= nj); + assert!(ni >= nj); + assert!(!(ni < nj)); + assert!(!(ni > nj)); + } else { + assert_eq!(ni.cmp(nj), Less); + assert_eq!(nj.cmp(ni), Greater); + + assert!(!(ni == nj)); + assert!(ni != nj); + + assert!(ni <= nj); + assert!(!(ni >= nj)); + assert!(ni < nj); + assert!(!(ni > nj)); + + assert!(!(nj <= ni)); + assert!(nj >= ni); + assert!(!(nj < ni)); + assert!(nj > ni); + } + } + } + } + + #[test] + fn test_hash() { + let a = BigInt::new(Zero, vec!()); + let b = BigInt::new(Zero, vec!(0)); + let c = BigInt::new(Plus, vec!(1)); + let d = BigInt::new(Plus, vec!(1,0,0,0,0,0)); + let e = BigInt::new(Plus, vec!(0,0,0,0,0,1)); + let f = BigInt::new(Minus, vec!(1)); + assert!(hash(&a) == hash(&b)); + assert!(hash(&b) != hash(&c)); + assert!(hash(&c) == hash(&d)); + assert!(hash(&d) != hash(&e)); + assert!(hash(&c) != hash(&f)); + } + + #[test] + fn test_convert_i64() { + fn check(b1: BigInt, i: i64) { + let b2: BigInt = FromPrimitive::from_i64(i).unwrap(); + assert!(b1 == b2); + assert!(b1.to_i64().unwrap() == i); + } + + check(Zero::zero(), 0); + check(One::one(), 1); + check(i64::MIN.to_bigint().unwrap(), i64::MIN); + check(i64::MAX.to_bigint().unwrap(), i64::MAX); + + assert_eq!( + (i64::MAX as u64 + 1).to_bigint().unwrap().to_i64(), + None); + + assert_eq!( + BigInt::from_biguint(Plus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_i64(), + None); + + assert_eq!( + BigInt::from_biguint(Minus, BigUint::new(vec!(1,0,0,1<<(BigDigit::bits-1)))).to_i64(), + None); + + assert_eq!( + BigInt::from_biguint(Minus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_i64(), + None); + } + + #[test] + fn test_convert_u64() { + fn check(b1: BigInt, u: u64) { + let b2: BigInt = FromPrimitive::from_u64(u).unwrap(); + assert!(b1 == b2); + assert!(b1.to_u64().unwrap() == u); + } + + check(Zero::zero(), 0); + check(One::one(), 1); + check(u64::MIN.to_bigint().unwrap(), u64::MIN); + check(u64::MAX.to_bigint().unwrap(), u64::MAX); + + assert_eq!( + BigInt::from_biguint(Plus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_u64(), + None); + + let max_value: BigUint = FromPrimitive::from_u64(u64::MAX).unwrap(); + assert_eq!(BigInt::from_biguint(Minus, max_value).to_u64(), None); + assert_eq!(BigInt::from_biguint(Minus, BigUint::new(vec!(1, 2, 3, 4, 5))).to_u64(), None); + } + + #[test] + fn test_convert_to_biguint() { + fn check(n: BigInt, ans_1: BigUint) { + assert_eq!(n.to_biguint().unwrap(), ans_1); + assert_eq!(n.to_biguint().unwrap().to_bigint().unwrap(), n); + } + let zero: BigInt = Zero::zero(); + let unsigned_zero: BigUint = Zero::zero(); + let positive = BigInt::from_biguint( + Plus, BigUint::new(vec!(1,2,3))); + let negative = -positive; + + check(zero, unsigned_zero); + check(positive, BigUint::new(vec!(1,2,3))); + + assert_eq!(negative.to_biguint(), None); + } + + static sum_triples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] = &[ + (&[], &[], &[]), + (&[], &[ 1], &[ 1]), + (&[ 1], &[ 1], &[ 2]), + (&[ 1], &[ 1, 1], &[ 2, 1]), + (&[ 1], &[-1], &[ 0, 1]), + (&[ 1], &[-1, -1], &[ 0, 0, 1]), + (&[-1, -1], &[-1, -1], &[-2, -1, 1]), + (&[ 1, 1, 1], &[-1, -1], &[ 0, 1, 2]), + (&[ 2, 2, 1], &[-1, -2], &[ 1, 1, 2]) + ]; + + #[test] + fn test_add() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(a + b == c); + assert!(b + a == c); + assert!(c + (-a) == b); + assert!(c + (-b) == a); + assert!(a + (-c) == (-b)); + assert!(b + (-c) == (-a)); + assert!((-a) + (-b) == (-c)) + assert!(a + (-a) == Zero::zero()); + } + } + + #[test] + fn test_sub() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(c - a == b); + assert!(c - b == a); + assert!((-b) - a == (-c)) + assert!((-a) - b == (-c)) + assert!(b - (-a) == c); + assert!(a - (-b) == c); + assert!((-c) - (-a) == (-b)); + assert!(a - a == Zero::zero()); + } + } + + static mul_triples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] = &[ + (&[], &[], &[]), + (&[], &[ 1], &[]), + (&[ 2], &[], &[]), + (&[ 1], &[ 1], &[1]), + (&[ 2], &[ 3], &[ 6]), + (&[ 1], &[ 1, 1, 1], &[1, 1, 1]), + (&[ 1, 2, 3], &[ 3], &[ 3, 6, 9]), + (&[ 1, 1, 1], &[-1], &[-1, -1, -1]), + (&[ 1, 2, 3], &[-1], &[-1, -2, -2, 2]), + (&[ 1, 2, 3, 4], &[-1], &[-1, -2, -2, -2, 3]), + (&[-1], &[-1], &[ 1, -2]), + (&[-1, -1], &[-1], &[ 1, -1, -2]), + (&[-1, -1, -1], &[-1], &[ 1, -1, -1, -2]), + (&[-1, -1, -1, -1], &[-1], &[ 1, -1, -1, -1, -2]), + (&[-1/2 + 1], &[ 2], &[ 0, 1]), + (&[0, -1/2 + 1], &[ 2], &[ 0, 0, 1]), + (&[ 1, 2], &[ 1, 2, 3], &[1, 4, 7, 6]), + (&[-1, -1], &[-1, -1, -1], &[1, 0, -1, -2, -1]), + (&[-1, -1, -1], &[-1, -1, -1, -1], &[1, 0, 0, -1, -2, -1, -1]), + (&[ 0, 0, 1], &[ 1, 2, 3], &[0, 0, 1, 2, 3]), + (&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1]) + ]; + + static div_rem_quadruples: &'static [(&'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit], + &'static [BigDigit])] + = &[ + (&[ 1], &[ 2], &[], &[1]), + (&[ 1, 1], &[ 2], &[-1/2+1], &[1]), + (&[ 1, 1, 1], &[ 2], &[-1/2+1, -1/2+1], &[1]), + (&[ 0, 1], &[-1], &[1], &[1]), + (&[-1, -1], &[-2], &[2, 1], &[3]) + ]; + + #[test] + fn test_mul() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(a * b == c); + assert!(b * a == c); + + assert!((-a) * b == -c); + assert!((-b) * a == -c); + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + let d = BigInt::from_slice(Plus, d_vec); + + assert!(a == b * c + d); + assert!(a == c * b + d); + } + } + + #[test] + fn test_div_mod_floor() { + fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) { + let (d, m) = a.div_mod_floor(b); + if !m.is_zero() { + assert_eq!(m.sign, b.sign); + } + assert!(m.abs() <= b.abs()); + assert!(*a == b * d + m); + assert!(d == *ans_d); + assert!(m == *ans_m); + } + + fn check(a: &BigInt, b: &BigInt, d: &BigInt, m: &BigInt) { + if m.is_zero() { + check_sub(a, b, d, m); + check_sub(a, &b.neg(), &d.neg(), m); + check_sub(&a.neg(), b, &d.neg(), m); + check_sub(&a.neg(), &b.neg(), d, m); + } else { + check_sub(a, b, d, m); + check_sub(a, &b.neg(), &(d.neg() - One::one()), &(m - *b)); + check_sub(&a.neg(), b, &(d.neg() - One::one()), &(b - *m)); + check_sub(&a.neg(), &b.neg(), d, &m.neg()); + } + } + + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); } + if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); } + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + let d = BigInt::from_slice(Plus, d_vec); + + if !b.is_zero() { + check(&a, &b, &c, &d); + } + } + } + + + #[test] + fn test_div_rem() { + fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) { + let (q, r) = a.div_rem(b); + if !r.is_zero() { + assert_eq!(r.sign, a.sign); + } + assert!(r.abs() <= b.abs()); + assert!(*a == b * q + r); + assert!(q == *ans_q); + assert!(r == *ans_r); + } + + fn check(a: &BigInt, b: &BigInt, q: &BigInt, r: &BigInt) { + check_sub(a, b, q, r); + check_sub(a, &b.neg(), &q.neg(), r); + check_sub(&a.neg(), b, &q.neg(), &r.neg()); + check_sub(&a.neg(), &b.neg(), q, &r.neg()); + } + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); } + if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); } + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + let d = BigInt::from_slice(Plus, d_vec); + + if !b.is_zero() { + check(&a, &b, &c, &d); + } + } + } + + #[test] + fn test_checked_add() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(a.checked_add(&b).unwrap() == c); + assert!(b.checked_add(&a).unwrap() == c); + assert!(c.checked_add(&(-a)).unwrap() == b); + assert!(c.checked_add(&(-b)).unwrap() == a); + assert!(a.checked_add(&(-c)).unwrap() == (-b)); + assert!(b.checked_add(&(-c)).unwrap() == (-a)); + assert!((-a).checked_add(&(-b)).unwrap() == (-c)) + assert!(a.checked_add(&(-a)).unwrap() == Zero::zero()); + } + } + + #[test] + fn test_checked_sub() { + for elm in sum_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(c.checked_sub(&a).unwrap() == b); + assert!(c.checked_sub(&b).unwrap() == a); + assert!((-b).checked_sub(&a).unwrap() == (-c)) + assert!((-a).checked_sub(&b).unwrap() == (-c)) + assert!(b.checked_sub(&(-a)).unwrap() == c); + assert!(a.checked_sub(&(-b)).unwrap() == c); + assert!((-c).checked_sub(&(-a)).unwrap() == (-b)); + assert!(a.checked_sub(&a).unwrap() == Zero::zero()); + } + } + + #[test] + fn test_checked_mul() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + assert!(a.checked_mul(&b).unwrap() == c); + assert!(b.checked_mul(&a).unwrap() == c); + + assert!((-a).checked_mul(&b).unwrap() == -c); + assert!((-b).checked_mul(&a).unwrap() == -c); + } + + for elm in div_rem_quadruples.iter() { + let (a_vec, b_vec, c_vec, d_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + let d = BigInt::from_slice(Plus, d_vec); + + assert!(a == b.checked_mul(&c).unwrap() + d); + assert!(a == c.checked_mul(&b).unwrap() + d); + } + } + #[test] + fn test_checked_div() { + for elm in mul_triples.iter() { + let (a_vec, b_vec, c_vec) = *elm; + let a = BigInt::from_slice(Plus, a_vec); + let b = BigInt::from_slice(Plus, b_vec); + let c = BigInt::from_slice(Plus, c_vec); + + if !a.is_zero() { + assert!(c.checked_div(&a).unwrap() == b); + assert!((-c).checked_div(&(-a)).unwrap() == b); + assert!((-c).checked_div(&a).unwrap() == -b); + } + if !b.is_zero() { + assert!(c.checked_div(&b).unwrap() == a); + assert!((-c).checked_div(&(-b)).unwrap() == a); + assert!((-c).checked_div(&b).unwrap() == -a); + } + + assert!(c.checked_div(&Zero::zero()).is_none()); + assert!((-c).checked_div(&Zero::zero()).is_none()); + } + } + + #[test] + fn test_gcd() { + fn check(a: int, b: int, c: int) { + let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); + let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); + let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); + + assert_eq!(big_a.gcd(&big_b), big_c); + } + + check(10, 2, 2); + check(10, 3, 1); + check(0, 3, 3); + check(3, 3, 3); + check(56, 42, 14); + check(3, -3, 3); + check(-6, 3, 3); + check(-4, -2, 2); + } + + #[test] + fn test_lcm() { + fn check(a: int, b: int, c: int) { + let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); + let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); + let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); + + assert_eq!(big_a.lcm(&big_b), big_c); + } + + check(1, 0, 0); + check(0, 1, 0); + check(1, 1, 1); + check(-1, 1, 1); + check(1, -1, 1); + check(-1, -1, 1); + check(8, 9, 72); + check(11, 5, 55); + } + + #[test] + fn test_abs_sub() { + let zero: BigInt = Zero::zero(); + let one: BigInt = One::one(); + assert_eq!((-one).abs_sub(&one), zero); + let one: BigInt = One::one(); + let zero: BigInt = Zero::zero(); + assert_eq!(one.abs_sub(&one), zero); + let one: BigInt = One::one(); + let zero: BigInt = Zero::zero(); + assert_eq!(one.abs_sub(&zero), one); + let one: BigInt = One::one(); + let two: BigInt = FromPrimitive::from_int(2).unwrap(); + assert_eq!(one.abs_sub(&-one), two); + } + + #[test] + fn test_to_str_radix() { + fn check(n: int, ans: &str) { + let n: BigInt = FromPrimitive::from_int(n).unwrap(); + assert!(ans == n.to_str_radix(10).as_slice()); + } + check(10, "10"); + check(1, "1"); + check(0, "0"); + check(-1, "-1"); + check(-10, "-10"); + } + + + #[test] + fn test_from_str_radix() { + fn check(s: &str, ans: Option) { + let ans = ans.map(|n| { + let x: BigInt = FromPrimitive::from_int(n).unwrap(); + x + }); + assert_eq!(FromStrRadix::from_str_radix(s, 10), ans); + } + check("10", Some(10)); + check("1", Some(1)); + check("0", Some(0)); + check("-1", Some(-1)); + check("-10", Some(-10)); + check("Z", None); + check("_", None); + + // issue 10522, this hit an edge case that caused it to + // attempt to allocate a vector of size (-1u) == huge. + let x: BigInt = + from_str(format!("1{}", "0".repeat(36)).as_slice()).unwrap(); + let _y = x.to_string(); + } + + #[test] + fn test_neg() { + assert!(-BigInt::new(Plus, vec!(1, 1, 1)) == + BigInt::new(Minus, vec!(1, 1, 1))); + assert!(-BigInt::new(Minus, vec!(1, 1, 1)) == + BigInt::new(Plus, vec!(1, 1, 1))); + let zero: BigInt = Zero::zero(); + assert_eq!(-zero, zero); + } + + #[test] + fn test_rand() { + let mut rng = task_rng(); + let _n: BigInt = rng.gen_bigint(137); + assert!(rng.gen_bigint(0).is_zero()); + } + + #[test] + fn test_rand_range() { + let mut rng = task_rng(); + + for _ in range(0u, 10) { + assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), + &FromPrimitive::from_uint(237).unwrap()), + FromPrimitive::from_uint(236).unwrap()); + } + + fn check(l: BigInt, u: BigInt) { + let mut rng = task_rng(); + for _ in range(0u, 1000) { + let n: BigInt = rng.gen_bigint_range(&l, &u); + assert!(n >= l); + assert!(n < u); + } + } + let l: BigInt = FromPrimitive::from_uint(403469000 + 2352).unwrap(); + let u: BigInt = FromPrimitive::from_uint(403469000 + 3513).unwrap(); + check( l.clone(), u.clone()); + check(-l.clone(), u.clone()); + check(-u.clone(), -l.clone()); + } + + #[test] + #[should_fail] + fn test_zero_rand_range() { + task_rng().gen_bigint_range(&FromPrimitive::from_int(54).unwrap(), + &FromPrimitive::from_int(54).unwrap()); + } + + #[test] + #[should_fail] + fn test_negative_rand_range() { + let mut rng = task_rng(); + let l = FromPrimitive::from_uint(2352).unwrap(); + let u = FromPrimitive::from_uint(3513).unwrap(); + // Switching u and l should fail: + let _n: BigInt = rng.gen_bigint_range(&u, &l); + } +} + +#[cfg(test)] +mod bench { + extern crate test; + use self::test::Bencher; + use super::BigUint; + use std::iter; + use std::mem::replace; + use std::num::{FromPrimitive, Zero, One}; + + fn factorial(n: uint) -> BigUint { + let mut f: BigUint = One::one(); + for i in iter::range_inclusive(1, n) { + f = f * FromPrimitive::from_uint(i).unwrap(); + } + f + } + + fn fib(n: uint) -> BigUint { + let mut f0: BigUint = Zero::zero(); + let mut f1: BigUint = One::one(); + for _ in range(0, n) { + let f2 = f0 + f1; + f0 = replace(&mut f1, f2); + } + f0 + } + + #[bench] + fn factorial_100(b: &mut Bencher) { + b.iter(|| { + factorial(100); + }); + } + + #[bench] + fn fib_100(b: &mut Bencher) { + b.iter(|| { + fib(100); + }); + } + + #[bench] + fn to_string(b: &mut Bencher) { + let fac = factorial(100); + let fib = fib(100); + b.iter(|| { + fac.to_string(); + }); + b.iter(|| { + fib.to_string(); + }); + } + + #[bench] + fn shr(b: &mut Bencher) { + let n = { let one : BigUint = One::one(); one << 1000 }; + b.iter(|| { + let mut m = n.clone(); + for _ in range(0u, 10) { + m = m >> 1; + } + }) + } +} diff --git a/src/complex.rs b/src/complex.rs new file mode 100644 index 0000000..24c99a3 --- /dev/null +++ b/src/complex.rs @@ -0,0 +1,379 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + + +//! Complex numbers. + +use std::fmt; +use std::num::{Zero, One, ToStrRadix}; + +// FIXME #1284: handle complex NaN & infinity etc. This +// probably doesn't map to C's _Complex correctly. + +/// A complex number in Cartesian form. +#[deriving(PartialEq, Clone, Hash)] +pub struct Complex { + /// Real portion of the complex number + pub re: T, + /// Imaginary portion of the complex number + pub im: T +} + +pub type Complex32 = Complex; +pub type Complex64 = Complex; + +impl Complex { + /// Create a new Complex + #[inline] + pub fn new(re: T, im: T) -> Complex { + Complex { re: re, im: im } + } + + /// Returns the square of the norm (since `T` doesn't necessarily + /// have a sqrt function), i.e. `re^2 + im^2`. + #[inline] + pub fn norm_sqr(&self) -> T { + self.re * self.re + self.im * self.im + } + + + /// Returns the complex conjugate. i.e. `re - i im` + #[inline] + pub fn conj(&self) -> Complex { + Complex::new(self.re.clone(), -self.im) + } + + + /// Multiplies `self` by the scalar `t`. + #[inline] + pub fn scale(&self, t: T) -> Complex { + Complex::new(self.re * t, self.im * t) + } + + /// Divides `self` by the scalar `t`. + #[inline] + pub fn unscale(&self, t: T) -> Complex { + Complex::new(self.re / t, self.im / t) + } + + /// Returns `1/self` + #[inline] + pub fn inv(&self) -> Complex { + let norm_sqr = self.norm_sqr(); + Complex::new(self.re / norm_sqr, + -self.im / norm_sqr) + } +} + +impl Complex { + /// Calculate |self| + #[inline] + pub fn norm(&self) -> T { + self.re.hypot(self.im) + } +} + +impl Complex { + /// Calculate the principal Arg of self. + #[inline] + pub fn arg(&self) -> T { + self.im.atan2(self.re) + } + /// Convert to polar form (r, theta), such that `self = r * exp(i + /// * theta)` + #[inline] + pub fn to_polar(&self) -> (T, T) { + (self.norm(), self.arg()) + } + /// Convert a polar representation into a complex number. + #[inline] + pub fn from_polar(r: &T, theta: &T) -> Complex { + Complex::new(*r * theta.cos(), *r * theta.sin()) + } +} + +/* arithmetic */ +// (a + i b) + (c + i d) == (a + c) + i (b + d) +impl Add, Complex> for Complex { + #[inline] + fn add(&self, other: &Complex) -> Complex { + Complex::new(self.re + other.re, self.im + other.im) + } +} +// (a + i b) - (c + i d) == (a - c) + i (b - d) +impl Sub, Complex> for Complex { + #[inline] + fn sub(&self, other: &Complex) -> Complex { + Complex::new(self.re - other.re, self.im - other.im) + } +} +// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) +impl Mul, Complex> for Complex { + #[inline] + fn mul(&self, other: &Complex) -> Complex { + Complex::new(self.re*other.re - self.im*other.im, + self.re*other.im + self.im*other.re) + } +} + +// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) +// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] +impl Div, Complex> for Complex { + #[inline] + fn div(&self, other: &Complex) -> Complex { + let norm_sqr = other.norm_sqr(); + Complex::new((self.re*other.re + self.im*other.im) / norm_sqr, + (self.im*other.re - self.re*other.im) / norm_sqr) + } +} + +impl Neg> for Complex { + #[inline] + fn neg(&self) -> Complex { + Complex::new(-self.re, -self.im) + } +} + +/* constants */ +impl Zero for Complex { + #[inline] + fn zero() -> Complex { + Complex::new(Zero::zero(), Zero::zero()) + } + + #[inline] + fn is_zero(&self) -> bool { + self.re.is_zero() && self.im.is_zero() + } +} + +impl One for Complex { + #[inline] + fn one() -> Complex { + Complex::new(One::one(), Zero::zero()) + } +} + +/* string conversions */ +impl fmt::Show for Complex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.im < Zero::zero() { + write!(f, "{}-{}i", self.re, -self.im) + } else { + write!(f, "{}+{}i", self.re, self.im) + } + } +} + +impl ToStrRadix for Complex { + fn to_str_radix(&self, radix: uint) -> String { + if self.im < Zero::zero() { + format!("{}-{}i", + self.re.to_str_radix(radix), + (-self.im).to_str_radix(radix)) + } else { + format!("{}+{}i", + self.re.to_str_radix(radix), + self.im.to_str_radix(radix)) + } + } +} + +#[cfg(test)] +mod test { + #![allow(non_uppercase_statics)] + + use super::{Complex64, Complex}; + use std::num::{Zero, One, Float}; + use std::hash::hash; + + pub static _0_0i : Complex64 = Complex { re: 0.0, im: 0.0 }; + pub static _1_0i : Complex64 = Complex { re: 1.0, im: 0.0 }; + pub static _1_1i : Complex64 = Complex { re: 1.0, im: 1.0 }; + pub static _0_1i : Complex64 = Complex { re: 0.0, im: 1.0 }; + pub static _neg1_1i : Complex64 = Complex { re: -1.0, im: 1.0 }; + pub static _05_05i : Complex64 = Complex { re: 0.5, im: 0.5 }; + pub static all_consts : [Complex64, .. 5] = [_0_0i, _1_0i, _1_1i, _neg1_1i, _05_05i]; + + #[test] + fn test_consts() { + // check our constants are what Complex::new creates + fn test(c : Complex64, r : f64, i: f64) { + assert_eq!(c, Complex::new(r,i)); + } + test(_0_0i, 0.0, 0.0); + test(_1_0i, 1.0, 0.0); + test(_1_1i, 1.0, 1.0); + test(_neg1_1i, -1.0, 1.0); + test(_05_05i, 0.5, 0.5); + + assert_eq!(_0_0i, Zero::zero()); + assert_eq!(_1_0i, One::one()); + } + + #[test] + #[ignore(cfg(target_arch = "x86"))] + // FIXME #7158: (maybe?) currently failing on x86. + fn test_norm() { + fn test(c: Complex64, ns: f64) { + assert_eq!(c.norm_sqr(), ns); + assert_eq!(c.norm(), ns.sqrt()) + } + test(_0_0i, 0.0); + test(_1_0i, 1.0); + test(_1_1i, 2.0); + test(_neg1_1i, 2.0); + test(_05_05i, 0.5); + } + + #[test] + fn test_scale_unscale() { + assert_eq!(_05_05i.scale(2.0), _1_1i); + assert_eq!(_1_1i.unscale(2.0), _05_05i); + for &c in all_consts.iter() { + assert_eq!(c.scale(2.0).unscale(2.0), c); + } + } + + #[test] + fn test_conj() { + for &c in all_consts.iter() { + assert_eq!(c.conj(), Complex::new(c.re, -c.im)); + assert_eq!(c.conj().conj(), c); + } + } + + #[test] + fn test_inv() { + assert_eq!(_1_1i.inv(), _05_05i.conj()); + assert_eq!(_1_0i.inv(), _1_0i.inv()); + } + + #[test] + #[should_fail] + fn test_divide_by_zero_natural() { + let n = Complex::new(2i, 3i); + let d = Complex::new(0, 0); + let _x = n / d; + } + + #[test] + #[should_fail] + #[ignore] + fn test_inv_zero() { + // FIXME #5736: should this really fail, or just NaN? + _0_0i.inv(); + } + + #[test] + fn test_arg() { + fn test(c: Complex64, arg: f64) { + assert!((c.arg() - arg).abs() < 1.0e-6) + } + test(_1_0i, 0.0); + test(_1_1i, 0.25 * Float::pi()); + test(_neg1_1i, 0.75 * Float::pi()); + test(_05_05i, 0.25 * Float::pi()); + } + + #[test] + fn test_polar_conv() { + fn test(c: Complex64) { + let (r, theta) = c.to_polar(); + assert!((c - Complex::from_polar(&r, &theta)).norm() < 1e-6); + } + for &c in all_consts.iter() { test(c); } + } + + mod arith { + use super::{_0_0i, _1_0i, _1_1i, _0_1i, _neg1_1i, _05_05i, all_consts}; + use std::num::Zero; + + #[test] + fn test_add() { + assert_eq!(_05_05i + _05_05i, _1_1i); + assert_eq!(_0_1i + _1_0i, _1_1i); + assert_eq!(_1_0i + _neg1_1i, _0_1i); + + for &c in all_consts.iter() { + assert_eq!(_0_0i + c, c); + assert_eq!(c + _0_0i, c); + } + } + + #[test] + fn test_sub() { + assert_eq!(_05_05i - _05_05i, _0_0i); + assert_eq!(_0_1i - _1_0i, _neg1_1i); + assert_eq!(_0_1i - _neg1_1i, _1_0i); + + for &c in all_consts.iter() { + assert_eq!(c - _0_0i, c); + assert_eq!(c - c, _0_0i); + } + } + + #[test] + fn test_mul() { + assert_eq!(_05_05i * _05_05i, _0_1i.unscale(2.0)); + assert_eq!(_1_1i * _0_1i, _neg1_1i); + + // i^2 & i^4 + assert_eq!(_0_1i * _0_1i, -_1_0i); + assert_eq!(_0_1i * _0_1i * _0_1i * _0_1i, _1_0i); + + for &c in all_consts.iter() { + assert_eq!(c * _1_0i, c); + assert_eq!(_1_0i * c, c); + } + } + #[test] + fn test_div() { + assert_eq!(_neg1_1i / _0_1i, _1_1i); + for &c in all_consts.iter() { + if c != Zero::zero() { + assert_eq!(c / c, _1_0i); + } + } + } + #[test] + fn test_neg() { + assert_eq!(-_1_0i + _0_1i, _neg1_1i); + assert_eq!((-_0_1i) * _0_1i, _1_0i); + for &c in all_consts.iter() { + assert_eq!(-(-c), c); + } + } + } + + #[test] + fn test_to_string() { + fn test(c : Complex64, s: String) { + assert_eq!(c.to_string(), s); + } + test(_0_0i, "0+0i".to_string()); + test(_1_0i, "1+0i".to_string()); + test(_0_1i, "0+1i".to_string()); + test(_1_1i, "1+1i".to_string()); + test(_neg1_1i, "-1+1i".to_string()); + test(-_neg1_1i, "1-1i".to_string()); + test(_05_05i, "0.5+0.5i".to_string()); + } + + #[test] + fn test_hash() { + let a = Complex::new(0i32, 0i32); + let b = Complex::new(1i32, 0i32); + let c = Complex::new(0i32, 1i32); + assert!(hash(&a) != hash(&b)); + assert!(hash(&b) != hash(&c)); + assert!(hash(&c) != hash(&a)); + } +} diff --git a/src/integer.rs b/src/integer.rs new file mode 100644 index 0000000..b06e2b4 --- /dev/null +++ b/src/integer.rs @@ -0,0 +1,496 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Integer trait and functions. + +pub trait Integer: Num + PartialOrd + + Div + + Rem { + /// Floored integer division. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert!(( 8i).div_floor(& 3) == 2); + /// assert!(( 8i).div_floor(&-3) == -3); + /// assert!((-8i).div_floor(& 3) == -3); + /// assert!((-8i).div_floor(&-3) == 2); + /// + /// assert!(( 1i).div_floor(& 2) == 0); + /// assert!(( 1i).div_floor(&-2) == -1); + /// assert!((-1i).div_floor(& 2) == -1); + /// assert!((-1i).div_floor(&-2) == 0); + /// ~~~ + fn div_floor(&self, other: &Self) -> Self; + + /// Floored integer modulo, satisfying: + /// + /// ~~~ + /// # use num::Integer; + /// # let n = 1i; let d = 1i; + /// assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n) + /// ~~~ + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert!(( 8i).mod_floor(& 3) == 2); + /// assert!(( 8i).mod_floor(&-3) == -1); + /// assert!((-8i).mod_floor(& 3) == 1); + /// assert!((-8i).mod_floor(&-3) == -2); + /// + /// assert!(( 1i).mod_floor(& 2) == 1); + /// assert!(( 1i).mod_floor(&-2) == -1); + /// assert!((-1i).mod_floor(& 2) == 1); + /// assert!((-1i).mod_floor(&-2) == -1); + /// ~~~ + fn mod_floor(&self, other: &Self) -> Self; + + /// Greatest Common Divisor (GCD). + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(6i.gcd(&8), 2); + /// assert_eq!(7i.gcd(&3), 1); + /// ~~~ + fn gcd(&self, other: &Self) -> Self; + + /// Lowest Common Multiple (LCM). + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(7i.lcm(&3), 21); + /// assert_eq!(2i.lcm(&4), 4); + /// ~~~ + fn lcm(&self, other: &Self) -> Self; + + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + fn divides(&self, other: &Self) -> bool; + + /// Returns `true` if `other` is a multiple of `self`. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(9i.is_multiple_of(&3), true); + /// assert_eq!(3i.is_multiple_of(&9), false); + /// ~~~ + fn is_multiple_of(&self, other: &Self) -> bool; + + /// Returns `true` if the number is even. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(3i.is_even(), false); + /// assert_eq!(4i.is_even(), true); + /// ~~~ + fn is_even(&self) -> bool; + + /// Returns `true` if the number is odd. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(3i.is_odd(), true); + /// assert_eq!(4i.is_odd(), false); + /// ~~~ + fn is_odd(&self) -> bool; + + /// Simultaneous truncated integer division and modulus. + /// Returns `(quotient, remainder)`. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(( 8i).div_rem( &3), ( 2, 2)); + /// assert_eq!(( 8i).div_rem(&-3), (-2, 2)); + /// assert_eq!((-8i).div_rem( &3), (-2, -2)); + /// assert_eq!((-8i).div_rem(&-3), ( 2, -2)); + /// + /// assert_eq!(( 1i).div_rem( &2), ( 0, 1)); + /// assert_eq!(( 1i).div_rem(&-2), ( 0, 1)); + /// assert_eq!((-1i).div_rem( &2), ( 0, -1)); + /// assert_eq!((-1i).div_rem(&-2), ( 0, -1)); + /// ~~~ + #[inline] + fn div_rem(&self, other: &Self) -> (Self, Self) { + (*self / *other, *self % *other) + } + + /// Simultaneous floored integer division and modulus. + /// Returns `(quotient, remainder)`. + /// + /// # Examples + /// + /// ~~~ + /// # use num::Integer; + /// assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2)); + /// assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1)); + /// assert_eq!((-8i).div_mod_floor( &3), (-3, 1)); + /// assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2)); + /// + /// assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1)); + /// assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1)); + /// assert_eq!((-1i).div_mod_floor( &2), (-1, 1)); + /// assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1)); + /// ~~~ + fn div_mod_floor(&self, other: &Self) -> (Self, Self) { + (self.div_floor(other), self.mod_floor(other)) + } +} + +/// Simultaneous integer division and modulus +#[inline] pub fn div_rem(x: T, y: T) -> (T, T) { x.div_rem(&y) } +/// Floored integer division +#[inline] pub fn div_floor(x: T, y: T) -> T { x.div_floor(&y) } +/// Floored integer modulus +#[inline] pub fn mod_floor(x: T, y: T) -> T { x.mod_floor(&y) } +/// Simultaneous floored integer division and modulus +#[inline] pub fn div_mod_floor(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(x: T, y: T) -> T { x.gcd(&y) } +/// Calculates the Lowest Common Multiple (LCM) of the number and `other`. +#[inline(always)] pub fn lcm(x: T, y: T) -> T { x.lcm(&y) } + +macro_rules! impl_integer_for_int { + ($T:ty, $test_mod:ident) => ( + impl Integer for $T { + /// Floored integer division + #[inline] + fn div_floor(&self, other: &$T) -> $T { + // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, + // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) + match self.div_rem(other) { + (d, r) if (r > 0 && *other < 0) + || (r < 0 && *other > 0) => d - 1, + (d, _) => d, + } + } + + /// Floored integer modulo + #[inline] + fn mod_floor(&self, other: &$T) -> $T { + // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, + // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) + match *self % *other { + r if (r > 0 && *other < 0) + || (r < 0 && *other > 0) => r + *other, + r => r, + } + } + + /// Calculates `div_floor` and `mod_floor` simultaneously + #[inline] + fn div_mod_floor(&self, other: &$T) -> ($T,$T) { + // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_, + // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf) + match self.div_rem(other) { + (d, r) if (r > 0 && *other < 0) + || (r < 0 && *other > 0) => (d - 1, r + *other), + (d, r) => (d, r), + } + } + + /// Calculates the Greatest Common Divisor (GCD) of the number and + /// `other`. The result is always positive. + #[inline] + fn gcd(&self, other: &$T) -> $T { + // Use Euclid's algorithm + let mut m = *self; + let mut n = *other; + while m != 0 { + let temp = m; + m = n % temp; + n = temp; + } + n.abs() + } + + /// Calculates the Lowest Common Multiple (LCM) of the number and + /// `other`. + #[inline] + fn lcm(&self, other: &$T) -> $T { + // should not have to recalculate abs + ((*self * *other) / self.gcd(other)).abs() + } + + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } + + /// Returns `true` if the number is divisible by `2` + #[inline] + fn is_even(&self) -> bool { self & 1 == 0 } + + /// Returns `true` if the number is not divisible by `2` + #[inline] + fn is_odd(&self) -> bool { !self.is_even() } + } + + #[cfg(test)] + mod $test_mod { + use Integer; + + /// Checks that the division rule holds for: + /// + /// - `n`: numerator (dividend) + /// - `d`: denominator (divisor) + /// - `qr`: quotient and remainder + #[cfg(test)] + fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) { + assert_eq!(d * q + r, n); + } + + #[test] + fn test_div_rem() { + fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) { + let (n,d) = nd; + let separate_div_rem = (n / d, n % d); + let combined_div_rem = n.div_rem(&d); + + assert_eq!(separate_div_rem, qr); + assert_eq!(combined_div_rem, qr); + + test_division_rule(nd, separate_div_rem); + test_division_rule(nd, combined_div_rem); + } + + test_nd_dr(( 8, 3), ( 2, 2)); + test_nd_dr(( 8, -3), (-2, 2)); + test_nd_dr((-8, 3), (-2, -2)); + test_nd_dr((-8, -3), ( 2, -2)); + + test_nd_dr(( 1, 2), ( 0, 1)); + test_nd_dr(( 1, -2), ( 0, 1)); + test_nd_dr((-1, 2), ( 0, -1)); + test_nd_dr((-1, -2), ( 0, -1)); + } + + #[test] + fn test_div_mod_floor() { + fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) { + let (n,d) = nd; + let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d)); + let combined_div_mod_floor = n.div_mod_floor(&d); + + assert_eq!(separate_div_mod_floor, dm); + assert_eq!(combined_div_mod_floor, dm); + + test_division_rule(nd, separate_div_mod_floor); + test_division_rule(nd, combined_div_mod_floor); + } + + test_nd_dm(( 8, 3), ( 2, 2)); + test_nd_dm(( 8, -3), (-3, -1)); + test_nd_dm((-8, 3), (-3, 1)); + test_nd_dm((-8, -3), ( 2, -2)); + + test_nd_dm(( 1, 2), ( 0, 1)); + test_nd_dm(( 1, -2), (-1, -1)); + test_nd_dm((-1, 2), (-1, 1)); + test_nd_dm((-1, -2), ( 0, -1)); + } + + #[test] + fn test_gcd() { + assert_eq!((10 as $T).gcd(&2), 2 as $T); + assert_eq!((10 as $T).gcd(&3), 1 as $T); + assert_eq!((0 as $T).gcd(&3), 3 as $T); + assert_eq!((3 as $T).gcd(&3), 3 as $T); + assert_eq!((56 as $T).gcd(&42), 14 as $T); + assert_eq!((3 as $T).gcd(&-3), 3 as $T); + assert_eq!((-6 as $T).gcd(&3), 3 as $T); + assert_eq!((-4 as $T).gcd(&-2), 2 as $T); + } + + #[test] + fn test_lcm() { + assert_eq!((1 as $T).lcm(&0), 0 as $T); + assert_eq!((0 as $T).lcm(&1), 0 as $T); + assert_eq!((1 as $T).lcm(&1), 1 as $T); + assert_eq!((-1 as $T).lcm(&1), 1 as $T); + assert_eq!((1 as $T).lcm(&-1), 1 as $T); + assert_eq!((-1 as $T).lcm(&-1), 1 as $T); + assert_eq!((8 as $T).lcm(&9), 72 as $T); + assert_eq!((11 as $T).lcm(&5), 55 as $T); + } + + #[test] + fn test_even() { + assert_eq!((-4 as $T).is_even(), true); + assert_eq!((-3 as $T).is_even(), false); + assert_eq!((-2 as $T).is_even(), true); + assert_eq!((-1 as $T).is_even(), false); + assert_eq!((0 as $T).is_even(), true); + assert_eq!((1 as $T).is_even(), false); + assert_eq!((2 as $T).is_even(), true); + assert_eq!((3 as $T).is_even(), false); + assert_eq!((4 as $T).is_even(), true); + } + + #[test] + fn test_odd() { + assert_eq!((-4 as $T).is_odd(), false); + assert_eq!((-3 as $T).is_odd(), true); + assert_eq!((-2 as $T).is_odd(), false); + assert_eq!((-1 as $T).is_odd(), true); + assert_eq!((0 as $T).is_odd(), false); + assert_eq!((1 as $T).is_odd(), true); + assert_eq!((2 as $T).is_odd(), false); + assert_eq!((3 as $T).is_odd(), true); + assert_eq!((4 as $T).is_odd(), false); + } + } + ) +} + +impl_integer_for_int!(i8, test_integer_i8) +impl_integer_for_int!(i16, test_integer_i16) +impl_integer_for_int!(i32, test_integer_i32) +impl_integer_for_int!(i64, test_integer_i64) +impl_integer_for_int!(int, test_integer_int) + +macro_rules! impl_integer_for_uint { + ($T:ty, $test_mod:ident) => ( + impl Integer for $T { + /// Unsigned integer division. Returns the same result as `div` (`/`). + #[inline] + fn div_floor(&self, other: &$T) -> $T { *self / *other } + + /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`). + #[inline] + fn mod_floor(&self, other: &$T) -> $T { *self % *other } + + /// Calculates the Greatest Common Divisor (GCD) of the number and `other` + #[inline] + fn gcd(&self, other: &$T) -> $T { + // Use Euclid's algorithm + let mut m = *self; + let mut n = *other; + while m != 0 { + let temp = m; + m = n % temp; + n = temp; + } + n + } + + /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. + #[inline] + fn lcm(&self, other: &$T) -> $T { + (*self * *other) / self.gcd(other) + } + + /// Deprecated, use `is_multiple_of` instead. + #[deprecated = "function renamed to `is_multiple_of`"] + #[inline] + fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); } + + /// Returns `true` if the number is a multiple of `other`. + #[inline] + fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 } + + /// Returns `true` if the number is divisible by `2`. + #[inline] + fn is_even(&self) -> bool { self & 1 == 0 } + + /// Returns `true` if the number is not divisible by `2`. + #[inline] + fn is_odd(&self) -> bool { !self.is_even() } + } + + #[cfg(test)] + mod $test_mod { + use Integer; + + #[test] + fn test_div_mod_floor() { + assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T); + assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T); + assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T)); + assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T); + assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T); + assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T)); + assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T); + assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T); + assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T)); + } + + #[test] + fn test_gcd() { + assert_eq!((10 as $T).gcd(&2), 2 as $T); + assert_eq!((10 as $T).gcd(&3), 1 as $T); + assert_eq!((0 as $T).gcd(&3), 3 as $T); + assert_eq!((3 as $T).gcd(&3), 3 as $T); + assert_eq!((56 as $T).gcd(&42), 14 as $T); + } + + #[test] + fn test_lcm() { + assert_eq!((1 as $T).lcm(&0), 0 as $T); + assert_eq!((0 as $T).lcm(&1), 0 as $T); + assert_eq!((1 as $T).lcm(&1), 1 as $T); + assert_eq!((8 as $T).lcm(&9), 72 as $T); + assert_eq!((11 as $T).lcm(&5), 55 as $T); + assert_eq!((99 as $T).lcm(&17), 1683 as $T); + } + + #[test] + fn test_is_multiple_of() { + assert!((6 as $T).is_multiple_of(&(6 as $T))); + assert!((6 as $T).is_multiple_of(&(3 as $T))); + assert!((6 as $T).is_multiple_of(&(1 as $T))); + } + + #[test] + fn test_even() { + assert_eq!((0 as $T).is_even(), true); + assert_eq!((1 as $T).is_even(), false); + assert_eq!((2 as $T).is_even(), true); + assert_eq!((3 as $T).is_even(), false); + assert_eq!((4 as $T).is_even(), true); + } + + #[test] + fn test_odd() { + assert_eq!((0 as $T).is_odd(), false); + assert_eq!((1 as $T).is_odd(), true); + assert_eq!((2 as $T).is_odd(), false); + assert_eq!((3 as $T).is_odd(), true); + assert_eq!((4 as $T).is_odd(), false); + } + } + ) +} + +impl_integer_for_uint!(u8, test_integer_u8) +impl_integer_for_uint!(u16, test_integer_u16) +impl_integer_for_uint!(u32, test_integer_u32) +impl_integer_for_uint!(u64, test_integer_u64) +impl_integer_for_uint!(uint, test_integer_uint) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..f12279b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,69 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Simple numerics. +//! +//! This crate contains arbitrary-sized integer, rational, and complex types. +//! +//! ## Example +//! +//! This example uses the BigRational type and [Newton's method][newt] to +//! approximate a square root to arbitrary precision: +//! +//! ``` +//! extern crate num; +//! +//! use num::bigint::BigInt; +//! use num::rational::{Ratio, BigRational}; +//! +//! fn approx_sqrt(number: u64, iterations: uint) -> BigRational { +//! let start: Ratio = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); +//! let mut approx = start.clone(); +//! +//! for _ in range(0, iterations) { +//! approx = (approx + (start / approx)) / +//! Ratio::from_integer(FromPrimitive::from_u64(2).unwrap()); +//! } +//! +//! approx +//! } +//! +//! fn main() { +//! println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416 +//! } +//! ``` +//! +//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method + +#![feature(macro_rules)] +#![feature(default_type_params)] + +#![crate_name = "num"] +#![experimental] +#![crate_type = "rlib"] +#![crate_type = "dylib"] +#![license = "MIT/ASL2"] +#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", + html_favicon_url = "http://www.rust-lang.org/favicon.ico", + html_root_url = "http://doc.rust-lang.org/master/", + html_playground_url = "http://play.rust-lang.org/")] +#![allow(deprecated)] // from_str_radix + +extern crate rand; + +pub use bigint::{BigInt, BigUint}; +pub use rational::{Rational, BigRational}; +pub use complex::Complex; +pub use integer::Integer; + +pub mod bigint; +pub mod complex; +pub mod integer; +pub mod rational; diff --git a/src/rational.rs b/src/rational.rs new file mode 100644 index 0000000..0e33454 --- /dev/null +++ b/src/rational.rs @@ -0,0 +1,761 @@ +// Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Rational numbers + +use Integer; + +use std::cmp; +use std::fmt; +use std::from_str::FromStr; +use std::num; +use std::num::{Zero, One, ToStrRadix, FromStrRadix}; + +use bigint::{BigInt, BigUint, Sign, Plus, Minus}; + +/// Represents the ratio between 2 numbers. +#[deriving(Clone, Hash)] +#[allow(missing_doc)] +pub struct Ratio { + numer: T, + denom: T +} + +/// Alias for a `Ratio` of machine-sized integers. +pub type Rational = Ratio; +pub type Rational32 = Ratio; +pub type Rational64 = Ratio; + +/// Alias for arbitrary precision rationals. +pub type BigRational = Ratio; + +impl + Ratio { + /// Creates a ratio representing the integer `t`. + #[inline] + pub fn from_integer(t: T) -> Ratio { + Ratio::new_raw(t, One::one()) + } + + /// Creates a ratio without checking for `denom == 0` or reducing. + #[inline] + pub fn new_raw(numer: T, denom: T) -> Ratio { + Ratio { numer: numer, denom: denom } + } + + /// Create a new Ratio. Fails if `denom == 0`. + #[inline] + pub fn new(numer: T, denom: T) -> Ratio { + if denom == Zero::zero() { + fail!("denominator == 0"); + } + let mut ret = Ratio::new_raw(numer, denom); + ret.reduce(); + ret + } + + /// Converts to an integer. + #[inline] + pub fn to_integer(&self) -> T { + self.trunc().numer + } + + /// Gets an immutable reference to the numerator. + #[inline] + pub fn numer<'a>(&'a self) -> &'a T { + &self.numer + } + + /// Gets an immutable reference to the denominator. + #[inline] + pub fn denom<'a>(&'a self) -> &'a T { + &self.denom + } + + /// Returns true if the rational number is an integer (denominator is 1). + #[inline] + pub fn is_integer(&self) -> bool { + self.denom == One::one() + } + + /// Put self into lowest terms, with denom > 0. + fn reduce(&mut self) { + let g : T = self.numer.gcd(&self.denom); + + // FIXME(#5992): assignment operator overloads + // self.numer /= g; + self.numer = self.numer / g; + // FIXME(#5992): assignment operator overloads + // self.denom /= g; + self.denom = self.denom / g; + + // keep denom positive! + if self.denom < Zero::zero() { + self.numer = -self.numer; + self.denom = -self.denom; + } + } + + /// Returns a `reduce`d copy of self. + pub fn reduced(&self) -> Ratio { + let mut ret = self.clone(); + ret.reduce(); + ret + } + + /// Returns the reciprocal. + #[inline] + pub fn recip(&self) -> Ratio { + Ratio::new_raw(self.denom.clone(), self.numer.clone()) + } + + /// Rounds towards minus infinity. + #[inline] + pub fn floor(&self) -> Ratio { + if *self < Zero::zero() { + Ratio::from_integer((self.numer - self.denom + One::one()) / self.denom) + } else { + Ratio::from_integer(self.numer / self.denom) + } + } + + /// Rounds towards plus infinity. + #[inline] + pub fn ceil(&self) -> Ratio { + if *self < Zero::zero() { + Ratio::from_integer(self.numer / self.denom) + } else { + Ratio::from_integer((self.numer + self.denom - One::one()) / self.denom) + } + } + + /// Rounds to the nearest integer. Rounds half-way cases away from zero. + #[inline] + pub fn round(&self) -> Ratio { + if *self < Zero::zero() { + // a/b - 1/2 = (2*a - b)/(2*b) + Ratio::from_integer((self.numer + self.numer - self.denom) / (self.denom + self.denom)) + } else { + // a/b + 1/2 = (2*a + b)/(2*b) + Ratio::from_integer((self.numer + self.numer + self.denom) / (self.denom + self.denom)) + } + } + + /// Rounds towards zero. + #[inline] + pub fn trunc(&self) -> Ratio { + Ratio::from_integer(self.numer / self.denom) + } + + /// Returns the fractional part of a number. + #[inline] + pub fn fract(&self) -> Ratio { + Ratio::new_raw(self.numer % self.denom, self.denom.clone()) + } +} + +impl Ratio { + /// Converts a float into a rational number. + pub fn from_float(f: T) -> Option { + if !f.is_finite() { + return None; + } + let (mantissa, exponent, sign) = f.integer_decode(); + let bigint_sign: Sign = if sign == 1 { Plus } else { Minus }; + if exponent < 0 { + let one: BigInt = One::one(); + let denom: BigInt = one << ((-exponent) as uint); + let numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); + Some(Ratio::new(BigInt::from_biguint(bigint_sign, numer), denom)) + } else { + let mut numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); + numer = numer << (exponent as uint); + Some(Ratio::from_integer(BigInt::from_biguint(bigint_sign, numer))) + } + } +} + +/* Comparisons */ + +// comparing a/b and c/d is the same as comparing a*d and b*c, so we +// abstract that pattern. The following macro takes a trait and either +// a comma-separated list of "method name -> return value" or just +// "method name" (return value is bool in that case) +macro_rules! cmp_impl { + (impl $imp:ident, $($method:ident),+) => { + cmp_impl!(impl $imp, $($method -> bool),+) + }; + // return something other than a Ratio + (impl $imp:ident, $($method:ident -> $res:ty),*) => { + impl + $imp> $imp for Ratio { + $( + #[inline] + fn $method(&self, other: &Ratio) -> $res { + (self.numer * other.denom). $method (&(self.denom*other.numer)) + } + )* + } + }; +} +cmp_impl!(impl PartialEq, eq, ne) +cmp_impl!(impl PartialOrd, lt -> bool, gt -> bool, le -> bool, ge -> bool, + partial_cmp -> Option) +cmp_impl!(impl Eq, ) +cmp_impl!(impl Ord, cmp -> cmp::Ordering) + +/* Arithmetic */ +// a/b * c/d = (a*c)/(b*d) +impl + Mul,Ratio> for Ratio { + #[inline] + fn mul(&self, rhs: &Ratio) -> Ratio { + Ratio::new(self.numer * rhs.numer, self.denom * rhs.denom) + } +} + +// (a/b) / (c/d) = (a*d)/(b*c) +impl + Div,Ratio> for Ratio { + #[inline] + fn div(&self, rhs: &Ratio) -> Ratio { + Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer) + } +} + +// Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern +macro_rules! arith_impl { + (impl $imp:ident, $method:ident) => { + impl + $imp,Ratio> for Ratio { + #[inline] + fn $method(&self, rhs: &Ratio) -> Ratio { + Ratio::new((self.numer * rhs.denom).$method(&(self.denom * rhs.numer)), + self.denom * rhs.denom) + } + } + } +} + +// a/b + c/d = (a*d + b*c)/(b*d) +arith_impl!(impl Add, add) + +// a/b - c/d = (a*d - b*c)/(b*d) +arith_impl!(impl Sub, sub) + +// a/b % c/d = (a*d % b*c)/(b*d) +arith_impl!(impl Rem, rem) + +impl + Neg> for Ratio { + #[inline] + fn neg(&self) -> Ratio { + Ratio::new_raw(-self.numer, self.denom.clone()) + } +} + +/* Constants */ +impl + Zero for Ratio { + #[inline] + fn zero() -> Ratio { + Ratio::new_raw(Zero::zero(), One::one()) + } + + #[inline] + fn is_zero(&self) -> bool { + *self == Zero::zero() + } +} + +impl + One for Ratio { + #[inline] + fn one() -> Ratio { + Ratio::new_raw(One::one(), One::one()) + } +} + +impl + Num for Ratio {} + +impl + num::Signed for Ratio { + #[inline] + fn abs(&self) -> Ratio { + if self.is_negative() { -self.clone() } else { self.clone() } + } + + #[inline] + fn abs_sub(&self, other: &Ratio) -> Ratio { + if *self <= *other { Zero::zero() } else { *self - *other } + } + + #[inline] + fn signum(&self) -> Ratio { + if *self > Zero::zero() { + num::one() + } else if self.is_zero() { + num::zero() + } else { + - num::one::>() + } + } + + #[inline] + fn is_positive(&self) -> bool { *self > Zero::zero() } + + #[inline] + fn is_negative(&self) -> bool { *self < Zero::zero() } +} + +/* String conversions */ +impl fmt::Show for Ratio { + /// Renders as `numer/denom`. If denom=1, renders as numer. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.denom == One::one() { + write!(f, "{}", self.numer) + } else { + write!(f, "{}/{}", self.numer, self.denom) + } + } +} + +impl ToStrRadix for Ratio { + /// Renders as `numer/denom` where the numbers are in base `radix`. + fn to_str_radix(&self, radix: uint) -> String { + format!("{}/{}", + self.numer.to_str_radix(radix), + self.denom.to_str_radix(radix)) + } +} + +impl + FromStr for Ratio { + /// Parses `numer/denom` or just `numer`. + fn from_str(s: &str) -> Option> { + let mut split = s.splitn(1, '/'); + + let num = split.next().and_then(|n| FromStr::from_str(n)); + let den = split.next().or(Some("1")).and_then(|d| FromStr::from_str(d)); + + match (num, den) { + (Some(n), Some(d)) => Some(Ratio::new(n, d)), + _ => None + } + } +} + +impl + FromStrRadix for Ratio { + /// Parses `numer/denom` where the numbers are in base `radix`. + fn from_str_radix(s: &str, radix: uint) -> Option> { + let split: Vec<&str> = s.splitn(1, '/').collect(); + if split.len() < 2 { + None + } else { + let a_option: Option = FromStrRadix::from_str_radix( + *split.get(0), + radix); + a_option.and_then(|a| { + let b_option: Option = + FromStrRadix::from_str_radix(*split.get(1), radix); + b_option.and_then(|b| { + Some(Ratio::new(a.clone(), b.clone())) + }) + }) + } + } +} + +#[cfg(test)] +mod test { + + use super::{Ratio, Rational, BigRational}; + use std::num::{Zero, One, FromStrRadix, FromPrimitive, ToStrRadix}; + use std::from_str::FromStr; + use std::hash::hash; + use std::num; + + pub static _0 : Rational = Ratio { numer: 0, denom: 1}; + pub static _1 : Rational = Ratio { numer: 1, denom: 1}; + pub static _2: Rational = Ratio { numer: 2, denom: 1}; + pub static _1_2: Rational = Ratio { numer: 1, denom: 2}; + pub static _3_2: Rational = Ratio { numer: 3, denom: 2}; + pub static _neg1_2: Rational = Ratio { numer: -1, denom: 2}; + pub static _1_3: Rational = Ratio { numer: 1, denom: 3}; + pub static _neg1_3: Rational = Ratio { numer: -1, denom: 3}; + pub static _2_3: Rational = Ratio { numer: 2, denom: 3}; + pub static _neg2_3: Rational = Ratio { numer: -2, denom: 3}; + + pub fn to_big(n: Rational) -> BigRational { + Ratio::new( + FromPrimitive::from_int(n.numer).unwrap(), + FromPrimitive::from_int(n.denom).unwrap() + ) + } + + #[test] + fn test_test_constants() { + // check our constants are what Ratio::new etc. would make. + assert_eq!(_0, Zero::zero()); + assert_eq!(_1, One::one()); + assert_eq!(_2, Ratio::from_integer(2i)); + assert_eq!(_1_2, Ratio::new(1i,2i)); + assert_eq!(_3_2, Ratio::new(3i,2i)); + assert_eq!(_neg1_2, Ratio::new(-1i,2i)); + } + + #[test] + fn test_new_reduce() { + let one22 = Ratio::new(2i,2); + + assert_eq!(one22, One::one()); + } + #[test] + #[should_fail] + fn test_new_zero() { + let _a = Ratio::new(1i,0); + } + + + #[test] + fn test_cmp() { + assert!(_0 == _0 && _1 == _1); + assert!(_0 != _1 && _1 != _0); + assert!(_0 < _1 && !(_1 < _0)); + assert!(_1 > _0 && !(_0 > _1)); + + assert!(_0 <= _0 && _1 <= _1); + assert!(_0 <= _1 && !(_1 <= _0)); + + assert!(_0 >= _0 && _1 >= _1); + assert!(_1 >= _0 && !(_0 >= _1)); + } + + + #[test] + fn test_to_integer() { + assert_eq!(_0.to_integer(), 0); + assert_eq!(_1.to_integer(), 1); + assert_eq!(_2.to_integer(), 2); + assert_eq!(_1_2.to_integer(), 0); + assert_eq!(_3_2.to_integer(), 1); + assert_eq!(_neg1_2.to_integer(), 0); + } + + + #[test] + fn test_numer() { + assert_eq!(_0.numer(), &0); + assert_eq!(_1.numer(), &1); + assert_eq!(_2.numer(), &2); + assert_eq!(_1_2.numer(), &1); + assert_eq!(_3_2.numer(), &3); + assert_eq!(_neg1_2.numer(), &(-1)); + } + #[test] + fn test_denom() { + assert_eq!(_0.denom(), &1); + assert_eq!(_1.denom(), &1); + assert_eq!(_2.denom(), &1); + assert_eq!(_1_2.denom(), &2); + assert_eq!(_3_2.denom(), &2); + assert_eq!(_neg1_2.denom(), &2); + } + + + #[test] + fn test_is_integer() { + assert!(_0.is_integer()); + assert!(_1.is_integer()); + assert!(_2.is_integer()); + assert!(!_1_2.is_integer()); + assert!(!_3_2.is_integer()); + assert!(!_neg1_2.is_integer()); + } + + #[test] + fn test_show() { + assert_eq!(format!("{}", _2), "2".to_string()); + assert_eq!(format!("{}", _1_2), "1/2".to_string()); + assert_eq!(format!("{}", _0), "0".to_string()); + assert_eq!(format!("{}", Ratio::from_integer(-2i)), "-2".to_string()); + } + + mod arith { + use super::{_0, _1, _2, _1_2, _3_2, _neg1_2, to_big}; + use super::super::{Ratio, Rational}; + + #[test] + fn test_add() { + fn test(a: Rational, b: Rational, c: Rational) { + assert_eq!(a + b, c); + assert_eq!(to_big(a) + to_big(b), to_big(c)); + } + + test(_1, _1_2, _3_2); + test(_1, _1, _2); + test(_1_2, _3_2, _2); + test(_1_2, _neg1_2, _0); + } + + #[test] + fn test_sub() { + fn test(a: Rational, b: Rational, c: Rational) { + assert_eq!(a - b, c); + assert_eq!(to_big(a) - to_big(b), to_big(c)) + } + + test(_1, _1_2, _1_2); + test(_3_2, _1_2, _1); + test(_1, _neg1_2, _3_2); + } + + #[test] + fn test_mul() { + fn test(a: Rational, b: Rational, c: Rational) { + assert_eq!(a * b, c); + assert_eq!(to_big(a) * to_big(b), to_big(c)) + } + + test(_1, _1_2, _1_2); + test(_1_2, _3_2, Ratio::new(3i,4i)); + test(_1_2, _neg1_2, Ratio::new(-1i, 4i)); + } + + #[test] + fn test_div() { + fn test(a: Rational, b: Rational, c: Rational) { + assert_eq!(a / b, c); + assert_eq!(to_big(a) / to_big(b), to_big(c)) + } + + test(_1, _1_2, _2); + test(_3_2, _1_2, _1 + _2); + test(_1, _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2); + } + + #[test] + fn test_rem() { + fn test(a: Rational, b: Rational, c: Rational) { + assert_eq!(a % b, c); + assert_eq!(to_big(a) % to_big(b), to_big(c)) + } + + test(_3_2, _1, _1_2); + test(_2, _neg1_2, _0); + test(_1_2, _2, _1_2); + } + + #[test] + fn test_neg() { + fn test(a: Rational, b: Rational) { + assert_eq!(-a, b); + assert_eq!(-to_big(a), to_big(b)) + } + + test(_0, _0); + test(_1_2, _neg1_2); + test(-_1, _1); + } + #[test] + fn test_zero() { + assert_eq!(_0 + _0, _0); + assert_eq!(_0 * _0, _0); + assert_eq!(_0 * _1, _0); + assert_eq!(_0 / _neg1_2, _0); + assert_eq!(_0 - _0, _0); + } + #[test] + #[should_fail] + fn test_div_0() { + let _a = _1 / _0; + } + } + + #[test] + fn test_round() { + assert_eq!(_1_3.ceil(), _1); + assert_eq!(_1_3.floor(), _0); + assert_eq!(_1_3.round(), _0); + assert_eq!(_1_3.trunc(), _0); + + assert_eq!(_neg1_3.ceil(), _0); + assert_eq!(_neg1_3.floor(), -_1); + assert_eq!(_neg1_3.round(), _0); + assert_eq!(_neg1_3.trunc(), _0); + + assert_eq!(_2_3.ceil(), _1); + assert_eq!(_2_3.floor(), _0); + assert_eq!(_2_3.round(), _1); + assert_eq!(_2_3.trunc(), _0); + + assert_eq!(_neg2_3.ceil(), _0); + assert_eq!(_neg2_3.floor(), -_1); + assert_eq!(_neg2_3.round(), -_1); + assert_eq!(_neg2_3.trunc(), _0); + + assert_eq!(_1_2.ceil(), _1); + assert_eq!(_1_2.floor(), _0); + assert_eq!(_1_2.round(), _1); + assert_eq!(_1_2.trunc(), _0); + + assert_eq!(_neg1_2.ceil(), _0); + assert_eq!(_neg1_2.floor(), -_1); + assert_eq!(_neg1_2.round(), -_1); + assert_eq!(_neg1_2.trunc(), _0); + + assert_eq!(_1.ceil(), _1); + assert_eq!(_1.floor(), _1); + assert_eq!(_1.round(), _1); + assert_eq!(_1.trunc(), _1); + } + + #[test] + fn test_fract() { + assert_eq!(_1.fract(), _0); + assert_eq!(_neg1_2.fract(), _neg1_2); + assert_eq!(_1_2.fract(), _1_2); + assert_eq!(_3_2.fract(), _1_2); + } + + #[test] + fn test_recip() { + assert_eq!(_1 * _1.recip(), _1); + assert_eq!(_2 * _2.recip(), _1); + assert_eq!(_1_2 * _1_2.recip(), _1); + assert_eq!(_3_2 * _3_2.recip(), _1); + assert_eq!(_neg1_2 * _neg1_2.recip(), _1); + } + + #[test] + fn test_to_from_str() { + fn test(r: Rational, s: String) { + assert_eq!(FromStr::from_str(s.as_slice()), Some(r)); + assert_eq!(r.to_string(), s); + } + test(_1, "1".to_string()); + test(_0, "0".to_string()); + test(_1_2, "1/2".to_string()); + test(_3_2, "3/2".to_string()); + test(_2, "2".to_string()); + test(_neg1_2, "-1/2".to_string()); + } + #[test] + fn test_from_str_fail() { + fn test(s: &str) { + let rational: Option = FromStr::from_str(s); + assert_eq!(rational, None); + } + + let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1"]; + for &s in xs.iter() { + test(s); + } + } + + #[test] + fn test_to_from_str_radix() { + fn test(r: Rational, s: String, n: uint) { + assert_eq!(FromStrRadix::from_str_radix(s.as_slice(), n), + Some(r)); + assert_eq!(r.to_str_radix(n).to_string(), s); + } + fn test3(r: Rational, s: String) { test(r, s, 3) } + fn test16(r: Rational, s: String) { test(r, s, 16) } + + test3(_1, "1/1".to_string()); + test3(_0, "0/1".to_string()); + test3(_1_2, "1/2".to_string()); + test3(_3_2, "10/2".to_string()); + test3(_2, "2/1".to_string()); + test3(_neg1_2, "-1/2".to_string()); + test3(_neg1_2 / _2, "-1/11".to_string()); + + test16(_1, "1/1".to_string()); + test16(_0, "0/1".to_string()); + test16(_1_2, "1/2".to_string()); + test16(_3_2, "3/2".to_string()); + test16(_2, "2/1".to_string()); + test16(_neg1_2, "-1/2".to_string()); + test16(_neg1_2 / _2, "-1/4".to_string()); + test16(Ratio::new(13i,15i), "d/f".to_string()); + test16(_1_2*_1_2*_1_2*_1_2, "1/10".to_string()); + } + + #[test] + fn test_from_str_radix_fail() { + fn test(s: &str) { + let radix: Option = FromStrRadix::from_str_radix(s, 3); + assert_eq!(radix, None); + } + + let xs = ["0 /1", "abc", "", "1/", "--1/2","3/2/1", "3/2"]; + for &s in xs.iter() { + test(s); + } + } + + #[test] + fn test_from_float() { + fn test(given: T, (numer, denom): (&str, &str)) { + let ratio: BigRational = Ratio::from_float(given).unwrap(); + assert_eq!(ratio, Ratio::new( + FromStr::from_str(numer).unwrap(), + FromStr::from_str(denom).unwrap())); + } + + // f32 + test(3.14159265359f32, ("13176795", "4194304")); + test(2f32.powf(100.), ("1267650600228229401496703205376", "1")); + test(-2f32.powf(100.), ("-1267650600228229401496703205376", "1")); + test(1.0 / 2f32.powf(100.), ("1", "1267650600228229401496703205376")); + test(684729.48391f32, ("1369459", "2")); + test(-8573.5918555f32, ("-4389679", "512")); + + // f64 + test(3.14159265359f64, ("3537118876014453", "1125899906842624")); + test(2f64.powf(100.), ("1267650600228229401496703205376", "1")); + test(-2f64.powf(100.), ("-1267650600228229401496703205376", "1")); + test(684729.48391f64, ("367611342500051", "536870912")); + test(-8573.5918555f64, ("-4713381968463931", "549755813888")); + test(1.0 / 2f64.powf(100.), ("1", "1267650600228229401496703205376")); + } + + #[test] + fn test_from_float_fail() { + use std::{f32, f64}; + + assert_eq!(Ratio::from_float(f32::NAN), None); + assert_eq!(Ratio::from_float(f32::INFINITY), None); + assert_eq!(Ratio::from_float(f32::NEG_INFINITY), None); + assert_eq!(Ratio::from_float(f64::NAN), None); + assert_eq!(Ratio::from_float(f64::INFINITY), None); + assert_eq!(Ratio::from_float(f64::NEG_INFINITY), None); + } + + #[test] + fn test_signed() { + assert_eq!(_neg1_2.abs(), _1_2); + assert_eq!(_3_2.abs_sub(&_1_2), _1); + assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero()); + assert_eq!(_1_2.signum(), One::one()); + assert_eq!(_neg1_2.signum(), - num::one::>()); + assert!(_neg1_2.is_negative()); + assert!(! _neg1_2.is_positive()); + assert!(! _1_2.is_negative()); + } + + #[test] + fn test_hash() { + assert!(hash(&_0) != hash(&_1)); + assert!(hash(&_0) != hash(&_3_2)); + } +}