From fab5dfc876e83349416a1bb39099e085092f777c Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Sat, 11 Oct 2014 18:36:14 +0000 Subject: [PATCH 1/2] wip --- src/bignum/lib.rs | 75 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/src/bignum/lib.rs b/src/bignum/lib.rs index 9cb70b1..a7d31a0 100644 --- a/src/bignum/lib.rs +++ b/src/bignum/lib.rs @@ -10,7 +10,7 @@ extern crate gmp; extern crate num; extern crate rand; -use gmp::{Mpz, RandState}; +use gmp::{Mpz, Mpq, RandState}; use std::fmt; use std::from_str::FromStr; use std::num::{One, Zero, ToStrRadix}; @@ -18,6 +18,16 @@ use libc::c_ulong; use rand::Rng; use num::Integer; +pub mod bigint { + pub use BigInt; + pub use ToBigInt; +} + +pub mod rational { + pub use BigRational; + pub use BigRational as Ratio; +} + #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Zero)] pub struct BigUint { data: Mpz @@ -301,6 +311,27 @@ impl ToBigInt for BigUint { } } +macro_rules! impl_to_bigint( + ($T:ty, $from_ty:path) => { + impl ToBigInt for $T { + 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) + pub trait RandBigInt { /// Generate a random `BigUint` of the given bit size. fn gen_biguint(&mut self, bit_size: uint) -> BigUint; @@ -351,6 +382,48 @@ impl RandBigInt for R { } } +#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Zero)] +pub struct BigRational { + data: Mpq +} + +impl BigRational { + pub fn new(numer: BigInt, denom: BigInt) -> BigRational { + BigRational::from_integer(numer) / BigRational::from_integer(denom) + } + + pub fn from_integer(t: BigInt) -> BigRational { + let mut res = BigRational{ data: Mpq::new() }; + res.data.set_z(&t.data); + + res + } +} + +impl One for BigRational { + fn one() -> BigRational { + BigRational{ data: One::one() } + } +} + +impl Add for BigRational { + fn add(&self, other: &BigRational) -> BigRational { + BigRational{ data: self.data.add(&other.data) } + } +} + +impl Mul for BigRational { + fn mul(&self, other: &BigRational) -> BigRational { + BigRational{ data: self.data.mul(&other.data) } + } +} + +impl Div for BigRational { + fn div(&self, other: &BigRational) -> BigRational { + BigRational{ data: self.data.div(&other.data) } + } +} + #[cfg(test)] mod test_biguint { use super::{BigUint, RandBigInt, ToBigUint, ToBigInt}; From 5d7d302df5cac0dc30d3f65397b2ed8e3df99e34 Mon Sep 17 00:00:00 2001 From: "Ricardo M. Correia" Date: Mon, 13 Oct 2014 19:29:08 +0200 Subject: [PATCH 2/2] wip --- src/bignum/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bignum/lib.rs b/src/bignum/lib.rs index a7d31a0..8750de2 100644 --- a/src/bignum/lib.rs +++ b/src/bignum/lib.rs @@ -25,7 +25,7 @@ pub mod bigint { pub mod rational { pub use BigRational; - pub use BigRational as Ratio; + pub use BigRational as Ratio; // Hack to allow Ratio::new() and friends to work } #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Zero)]