Skip to content

Divide Num into separate traits #8049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/libextra/num/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A BigInt is a combination of BigUint and Sign.
use std::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
use std::int;
use std::num;
use std::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, Orderable};
use std::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, OrderedRing};
use std::str;
use std::uint;
use std::vec;
Expand Down Expand Up @@ -151,9 +151,11 @@ impl FromStr for BigUint {
}
}

impl Num for BigUint {}
impl Semiring for BigUint;

impl Orderable for BigUint {
impl Ring for BigUint;

impl OrderedRing for BigUint {

fn min(&self, other: &BigUint) -> BigUint {
if self < other { self.clone() } else { other.clone() }
Expand Down Expand Up @@ -202,7 +204,7 @@ impl One for BigUint {
fn one() -> BigUint { BigUint::new(~[1]) }
}

impl Unsigned for BigUint {}
impl Unsigned for BigUint;

impl Add<BigUint, BigUint> for BigUint {

Expand Down Expand Up @@ -826,9 +828,11 @@ impl FromStr for BigInt {
}
}

impl Num for BigInt {}
impl Semiring for BigInt;

impl Ring for BigInt;

impl Orderable for BigInt {
impl OrderedRing for BigInt {

fn min(&self, other: &BigInt) -> BigInt {
if self < other { self.clone() } else { other.clone() }
Expand Down
24 changes: 12 additions & 12 deletions src/libextra/num/complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub type Complex = Cmplx<float>;
pub type Complex32 = Cmplx<f32>;
pub type Complex64 = Cmplx<f64>;

impl<T: Clone + Num> Cmplx<T> {
impl<T: Clone + Field> Cmplx<T> {
/// Create a new Cmplx
#[inline]
pub fn new(re: T, im: T) -> Cmplx<T> {
Expand Down Expand Up @@ -79,15 +79,15 @@ impl<T: Clone + Num> Cmplx<T> {
}
}

impl<T: Clone + Algebraic + Num> Cmplx<T> {
impl<T: Clone + Algebraic + Field> Cmplx<T> {
/// Calculate |self|
#[inline]
pub fn norm(&self) -> T {
self.re.hypot(&self.im)
}
}

impl<T: Clone + Trigonometric + Algebraic + Num> Cmplx<T> {
impl<T: Clone + Trigonometric + Algebraic + Field> Cmplx<T> {
/// Calculate the principal Arg of self.
#[inline]
pub fn arg(&self) -> T {
Expand All @@ -108,21 +108,21 @@ impl<T: Clone + Trigonometric + Algebraic + Num> Cmplx<T> {

/* arithmetic */
// (a + i b) + (c + i d) == (a + c) + i (b + d)
impl<T: Clone + Num> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
impl<T: Clone + Field> Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
#[inline]
fn add(&self, other: &Cmplx<T>) -> Cmplx<T> {
Cmplx::new(self.re + other.re, self.im + other.im)
}
}
// (a + i b) - (c + i d) == (a - c) + i (b - d)
impl<T: Clone + Num> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
impl<T: Clone + Field> Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
#[inline]
fn sub(&self, other: &Cmplx<T>) -> Cmplx<T> {
Cmplx::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<T: Clone + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
impl<T: Clone + Field> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
#[inline]
fn mul(&self, other: &Cmplx<T>) -> Cmplx<T> {
Cmplx::new(self.re*other.re - self.im*other.im,
Expand All @@ -132,7 +132,7 @@ impl<T: Clone + Num> Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {

// (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<T: Clone + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
impl<T: Clone + Field> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
#[inline]
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
let norm_sqr = other.norm_sqr();
Expand All @@ -141,15 +141,15 @@ impl<T: Clone + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
}
}

impl<T: Clone + Num> Neg<Cmplx<T>> for Cmplx<T> {
impl<T: Clone + Field> Neg<Cmplx<T>> for Cmplx<T> {
#[inline]
fn neg(&self) -> Cmplx<T> {
Cmplx::new(-self.re, -self.im)
}
}

/* constants */
impl<T: Clone + Num> Zero for Cmplx<T> {
impl<T: Clone + Field> Zero for Cmplx<T> {
#[inline]
fn zero() -> Cmplx<T> {
Cmplx::new(Zero::zero(), Zero::zero())
Expand All @@ -161,15 +161,15 @@ impl<T: Clone + Num> Zero for Cmplx<T> {
}
}

impl<T: Clone + Num> One for Cmplx<T> {
impl<T: Clone + Field> One for Cmplx<T> {
#[inline]
fn one() -> Cmplx<T> {
Cmplx::new(One::one(), Zero::zero())
}
}

/* string conversions */
impl<T: ToStr + Num + Ord> ToStr for Cmplx<T> {
impl<T: ToStr + Field + Ord> ToStr for Cmplx<T> {
fn to_str(&self) -> ~str {
if self.im < Zero::zero() {
fmt!("%s-%si", self.re.to_str(), (-self.im).to_str())
Expand All @@ -179,7 +179,7 @@ impl<T: ToStr + Num + Ord> ToStr for Cmplx<T> {
}
}

impl<T: ToStrRadix + Num + Ord> ToStrRadix for Cmplx<T> {
impl<T: ToStrRadix + Field + Ord> ToStrRadix for Cmplx<T> {
fn to_str_radix(&self, radix: uint) -> ~str {
if self.im < Zero::zero() {
fmt!("%s-%si", self.re.to_str_radix(radix), (-self.im).to_str_radix(radix))
Expand Down
11 changes: 7 additions & 4 deletions src/libextra/num/rational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ cmp_impl!(impl TotalEq, equals)
cmp_impl!(impl Ord, lt, gt, le, ge)
cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering)

impl<T: Clone + Integer + Ord> Orderable for Ratio<T> {
impl<T: Clone + Integer + Ord> OrderedRing for Ratio<T> {
#[inline]
fn min(&self, other: &Ratio<T>) -> Ratio<T> {
if *self < *other { self.clone() } else { other.clone() }
Expand Down Expand Up @@ -201,8 +201,9 @@ impl<T: Clone + Integer + Ord>
}
}

impl<T: Clone + Integer + Ord>
Num for Ratio<T> {}
impl<T: Clone + Integer + Ord> Semiring for Ratio<T>;

impl<T: Clone + Integer + Ord> Ring for Ratio<T>;

/* Utils */
impl<T: Clone + Integer + Ord>
Expand Down Expand Up @@ -243,13 +244,15 @@ impl<T: Clone + Integer + Ord>
}
}

impl<T: Clone + Integer + Ord> Fractional for Ratio<T> {
impl<T: Clone + Integer + Ord> Field for Ratio<T> {
#[inline]
fn recip(&self) -> Ratio<T> {
Ratio::new_raw(self.denom.clone(), self.numer.clone())
}
}

impl<T: Clone + Integer + Ord> Fractional for Ratio<T>;

/* String conversions */
impl<T: ToStr> ToStr for Ratio<T> {
/// Renders as `numer/denom`.
Expand Down
10 changes: 7 additions & 3 deletions src/libstd/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ pub mod consts {
pub static ln_10: f32 = 2.30258509299404568401799145468436421_f32;
}

impl Num for f32 {}
impl Semiring for f32;

impl Ring for f32;

#[cfg(not(test))]
impl Eq for f32 {
Expand Down Expand Up @@ -203,7 +205,7 @@ impl Ord for f32 {
fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
}

impl Orderable for f32 {
impl OrderedRing for f32 {
/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn min(&self, other: &f32) -> f32 {
Expand Down Expand Up @@ -350,12 +352,14 @@ impl Round for f32 {
fn fract(&self) -> f32 { *self - self.trunc() }
}

impl Fractional for f32 {
impl Field for f32 {
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f32 { 1.0 / *self }
}

impl Fractional for f32;

impl Algebraic for f32 {
#[inline]
fn pow(&self, n: &f32) -> f32 { pow(*self, *n) }
Expand Down
10 changes: 7 additions & 3 deletions src/libstd/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ pub mod consts {
pub static ln_10: f64 = 2.30258509299404568401799145468436421_f64;
}

impl Num for f64 {}
impl Semiring for f64;

impl Ring for f64;

#[cfg(not(test))]
impl Eq for f64 {
Expand Down Expand Up @@ -226,7 +228,7 @@ impl Ord for f64 {
fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
}

impl Orderable for f64 {
impl OrderedRing for f64 {
/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn min(&self, other: &f64) -> f64 {
Expand Down Expand Up @@ -363,12 +365,14 @@ impl Round for f64 {
fn fract(&self) -> f64 { *self - self.trunc() }
}

impl Fractional for f64 {
impl Field for f64 {
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> f64 { 1.0 / *self }
}

impl Fractional for f64;

impl Algebraic for f64 {
#[inline]
fn pow(&self, n: &f64) -> f64 { pow(*self, *n) }
Expand Down
10 changes: 7 additions & 3 deletions src/libstd/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,9 @@ pub fn pow_with_uint(base: uint, pow: uint) -> float {
return total;
}

impl Num for float {}
impl Semiring for float;

impl Ring for float;

#[cfg(not(test))]
impl Eq for float {
Expand Down Expand Up @@ -370,7 +372,7 @@ impl Ord for float {
fn gt(&self, other: &float) -> bool { (*self) > (*other) }
}

impl Orderable for float {
impl OrderedRing for float {
/// Returns `NaN` if either of the numbers are `NaN`.
#[inline]
fn min(&self, other: &float) -> float {
Expand Down Expand Up @@ -433,12 +435,14 @@ impl Round for float {
fn fract(&self) -> float { *self - self.trunc() }
}

impl Fractional for float {
impl Field for float {
/// The reciprocal (multiplicative inverse) of the number
#[inline]
fn recip(&self) -> float { 1.0 / *self }
}

impl Fractional for float;

impl Algebraic for float {
#[inline]
fn pow(&self, n: &float) -> float {
Expand Down
10 changes: 6 additions & 4 deletions src/libstd/num/int_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ pub fn range_rev(hi: $T, lo: $T, it: &fn($T) -> bool) -> bool {
range_step_inclusive(hi-1, lo, -1 as $T, it)
}

impl Num for $T {}
impl Semiring for $T;

impl Ring for $T;

#[cfg(not(test))]
impl Ord for $T {
Expand All @@ -160,7 +162,7 @@ impl Eq for $T {
fn ne(&self, other: &$T) -> bool { return (*self) != (*other); }
}

impl Orderable for $T {
impl OrderedRing for $T {
#[inline]
fn min(&self, other: &$T) -> $T {
if *self < *other { *self } else { *other }
Expand Down Expand Up @@ -425,7 +427,7 @@ impl Integer for $T {
fn is_odd(&self) -> bool { !self.is_even() }
}

impl Bitwise for $T {}
impl Bitwise for $T;

#[cfg(not(test))]
impl BitOr<$T,$T> for $T {
Expand Down Expand Up @@ -471,7 +473,7 @@ impl Bounded for $T {
fn max_value() -> $T { max_value }
}

impl Int for $T {}
impl Int for $T;

impl Primitive for $T {
#[inline]
Expand Down
Loading