auto merge of #6147 : bjz/rust/numeric-traits, r=brson

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`
This commit is contained in:
bors 2013-05-01 01:51:35 -07:00
commit f67239fac3
27 changed files with 201 additions and 237 deletions

View File

@ -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`
@ -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.

View File

@ -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};

View File

@ -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; }
@ -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 {

View File

@ -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; }
@ -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 }

View File

@ -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};
@ -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)]

View File

@ -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.
@ -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
///
@ -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)]
@ -307,25 +302,25 @@ 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,
}
}
@ -333,25 +328,25 @@ impl Integer for T {
/// 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 {
@ -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)
}
@ -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));

View File

@ -11,13 +11,11 @@
//! An interface for numeric types
use cmp::{Eq, Ord};
#[cfg(stage0)]
use ops::{Add, Sub, Mul, Neg};
#[cfg(stage0)]
use Quot = ops::Div;
use ops::{Add, Sub, Mul, Div, Neg};
#[cfg(stage0)]
use Rem = ops::Modulo;
#[cfg(not(stage0))]
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
use option::Option;
use kinds::Copy;
@ -32,7 +30,7 @@ pub trait Num: Eq + Zero + One
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Quot<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}
pub trait IntConvertible {
@ -76,12 +74,13 @@ pub fn abs<T:Ord + Zero + Neg<T>>(v: T) -> T {
pub trait Integer: Num
+ Orderable
+ Quot<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {
fn div(&self, other: &Self) -> Self;
fn modulo(&self, other: &Self) -> Self;
fn div_mod(&self, other: &Self) -> (Self,Self);
fn quot_rem(&self, other: &Self) -> (Self,Self);
fn div_rem(&self, other: &Self) -> (Self,Self);
fn div_floor(&self, other: &Self) -> Self;
fn mod_floor(&self, other: &Self) -> Self;
fn div_mod_floor(&self, other: &Self) -> (Self,Self);
fn gcd(&self, other: &Self) -> Self;
fn lcm(&self, other: &Self) -> Self;
@ -102,7 +101,7 @@ pub trait Round {
pub trait Fractional: Num
+ Orderable
+ Round
+ Quot<Self,Self> {
+ Div<Self,Self> {
fn recip(&self) -> Self;
}
@ -226,7 +225,7 @@ pub trait Primitive: Num
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Quot<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {
// FIXME (#5527): These should be associated constants
fn bits() -> uint;
@ -371,7 +370,7 @@ pub trait FromStrRadix {
/// - If code written to use this function doesn't care about it, it's
/// probably assuming that `x^0` always equals `1`.
///
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Quot<T,T>+Mul<T,T>>(
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
radix: uint, pow: uint) -> T {
let _0: T = Zero::zero();
let _1: T = One::one();
@ -413,13 +412,13 @@ pub fn test_num<T:Num + NumCast>(ten: T, two: T) {
assert_eq!(ten.add(&two), cast(12));
assert_eq!(ten.sub(&two), cast(8));
assert_eq!(ten.mul(&two), cast(20));
assert_eq!(ten.quot(&two), cast(5));
assert_eq!(ten.div(&two), cast(5));
assert_eq!(ten.rem(&two), cast(0));
assert_eq!(ten.add(&two), ten + two);
assert_eq!(ten.sub(&two), ten - two);
assert_eq!(ten.mul(&two), ten * two);
assert_eq!(ten.quot(&two), ten / two);
assert_eq!(ten.div(&two), ten / two);
assert_eq!(ten.rem(&two), ten % two);
}

View File

@ -10,15 +10,13 @@
use core::cmp::{Ord, Eq};
#[cfg(stage0)]
use ops::{Add, Sub, Mul, Neg};
#[cfg(stage0)]
use Quot = ops::Div;
use ops::{Add, Sub, Mul, Div, Neg};
#[cfg(stage0)]
use Rem = ops::Modulo;
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
use ops::{Add, Sub, Mul, Div, Rem, Neg};
use option::{None, Option, Some};
use char;
use str;
@ -67,7 +65,7 @@ fn is_neg_inf<T:Eq+NumStrConv>(num: &T) -> bool {
}
#[inline(always)]
fn is_neg_zero<T:Eq+One+Zero+NumStrConv+Quot<T,T>>(num: &T) -> bool {
fn is_neg_zero<T:Eq+One+Zero+NumStrConv+Div<T,T>>(num: &T) -> bool {
let _0: T = Zero::zero();
let _1: T = One::one();
@ -180,7 +178,7 @@ static nan_buf: [u8, ..3] = ['N' as u8, 'a' as u8, 'N' as u8];
* - Fails if `radix` < 2 or `radix` > 36.
*/
pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
Quot<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
num: &T, radix: uint, negative_zero: bool,
sign: SignFormat, digits: SignificantDigits) -> (~[u8], bool) {
if (radix as int) < 2 {
@ -388,7 +386,7 @@ pub fn to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
*/
#[inline(always)]
pub fn to_str_common<T:NumCast+Zero+One+Eq+Ord+NumStrConv+Copy+
Quot<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
Div<T,T>+Neg<T>+Rem<T,T>+Mul<T,T>>(
num: &T, radix: uint, negative_zero: bool,
sign: SignFormat, digits: SignificantDigits) -> (~str, bool) {
let (bytes, special) = to_str_bytes_common(num, radix,
@ -441,7 +439,7 @@ priv static DIGIT_E_RADIX: uint = ('e' as uint) - ('a' as uint) + 11u;
* - Fails if `radix` > 18 and `special == true` due to conflict
* between digit and lowest first character in `inf` and `NaN`, the `'i'`.
*/
pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Quot<T,T>+
pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+
Mul<T,T>+Sub<T,T>+Neg<T>+Add<T,T>+
NumStrConv>(
buf: &[u8], radix: uint, negative: bool, fractional: bool,
@ -638,7 +636,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Quot<T,T>+
* `from_str_bytes_common()`, for details see there.
*/
#[inline(always)]
pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Quot<T,T>+Mul<T,T>+
pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+Mul<T,T>+
Sub<T,T>+Neg<T>+Add<T,T>+NumStrConv>(
buf: &str, radix: uint, negative: bool, fractional: bool,
special: bool, exponent: ExponentFormat, empty_zero: bool,

View File

@ -31,7 +31,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 }
#[inline(always)]
pub fn rem(x: T, y: T) -> T { x % y }
@ -166,16 +166,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 {
#[inline(always)]
fn quot(&self, other: &T) -> T { *self / *other }
}
#[cfg(stage0,notest)]
impl Modulo<T,T> for T {
@ -197,23 +192,23 @@ impl Neg<T> for T {
impl Unsigned for T {}
impl Integer for T {
/// Unsigned integer division. Returns the same result as `quot` (`/`).
/// Calculates `div` (`\`) and `rem` (`%`) simultaneously
#[inline(always)]
fn div(&self, other: &T) -> T { *self / *other }
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
#[inline(always)]
fn modulo(&self, other: &T) -> T { *self / *other }
/// Calculates `div` and `modulo` simultaneously
#[inline(always)]
fn div_mod(&self, other: &T) -> (T,T) {
fn div_rem(&self, other: &T) -> (T,T) {
(*self / *other, *self % *other)
}
/// Calculates `quot` (`\`) and `rem` (`%`) simultaneously
/// Unsigned integer division. Returns the same result as `div` (`/`).
#[inline(always)]
fn quot_rem(&self, other: &T) -> (T,T) {
fn div_floor(&self, other: &T) -> T { *self / *other }
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
#[inline(always)]
fn mod_floor(&self, other: &T) -> T { *self / *other }
/// Calculates `div_floor` and `modulo_floor` simultaneously
#[inline(always)]
fn div_mod_floor(&self, other: &T) -> (T,T) {
(*self / *other, *self % *other)
}

View File

@ -31,15 +31,9 @@ pub trait Mul<RHS,Result> {
}
#[lang="div"]
#[cfg(stage0)]
pub trait Div<RHS,Result> {
fn div(&self, rhs: &RHS) -> Result;
}
#[lang="quot"]
#[cfg(not(stage0))]
pub trait Quot<RHS,Result> {
fn quot(&self, rhs: &RHS) -> Result;
}
#[lang="modulo"]
#[cfg(stage0)]

View File

@ -17,7 +17,7 @@ pub use kinds::{Const, Copy, Owned, Durable};
#[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::{Drop};
pub use ops::{Shl, Shr, Index};

View File

@ -279,7 +279,7 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
add => Ok(const_float(a + b)),
subtract => Ok(const_float(a - b)),
mul => Ok(const_float(a * b)),
quot => Ok(const_float(a / b)),
div => Ok(const_float(a / b)),
rem => Ok(const_float(a % b)),
eq => fromb(a == b),
lt => fromb(a < b),
@ -295,8 +295,8 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
add => Ok(const_int(a + b)),
subtract => Ok(const_int(a - b)),
mul => Ok(const_int(a * b)),
quot if b == 0 => Err(~"attempted quotient with a divisor of zero"),
quot => Ok(const_int(a / b)),
div if b == 0 => Err(~"attempted to divide by zero"),
div => Ok(const_int(a / b)),
rem if b == 0 => Err(~"attempted remainder with a divisor of zero"),
rem => Ok(const_int(a % b)),
and | bitand => Ok(const_int(a & b)),
@ -317,8 +317,8 @@ pub fn eval_const_expr_partial(tcx: middle::ty::ctxt, e: @expr)
add => Ok(const_uint(a + b)),
subtract => Ok(const_uint(a - b)),
mul => Ok(const_uint(a * b)),
quot if b == 0 => Err(~"attempted quotient with a divisor of zero"),
quot => Ok(const_uint(a / b)),
div if b == 0 => Err(~"attempted to divide by zero"),
div => Ok(const_uint(a / b)),
rem if b == 0 => Err(~"attempted remainder with a divisor of zero"),
rem => Ok(const_uint(a % b)),
and | bitand => Ok(const_uint(a & b)),

View File

@ -42,7 +42,7 @@ pub enum LangItem {
AddTraitLangItem, // 5
SubTraitLangItem, // 6
MulTraitLangItem, // 7
QuotTraitLangItem, // 8
DivTraitLangItem, // 8
RemTraitLangItem, // 9
NegTraitLangItem, // 10
NotTraitLangItem, // 11
@ -105,7 +105,7 @@ pub impl LanguageItems {
5 => "add",
6 => "sub",
7 => "mul",
8 => "quot",
8 => "div",
9 => "rem",
10 => "neg",
11 => "not",
@ -167,8 +167,8 @@ pub impl LanguageItems {
pub fn mul_trait(&const self) -> def_id {
self.items[MulTraitLangItem as uint].get()
}
pub fn quot_trait(&const self) -> def_id {
self.items[QuotTraitLangItem as uint].get()
pub fn div_trait(&const self) -> def_id {
self.items[DivTraitLangItem as uint].get()
}
pub fn rem_trait(&const self) -> def_id {
self.items[RemTraitLangItem as uint].get()
@ -268,7 +268,7 @@ fn LanguageItemCollector<'r>(crate: @crate,
item_refs.insert(@~"add", AddTraitLangItem as uint);
item_refs.insert(@~"sub", SubTraitLangItem as uint);
item_refs.insert(@~"mul", MulTraitLangItem as uint);
item_refs.insert(@~"quot", QuotTraitLangItem as uint);
item_refs.insert(@~"div", DivTraitLangItem as uint);
item_refs.insert(@~"rem", RemTraitLangItem as uint);
item_refs.insert(@~"neg", NegTraitLangItem as uint);
item_refs.insert(@~"not", NotTraitLangItem as uint);

View File

@ -33,7 +33,7 @@ use syntax::ast::{def_upvar, def_use, def_variant, expr, expr_assign_op};
use syntax::ast::{expr_binary, expr_break, expr_field};
use syntax::ast::{expr_fn_block, expr_index, expr_method_call, expr_path};
use syntax::ast::{def_prim_ty, def_region, def_self, def_ty, def_ty_param};
use syntax::ast::{def_upvar, def_use, def_variant, quot, eq};
use syntax::ast::{def_upvar, def_use, def_variant, div, eq};
use syntax::ast::{expr, expr_again, expr_assign_op};
use syntax::ast::{expr_index, expr_loop};
use syntax::ast::{expr_path, expr_struct, expr_unary, fn_decl};
@ -4901,9 +4901,9 @@ pub impl Resolver {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.mul_trait());
}
expr_binary(quot, _, _) | expr_assign_op(quot, _, _) => {
expr_binary(div, _, _) | expr_assign_op(div, _, _) => {
self.add_fixed_trait_for_expr(expr.id,
self.lang_items.quot_trait());
self.lang_items.div_trait());
}
expr_binary(rem, _, _) | expr_assign_op(rem, _, _) => {
self.add_fixed_trait_for_expr(expr.id,

View File

@ -777,10 +777,10 @@ pub fn cast_shift_rhs(op: ast::binop,
}
}
pub fn fail_if_zero(cx: block, span: span, quotrem: ast::binop,
pub fn fail_if_zero(cx: block, span: span, divrem: ast::binop,
rhs: ValueRef, rhs_t: ty::t) -> block {
let text = if quotrem == ast::quot {
@~"attempted quotient with a divisor of zero"
let text = if divrem == ast::div {
@~"attempted to divide by zero"
} else {
@~"attempted remainder with a divisor of zero"
};

View File

@ -270,7 +270,7 @@ fn const_expr_unadjusted(cx: @CrateContext, e: @ast::expr) -> ValueRef {
if is_float { llvm::LLVMConstFMul(te1, te2) }
else { llvm::LLVMConstMul(te1, te2) }
}
ast::quot => {
ast::div => {
if is_float { llvm::LLVMConstFDiv(te1, te2) }
else if signed { llvm::LLVMConstSDiv(te1, te2) }
else { llvm::LLVMConstUDiv(te1, te2) }

View File

@ -1435,7 +1435,7 @@ fn trans_eager_binop(bcx: block,
if is_float { FMul(bcx, lhs, rhs) }
else { Mul(bcx, lhs, rhs) }
}
ast::quot => {
ast::div => {
if is_float {
FDiv(bcx, lhs, rhs)
} else {

View File

@ -4134,7 +4134,7 @@ pub fn is_binopable(_cx: ctxt, ty: t, op: ast::binop) -> bool {
ast::add => opcat_add,
ast::subtract => opcat_sub,
ast::mul => opcat_mult,
ast::quot => opcat_mult,
ast::div => opcat_mult,
ast::rem => opcat_mult,
ast::and => opcat_logic,
ast::or => opcat_logic,

View File

@ -293,10 +293,10 @@ impl Mul<BigUint, BigUint> for BigUint {
}
}
impl Quot<BigUint, BigUint> for BigUint {
impl Div<BigUint, BigUint> for BigUint {
#[inline(always)]
fn quot(&self, other: &BigUint) -> BigUint {
let (q, _) = self.quot_rem(other);
fn div(&self, other: &BigUint) -> BigUint {
let (q, _) = self.div_rem(other);
return q;
}
}
@ -304,7 +304,7 @@ impl Quot<BigUint, BigUint> for BigUint {
impl Rem<BigUint, BigUint> for BigUint {
#[inline(always)]
fn rem(&self, other: &BigUint) -> BigUint {
let (_, r) = self.quot_rem(other);
let (_, r) = self.div_rem(other);
return r;
}
}
@ -316,19 +316,24 @@ impl Neg<BigUint> for BigUint {
impl Integer for BigUint {
#[inline(always)]
fn div(&self, other: &BigUint) -> BigUint {
let (d, _) = self.div_mod(other);
fn div_rem(&self, other: &BigUint) -> (BigUint, BigUint) {
self.div_mod_floor(other)
}
#[inline(always)]
fn div_floor(&self, other: &BigUint) -> BigUint {
let (d, _) = self.div_mod_floor(other);
return d;
}
#[inline(always)]
fn modulo(&self, other: &BigUint) -> BigUint {
let (_, m) = self.div_mod(other);
fn mod_floor(&self, other: &BigUint) -> BigUint {
let (_, m) = self.div_mod_floor(other);
return m;
}
#[inline(always)]
fn div_mod(&self, other: &BigUint) -> (BigUint, BigUint) {
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 (copy *self, Zero::zero()); }
@ -346,11 +351,11 @@ impl Integer for BigUint {
shift += 1;
}
assert!(shift < BigDigit::bits);
let (d, m) = div_mod_inner(self << shift, other << shift);
let (d, m) = div_mod_floor_inner(self << shift, other << shift);
return (d, m >> shift);
#[inline(always)]
fn div_mod_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
fn div_mod_floor_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
let mut m = a;
let mut d = Zero::zero::<BigUint>();
let mut n = 1;
@ -409,11 +414,6 @@ impl Integer for BigUint {
}
}
#[inline(always)]
fn quot_rem(&self, other: &BigUint) -> (BigUint, BigUint) {
self.div_mod(other)
}
/**
* Calculates the Greatest Common Divisor (GCD) of the number and `other`
*
@ -485,7 +485,7 @@ impl ToStrRadix for BigUint {
let mut result = ~[];
let mut m = n;
while m > divider {
let (d, m0) = m.div_mod(&divider);
let (d, m0) = m.div_mod_floor(&divider);
result += [m0.to_uint() as BigDigit];
m = d;
}
@ -894,10 +894,10 @@ impl Mul<BigInt, BigInt> for BigInt {
}
}
impl Quot<BigInt, BigInt> for BigInt {
impl Div<BigInt, BigInt> for BigInt {
#[inline(always)]
fn quot(&self, other: &BigInt) -> BigInt {
let (q, _) = self.quot_rem(other);
fn div(&self, other: &BigInt) -> BigInt {
let (q, _) = self.div_rem(other);
return q;
}
}
@ -905,7 +905,7 @@ impl Quot<BigInt, BigInt> for BigInt {
impl Rem<BigInt, BigInt> for BigInt {
#[inline(always)]
fn rem(&self, other: &BigInt) -> BigInt {
let (_, r) = self.quot_rem(other);
let (_, r) = self.div_rem(other);
return r;
}
}
@ -919,21 +919,36 @@ impl Neg<BigInt> for BigInt {
impl Integer for BigInt {
#[inline(always)]
fn div(&self, other: &BigInt) -> BigInt {
let (d, _) = self.div_mod(other);
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(always)]
fn div_floor(&self, other: &BigInt) -> BigInt {
let (d, _) = self.div_mod_floor(other);
return d;
}
#[inline(always)]
fn modulo(&self, other: &BigInt) -> BigInt {
let (_, m) = self.div_mod(other);
fn mod_floor(&self, other: &BigInt) -> BigInt {
let (_, m) = self.div_mod_floor(other);
return m;
}
#[inline(always)]
fn div_mod(&self, other: &BigInt) -> (BigInt, BigInt) {
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
// m.sign == other.sign
let (d_ui, m_ui) = self.data.quot_rem(&other.data);
let (d_ui, m_ui) = self.data.div_rem(&other.data);
let d = BigInt::from_biguint(Plus, d_ui),
m = BigInt::from_biguint(Plus, m_ui);
match (self.sign, other.sign) {
@ -953,21 +968,6 @@ impl Integer for BigInt {
}
}
#[inline(always)]
fn quot_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
// r.sign == self.sign
let (q_ui, r_ui) = self.data.div_mod(&other.data);
let q = BigInt::from_biguint(Plus, q_ui);
let r = BigInt::from_biguint(Plus, r_ui);
match (self.sign, other.sign) {
(_, Zero) => fail!(),
(Plus, Plus) | (Zero, Plus) => ( q, r),
(Plus, Minus) | (Zero, Minus) => (-q, r),
(Minus, Plus) => (-q, -r),
(Minus, Minus) => ( q, -r)
}
}
/**
* Calculates the Greatest Common Divisor (GCD) of the number and `other`
*
@ -1100,8 +1100,6 @@ pub impl BigInt {
#[cfg(test)]
mod biguint_tests {
use core::*;
use core::num::{IntConvertible, Zero, One, FromStrRadix};
use core::cmp::{Less, Equal, Greater};
use super::{BigUint, BigDigit};
@ -1347,7 +1345,7 @@ mod biguint_tests {
(&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1])
];
static quot_rem_quadruples: &'static [(&'static [BigDigit],
static div_rem_quadruples: &'static [(&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit])]
@ -1371,7 +1369,7 @@ mod biguint_tests {
assert!(b * a == c);
}
for quot_rem_quadruples.each |elm| {
for div_rem_quadruples.each |elm| {
let (aVec, bVec, cVec, dVec) = *elm;
let a = BigUint::from_slice(aVec);
let b = BigUint::from_slice(bVec);
@ -1384,7 +1382,7 @@ mod biguint_tests {
}
#[test]
fn test_quot_rem() {
fn test_div_rem() {
for mul_triples.each |elm| {
let (aVec, bVec, cVec) = *elm;
let a = BigUint::from_slice(aVec);
@ -1392,21 +1390,21 @@ mod biguint_tests {
let c = BigUint::from_slice(cVec);
if !a.is_zero() {
assert!(c.quot_rem(&a) == (copy b, Zero::zero()));
assert!(c.div_rem(&a) == (copy b, Zero::zero()));
}
if !b.is_zero() {
assert!(c.quot_rem(&b) == (copy a, Zero::zero()));
assert!(c.div_rem(&b) == (copy a, Zero::zero()));
}
}
for quot_rem_quadruples.each |elm| {
for div_rem_quadruples.each |elm| {
let (aVec, bVec, cVec, dVec) = *elm;
let a = BigUint::from_slice(aVec);
let b = BigUint::from_slice(bVec);
let c = BigUint::from_slice(cVec);
let d = BigUint::from_slice(dVec);
if !b.is_zero() { assert!(a.quot_rem(&b) == (c, d)); }
if !b.is_zero() { assert!(a.div_rem(&b) == (c, d)); }
}
}
@ -1558,7 +1556,6 @@ mod biguint_tests {
#[cfg(test)]
mod bigint_tests {
use super::{BigInt, BigUint, BigDigit, Sign, Minus, Zero, Plus};
use core::*;
use core::cmp::{Less, Equal, Greater};
use core::num::{IntConvertible, Zero, One, FromStrRadix};
@ -1750,10 +1747,10 @@ mod bigint_tests {
(&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1])
];
static quot_rem_quadruples: &'static [(&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit])]
static div_rem_quadruples: &'static [(&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit],
&'static [BigDigit])]
= &[
(&[ 1], &[ 2], &[], &[1]),
(&[ 1, 1], &[ 2], &[-1/2+1], &[1]),
@ -1777,7 +1774,7 @@ mod bigint_tests {
assert!((-b) * a == -c);
}
for quot_rem_quadruples.each |elm| {
for div_rem_quadruples.each |elm| {
let (aVec, bVec, cVec, dVec) = *elm;
let a = BigInt::from_slice(Plus, aVec);
let b = BigInt::from_slice(Plus, bVec);
@ -1790,9 +1787,9 @@ mod bigint_tests {
}
#[test]
fn test_div_mod() {
fn test_div_mod_floor() {
fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
let (d, m) = a.div_mod(b);
let (d, m) = a.div_mod_floor(b);
if !m.is_zero() {
assert!(m.sign == b.sign);
}
@ -1826,7 +1823,7 @@ mod bigint_tests {
if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); }
}
for quot_rem_quadruples.each |elm| {
for div_rem_quadruples.each |elm| {
let (aVec, bVec, cVec, dVec) = *elm;
let a = BigInt::from_slice(Plus, aVec);
let b = BigInt::from_slice(Plus, bVec);
@ -1841,9 +1838,9 @@ mod bigint_tests {
#[test]
fn test_quot_rem() {
fn test_div_rem() {
fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) {
let (q, r) = a.quot_rem(b);
let (q, r) = a.div_rem(b);
if !r.is_zero() {
assert!(r.sign == a.sign);
}
@ -1869,7 +1866,7 @@ mod bigint_tests {
if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); }
}
for quot_rem_quadruples.each |elm| {
for div_rem_quadruples.each |elm| {
let (aVec, bVec, cVec, dVec) = *elm;
let a = BigInt::from_slice(Plus, aVec);
let b = BigInt::from_slice(Plus, bVec);

View File

@ -102,9 +102,9 @@ impl<T: Copy + 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: Copy + Num> Quot<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
impl<T: Copy + Num> Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
#[inline]
fn quot(&self, other: &Cmplx<T>) -> Cmplx<T> {
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
let norm_sqr = other.norm_sqr();
Cmplx::new((self.re*other.re + self.im*other.im) / norm_sqr,
(self.im*other.re - self.re*other.im) / norm_sqr)
@ -275,7 +275,7 @@ mod test {
}
}
#[test]
fn test_quot() {
fn test_div() {
assert_eq!(_neg1_1i / _0_1i, _1_1i);
for all_consts.each |&c| {
if c != Zero::zero() {

View File

@ -143,9 +143,9 @@ impl<T: Copy + Num + Ord>
// (a/b) / (c/d) = (a*d)/(b*c)
impl<T: Copy + Num + Ord>
Quot<Ratio<T>,Ratio<T>> for Ratio<T> {
Div<Ratio<T>,Ratio<T>> for Ratio<T> {
#[inline]
fn quot(&self, rhs: &Ratio<T>) -> Ratio<T> {
fn div(&self, rhs: &Ratio<T>) -> Ratio<T> {
Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer)
}
}
@ -395,7 +395,7 @@ mod test {
}
#[test]
fn test_quot() {
fn test_div() {
assert_eq!(_1 / _1_2, _2);
assert_eq!(_3_2 / _1_2, _1 + _2);
assert_eq!(_1 / _neg1_2, _neg1_2 + _neg1_2 + _neg1_2 + _neg1_2);
@ -424,7 +424,7 @@ mod test {
}
#[test]
#[should_fail]
fn test_quot_0() {
fn test_div_0() {
let _a = _1 / _0;
}
}

View File

@ -389,7 +389,7 @@ pub enum binop {
add,
subtract,
mul,
quot,
div,
rem,
and,
or,

View File

@ -73,7 +73,7 @@ pub fn binop_to_str(op: binop) -> ~str {
add => return ~"+",
subtract => return ~"-",
mul => return ~"*",
quot => return ~"/",
div => return ~"/",
rem => return ~"%",
and => return ~"&&",
or => return ~"||",
@ -96,7 +96,7 @@ pub fn binop_to_method_name(op: binop) -> Option<~str> {
add => return Some(~"add"),
subtract => return Some(~"sub"),
mul => return Some(~"mul"),
quot => return Some(~"quot"),
div => return Some(~"div"),
rem => return Some(~"rem"),
bitxor => return Some(~"bitxor"),
bitand => return Some(~"bitand"),
@ -341,7 +341,7 @@ pub fn is_self(d: ast::def) -> bool {
/// Maps a binary operator to its precedence
pub fn operator_prec(op: ast::binop) -> uint {
match op {
mul | quot | rem => 12u,
mul | div | rem => 12u,
// 'as' sits between here with 11
add | subtract => 10u,
shl | shr => 9u,

View File

@ -19,7 +19,7 @@ use ast::{_mod, add, arg, arm, attribute, bind_by_ref, bind_infer};
use ast::{bind_by_copy, bitand, bitor, bitxor, blk};
use ast::{blk_check_mode, box};
use ast::{crate, crate_cfg, decl, decl_item};
use ast::{decl_local, default_blk, deref, quot, enum_def};
use ast::{decl_local, default_blk, deref, div, enum_def};
use ast::{expr, expr_, expr_addr_of, expr_match, expr_again};
use ast::{expr_assign, expr_assign_op, expr_binary, expr_block};
use ast::{expr_break, expr_call, expr_cast, expr_copy, expr_do_body};
@ -1836,7 +1836,7 @@ pub impl Parser {
token::PLUS => aop = add,
token::MINUS => aop = subtract,
token::STAR => aop = mul,
token::SLASH => aop = quot,
token::SLASH => aop = div,
token::PERCENT => aop = rem,
token::CARET => aop = bitxor,
token::AND => aop = bitand,

View File

@ -371,7 +371,7 @@ impl<'self> to_bytes::IterBytes for StringRef<'self> {
pub fn token_to_binop(tok: Token) -> Option<ast::binop> {
match tok {
BINOP(STAR) => Some(ast::mul),
BINOP(SLASH) => Some(ast::quot),
BINOP(SLASH) => Some(ast::div),
BINOP(PERCENT) => Some(ast::rem),
BINOP(PLUS) => Some(ast::add),
BINOP(MINUS) => Some(ast::subtract),

View File

@ -1,5 +1,5 @@
enum test {
quot_zero = 1/0, //~ERROR expected constant: attempted quotient with a divisor of zero
div_zero = 1/0, //~ERROR expected constant: attempted to divide by zero
rem_zero = 1%0 //~ERROR expected constant: attempted remainder with a divisor of zero
}

View File

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// error-pattern:attempted quotient with a divisor of zero
// error-pattern:attempted to divide by zero
fn main() {
let y = 0;
let z = 1 / y;