: 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`
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.
#[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};
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; }
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 {
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; }
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 }
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};
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)]
#[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.
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
///
/// ~~~
///
#[inline(always)]
- fn quot(&self, other: &T) -> T { *self / *other }
+ fn div(&self, other: &T) -> T { *self / *other }
}
#[cfg(stage0,notest)]
/// # 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 {
}
}
- /// 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)
}
}
#[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));
//! 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;
+ Add<Self,Self>
+ Sub<Self,Self>
+ Mul<Self,Self>
- + Quot<Self,Self>
+ + Div<Self,Self>
+ Rem<Self,Self> {}
pub trait IntConvertible {
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;
pub trait Fractional: Num
+ Orderable
+ Round
- + Quot<Self,Self> {
+ + Div<Self,Self> {
fn recip(&self) -> Self;
}
+ 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;
/// - 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();
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);
}
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;
}
#[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();
* - 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 {
*/
#[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,
* - 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,
* `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,
#[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 }
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 {
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 }
+ fn div_rem(&self, other: &T) -> (T,T) {
+ (*self / *other, *self % *other)
+ }
- /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
+ /// Unsigned integer division. Returns the same result as `div` (`/`).
#[inline(always)]
- fn modulo(&self, other: &T) -> T { *self / *other }
+ fn div_floor(&self, other: &T) -> T { *self / *other }
- /// Calculates `div` and `modulo` simultaneously
+ /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
#[inline(always)]
- fn div_mod(&self, other: &T) -> (T,T) {
- (*self / *other, *self % *other)
- }
+ fn mod_floor(&self, other: &T) -> T { *self / *other }
- /// Calculates `quot` (`\`) and `rem` (`%`) simultaneously
+ /// Calculates `div_floor` and `modulo_floor` simultaneously
#[inline(always)]
- fn quot_rem(&self, other: &T) -> (T,T) {
+ fn div_mod_floor(&self, other: &T) -> (T,T) {
(*self / *other, *self % *other)
}
}
#[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)]
#[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};
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),
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)),
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)),
AddTraitLangItem, // 5
SubTraitLangItem, // 6
MulTraitLangItem, // 7
- QuotTraitLangItem, // 8
+ DivTraitLangItem, // 8
RemTraitLangItem, // 9
NegTraitLangItem, // 10
NotTraitLangItem, // 11
5 => "add",
6 => "sub",
7 => "mul",
- 8 => "quot",
+ 8 => "div",
9 => "rem",
10 => "neg",
11 => "not",
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()
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);
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};
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,
}
}
-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"
};
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) }
if is_float { FMul(bcx, lhs, rhs) }
else { Mul(bcx, lhs, rhs) }
}
- ast::quot => {
+ ast::div => {
if is_float {
FDiv(bcx, lhs, rhs)
} else {
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,
}
}
-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;
}
}
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;
}
}
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()); }
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;
}
}
- #[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`
*
let mut result = ~[];
let mut m = n;
while m > divider {
- let (d, m0) = m.div_mod(÷r);
+ let (d, m0) = m.div_mod_floor(÷r);
result += [m0.to_uint() as BigDigit];
m = d;
}
}
}
-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;
}
}
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;
}
}
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) {
}
}
- #[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`
*
#[cfg(test)]
mod biguint_tests {
-
- use core::*;
use core::num::{IntConvertible, Zero, One, FromStrRadix};
use core::cmp::{Less, Equal, Greater};
use super::{BigUint, BigDigit};
(&[ 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])]
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);
}
#[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);
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)); }
}
}
#[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};
(&[ 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]),
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);
}
#[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);
}
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);
#[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);
}
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);
// (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)
}
}
#[test]
- fn test_quot() {
+ fn test_div() {
assert_eq!(_neg1_1i / _0_1i, _1_1i);
for all_consts.each |&c| {
if c != Zero::zero() {
// (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)
}
}
}
#[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);
}
#[test]
#[should_fail]
- fn test_quot_0() {
+ fn test_div_0() {
let _a = _1 / _0;
}
}
add,
subtract,
mul,
- quot,
+ div,
rem,
and,
or,
add => return ~"+",
subtract => return ~"-",
mul => return ~"*",
- quot => return ~"/",
+ div => return ~"/",
rem => return ~"%",
and => return ~"&&",
or => return ~"||",
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"),
/// 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,
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};
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,
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),
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
}
// 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;