Skip to content

Commit

Permalink
auto merge of #6147 : bjz/rust/numeric-traits, r=brson
Browse files Browse the repository at this point in the history
After much discussion on IRC and #4819, we have decided to revert to the old naming of the `/` operator. This does not change its behavior. In making this change, we also have had to rename some of the methods in the `Integer` trait. Here is a list of the methods that have changed:

- `Quot::quot` -> `Div::div`
- `Rem::rem` - stays the same
- `Integer::quot_rem` -> `Integer::div_rem`
- `Integer::div` -> `Integer::div_floor`
- `Integer::modulo` -> `Integer::mod_floor`
- `Integer::div_mod` -> `Integer::div_mod_floor`
  • Loading branch information
bors committed May 1, 2013
2 parents bfccfdc + ee26c7c commit f67239f
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 235 deletions.
6 changes: 3 additions & 3 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1467,8 +1467,8 @@ A complete list of the built-in language items follows:
: Elements can be subtracted.
`mul`
: Elements can be multiplied.
`quot`
: Elements have a quotient operation.
`div`
: Elements have a division operation.
`rem`
: Elements have a remainder operation.
`neg`
Expand Down Expand Up @@ -1857,7 +1857,7 @@ The default meaning of the operators on standard types is given here.
Calls the `mul` method on the `core::ops::Mul` trait.
`/`
: Quotient.
Calls the `quot` method on the `core::ops::Quot` trait.
Calls the `div` method on the `core::ops::Div` trait.
`%`
: Remainder.
Calls the `rem` method on the `core::ops::Rem` trait.
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub use ops::{Drop};
#[cfg(stage0)]
pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
#[cfg(not(stage0))]
pub use ops::{Add, Sub, Mul, Quot, Rem, Neg, Not};
pub use ops::{Add, Sub, Mul, Div, Rem, Neg, Not};
pub use ops::{BitAnd, BitOr, BitXor};
pub use ops::{Shl, Shr, Index};

Expand Down
9 changes: 2 additions & 7 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn sub(x: f32, y: f32) -> f32 { return x - y; }
pub fn mul(x: f32, y: f32) -> f32 { return x * y; }

#[inline(always)]
pub fn quot(x: f32, y: f32) -> f32 { return x / y; }
pub fn div(x: f32, y: f32) -> f32 { return x / y; }

#[inline(always)]
pub fn rem(x: f32, y: f32) -> f32 { return x % y; }
Expand Down Expand Up @@ -279,16 +279,11 @@ impl Mul<f32,f32> for f32 {
fn mul(&self, other: &f32) -> f32 { *self * *other }
}

#[cfg(stage0,notest)]
#[cfg(notest)]
impl Div<f32,f32> for f32 {
#[inline(always)]
fn div(&self, other: &f32) -> f32 { *self / *other }
}
#[cfg(not(stage0),notest)]
impl Quot<f32,f32> for f32 {
#[inline(always)]
fn quot(&self, other: &f32) -> f32 { *self / *other }
}

#[cfg(stage0,notest)]
impl Modulo<f32,f32> for f32 {
Expand Down
9 changes: 2 additions & 7 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub fn sub(x: f64, y: f64) -> f64 { return x - y; }
pub fn mul(x: f64, y: f64) -> f64 { return x * y; }

#[inline(always)]
pub fn quot(x: f64, y: f64) -> f64 { return x / y; }
pub fn div(x: f64, y: f64) -> f64 { return x / y; }

#[inline(always)]
pub fn rem(x: f64, y: f64) -> f64 { return x % y; }
Expand Down Expand Up @@ -296,15 +296,10 @@ impl Sub<f64,f64> for f64 {
impl Mul<f64,f64> for f64 {
fn mul(&self, other: &f64) -> f64 { *self * *other }
}
#[cfg(stage0,notest)]
#[cfg(notest)]
impl Div<f64,f64> for f64 {
fn div(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(not(stage0),notest)]
impl Quot<f64,f64> for f64 {
#[inline(always)]
fn quot(&self, other: &f64) -> f64 { *self / *other }
}
#[cfg(stage0,notest)]
impl Modulo<f64,f64> for f64 {
fn modulo(&self, other: &f64) -> f64 { *self % *other }
Expand Down
10 changes: 3 additions & 7 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use libc::c_int;
use num::{Zero, One, strconv};
use prelude::*;

pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt};
pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt};
pub use f64::logarithm;
pub use f64::{acos, asin, atan2, cbrt, ceil, copysign, cosh, floor};
pub use f64::{erf, erfc, exp, expm1, exp2, abs_sub};
Expand Down Expand Up @@ -692,16 +692,12 @@ impl Mul<float,float> for float {
fn mul(&self, other: &float) -> float { *self * *other }
}

#[cfg(stage0,notest)]
#[cfg(notest)]
impl Div<float,float> for float {
#[inline(always)]
fn div(&self, other: &float) -> float { *self / *other }
}
#[cfg(not(stage0),notest)]
impl Quot<float,float> for float {
#[inline(always)]
fn quot(&self, other: &float) -> float { *self / *other }
}

#[cfg(stage0,notest)]
impl Modulo<float,float> for float {
#[inline(always)]
Expand Down
123 changes: 59 additions & 64 deletions src/libcore/num/int-template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn sub(x: T, y: T) -> T { x - y }
#[inline(always)]
pub fn mul(x: T, y: T) -> T { x * y }
#[inline(always)]
pub fn quot(x: T, y: T) -> T { x / y }
pub fn div(x: T, y: T) -> T { x / y }

///
/// Returns the remainder of y / x.
Expand Down Expand Up @@ -201,16 +201,11 @@ impl Mul<T,T> for T {
fn mul(&self, other: &T) -> T { *self * *other }
}

#[cfg(stage0,notest)]
#[cfg(notest)]
impl Div<T,T> for T {
#[inline(always)]
fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(not(stage0),notest)]
impl Quot<T,T> for T {
///
/// Returns the integer quotient, truncated towards 0. As this behaviour reflects
/// the underlying machine implementation it is more efficient than `Natural::div`.
/// Integer division, truncated towards 0. As this behaviour reflects the underlying
/// machine implementation it is more efficient than `Integer::div_floor`.
///
/// # Examples
///
Expand All @@ -227,7 +222,7 @@ impl Quot<T,T> for T {
/// ~~~
///
#[inline(always)]
fn quot(&self, other: &T) -> T { *self / *other }
fn div(&self, other: &T) -> T { *self / *other }
}

#[cfg(stage0,notest)]
Expand Down Expand Up @@ -307,51 +302,51 @@ impl Integer for T {
/// # Examples
///
/// ~~~
/// assert!(( 8).div( 3) == 2);
/// assert!(( 8).div(-3) == -3);
/// assert!((-8).div( 3) == -3);
/// assert!((-8).div(-3) == 2);
/// assert!(( 8).div_floor( 3) == 2);
/// assert!(( 8).div_floor(-3) == -3);
/// assert!((-8).div_floor( 3) == -3);
/// assert!((-8).div_floor(-3) == 2);
///
/// assert!(( 1).div( 2) == 0);
/// assert!(( 1).div(-2) == -1);
/// assert!((-1).div( 2) == -1);
/// assert!((-1).div(-2) == 0);
/// assert!(( 1).div_floor( 2) == 0);
/// assert!(( 1).div_floor(-2) == -1);
/// assert!((-1).div_floor( 2) == -1);
/// assert!((-1).div_floor(-2) == 0);
/// ~~~
///
#[inline(always)]
fn div(&self, other: &T) -> T {
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.quot_rem(other) {
(q, r) if (r > 0 && *other < 0)
|| (r < 0 && *other > 0) => q - 1,
(q, _) => q,
match self.div_rem(other) {
(d, r) if (r > 0 && *other < 0)
|| (r < 0 && *other > 0) => d - 1,
(d, _) => d,
}
}

///
/// Integer modulo, satisfying:
///
/// ~~~
/// assert!(n.div(d) * d + n.modulo(d) == n)
/// assert!(n.div_floor(d) * d + n.mod_floor(d) == n)
/// ~~~
///
/// # Examples
///
/// ~~~
/// assert!(( 8).modulo( 3) == 2);
/// assert!(( 8).modulo(-3) == -1);
/// assert!((-8).modulo( 3) == 1);
/// assert!((-8).modulo(-3) == -2);
/// assert!(( 8).mod_floor( 3) == 2);
/// assert!(( 8).mod_floor(-3) == -1);
/// assert!((-8).mod_floor( 3) == 1);
/// assert!((-8).mod_floor(-3) == -2);
///
/// assert!(( 1).modulo( 2) == 1);
/// assert!(( 1).modulo(-2) == -1);
/// assert!((-1).modulo( 2) == 1);
/// assert!((-1).modulo(-2) == -1);
/// assert!(( 1).mod_floor( 2) == 1);
/// assert!(( 1).mod_floor(-2) == -1);
/// assert!((-1).mod_floor( 2) == 1);
/// assert!((-1).mod_floor(-2) == -1);
/// ~~~
///
#[inline(always)]
fn modulo(&self, other: &T) -> T {
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 {
Expand All @@ -361,21 +356,21 @@ impl Integer for T {
}
}

/// Calculates `div` and `modulo` simultaneously
/// Calculates `div_floor` and `mod_floor` simultaneously
#[inline(always)]
fn div_mod(&self, other: &T) -> (T,T) {
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.quot_rem(other) {
(q, r) if (r > 0 && *other < 0)
|| (r < 0 && *other > 0) => (q - 1, r + *other),
(q, r) => (q, r),
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 `quot` (`\`) and `rem` (`%`) simultaneously
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
#[inline(always)]
fn quot_rem(&self, other: &T) -> (T,T) {
fn div_rem(&self, other: &T) -> (T,T) {
(*self / *other, *self % *other)
}

Expand Down Expand Up @@ -599,42 +594,42 @@ mod tests {
}

#[test]
fn test_quot_rem() {
fn test_nd_qr(nd: (T,T), qr: (T,T)) {
fn test_div_rem() {
fn test_nd_dr(nd: (T,T), qr: (T,T)) {
let (n,d) = nd;
let separate_quot_rem = (n / d, n % d);
let combined_quot_rem = n.quot_rem(&d);
let separate_div_rem = (n / d, n % d);
let combined_div_rem = n.div_rem(&d);

assert_eq!(separate_quot_rem, qr);
assert_eq!(combined_quot_rem, qr);
assert_eq!(separate_div_rem, qr);
assert_eq!(combined_div_rem, qr);

test_division_rule(nd, separate_quot_rem);
test_division_rule(nd, combined_quot_rem);
test_division_rule(nd, separate_div_rem);
test_division_rule(nd, combined_div_rem);
}

test_nd_qr(( 8, 3), ( 2, 2));
test_nd_qr(( 8, -3), (-2, 2));
test_nd_qr((-8, 3), (-2, -2));
test_nd_qr((-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((-8, -3), ( 2, -2));

test_nd_qr(( 1, 2), ( 0, 1));
test_nd_qr(( 1, -2), ( 0, 1));
test_nd_qr((-1, 2), ( 0, -1));
test_nd_qr((-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_nd_dr((-1, -2), ( 0, -1));
}

#[test]
fn test_div_mod() {
fn test_div_mod_floor() {
fn test_nd_dm(nd: (T,T), dm: (T,T)) {
let (n,d) = nd;
let separate_div_mod = (n.div(&d), n.modulo(&d));
let combined_div_mod = n.div_mod(&d);
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, dm);
assert_eq!(combined_div_mod, dm);
assert_eq!(separate_div_mod_floor, dm);
assert_eq!(combined_div_mod_floor, dm);

test_division_rule(nd, separate_div_mod);
test_division_rule(nd, combined_div_mod);
test_division_rule(nd, separate_div_mod_floor);
test_division_rule(nd, combined_div_mod_floor);
}

test_nd_dm(( 8, 3), ( 2, 2));
Expand Down
Loading

0 comments on commit f67239f

Please sign in to comment.