auto merge of #5990 : bjz/rust/rem-quot, r=catamorphism
This renaming, proposed in the [Numeric Bikeshed](https://github.com/mozilla/rust/wiki/Bikeshed-Numeric-Traits#rename-modulo-into-rem-or-remainder-in-traits-and-docs), will allow us to implement div and and modulo methods that follow the conventional mathematical definitions for negative numbers without altering the definitions of the operators (and confusing systems programmers). Here is a useful answer on StackOverflow that explains the difference between `div`/`mod` and `quot`/`rem` in Haskell: (When is the difference between quotRem and divMod useful?)[http://stackoverflow.com/a/339823/679485]. This is part of the numeric trait reforms tracked in issue #4819.
This commit is contained in:
commit
6a31525c50
16
doc/rust.md
16
doc/rust.md
@ -1467,10 +1467,10 @@ A complete list of the built-in language items follows:
|
||||
: Elements can be subtracted.
|
||||
`mul`
|
||||
: Elements can be multiplied.
|
||||
`div`
|
||||
: Elements can be divided.
|
||||
`mod`
|
||||
: Elements have a modulo operation.
|
||||
`quot`
|
||||
: Elements have a quotient operation.
|
||||
`rem`
|
||||
: Elements have a remainder operation.
|
||||
`neg`
|
||||
: Elements can be negated arithmetically.
|
||||
`not`
|
||||
@ -1856,11 +1856,11 @@ The default meaning of the operators on standard types is given here.
|
||||
: Multiplication.
|
||||
Calls the `mul` method on the `core::ops::Mul` trait.
|
||||
`/`
|
||||
: Division.
|
||||
Calls the `div` method on the `core::ops::Div` trait.
|
||||
: Quotient.
|
||||
Calls the `quot` method on the `core::ops::Quot` trait.
|
||||
`%`
|
||||
: Modulo (a.k.a. "remainder").
|
||||
Calls the `modulo` method on the `core::ops::Modulo` trait.
|
||||
: Remainder.
|
||||
Calls the `rem` method on the `core::ops::Rem` trait.
|
||||
|
||||
#### Bitwise operators
|
||||
|
||||
|
@ -362,7 +362,7 @@ The nil type, written `()`, has a single value, also written `()`.
|
||||
## Operators
|
||||
|
||||
Rust's set of operators contains very few surprises. Arithmetic is done with
|
||||
`*`, `/`, `%`, `+`, and `-` (multiply, divide, take remainder, add, and subtract). `-` is
|
||||
`*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and subtract). `-` is
|
||||
also a unary prefix operator that negates numbers. As in C, the bitwise operators
|
||||
`>>`, `<<`, `&`, `|`, and `^` are also supported.
|
||||
|
||||
|
@ -57,8 +57,8 @@
|
||||
<item> Add </item>
|
||||
<item> Sub </item>
|
||||
<item> Mul </item>
|
||||
<item> Div </item>
|
||||
<item> Modulo </item>
|
||||
<item> Quot </item>
|
||||
<item> Rem </item>
|
||||
<item> Neg </item>
|
||||
<item> BitAnd </item>
|
||||
<item> BitOr </item>
|
||||
|
@ -46,7 +46,7 @@ syn keyword rustType off_t dev_t ino_t pid_t mode_t ssize_t
|
||||
|
||||
syn keyword rustTrait Const Copy Send Owned " inherent traits
|
||||
syn keyword rustTrait Eq Ord Num Ptr
|
||||
syn keyword rustTrait Drop Add Sub Mul Div Modulo Neg BitAnd BitOr
|
||||
syn keyword rustTrait Drop Add Sub Mul Quot Rem Neg BitAnd BitOr
|
||||
syn keyword rustTrait BitXor Shl Shr Index
|
||||
|
||||
syn keyword rustSelf self
|
||||
|
@ -75,7 +75,12 @@ they contained the following prologue:
|
||||
|
||||
pub use kinds::{Const, Copy, Owned, Durable};
|
||||
pub use ops::{Drop};
|
||||
#[cfg(stage0)]
|
||||
pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
pub use ops::{Add, Sub, Mul, Quot, Rem, Neg, Not};
|
||||
pub use ops::{BitAnd, BitOr, BitXor};
|
||||
pub use ops::{Shl, Shr, Index};
|
||||
|
||||
|
@ -16,8 +16,13 @@ use option::Option;
|
||||
use from_str;
|
||||
use to_str;
|
||||
|
||||
#[cfg(notest)] use cmp;
|
||||
#[cfg(notest)] use ops;
|
||||
#[cfg(notest)] use cmp::{Eq, Ord};
|
||||
#[cfg(stage0,notest)]
|
||||
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
|
||||
|
||||
pub use cmath::c_float_targ_consts::*;
|
||||
|
||||
@ -131,7 +136,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 div(x: f32, y: f32) -> f32 { return x / y; }
|
||||
pub fn quot(x: f32, y: f32) -> f32 { return x / y; }
|
||||
|
||||
#[inline(always)]
|
||||
pub fn rem(x: f32, y: f32) -> f32 { return x % y; }
|
||||
@ -265,7 +270,7 @@ pub fn logarithm(n: f32, b: f32) -> f32 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl cmp::Eq for f32 {
|
||||
impl Eq for f32 {
|
||||
#[inline(always)]
|
||||
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
|
||||
#[inline(always)]
|
||||
@ -273,7 +278,7 @@ impl cmp::Eq for f32 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl cmp::Ord for f32 {
|
||||
impl Ord for f32 {
|
||||
#[inline(always)]
|
||||
fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
|
||||
#[inline(always)]
|
||||
@ -295,33 +300,41 @@ impl num::One for f32 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::Add<f32,f32> for f32 {
|
||||
#[inline(always)]
|
||||
impl Add<f32,f32> for f32 {
|
||||
fn add(&self, other: &f32) -> f32 { *self + *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Sub<f32,f32> for f32 {
|
||||
#[inline(always)]
|
||||
impl Sub<f32,f32> for f32 {
|
||||
fn sub(&self, other: &f32) -> f32 { *self - *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Mul<f32,f32> for f32 {
|
||||
#[inline(always)]
|
||||
impl Mul<f32,f32> for f32 {
|
||||
fn mul(&self, other: &f32) -> f32 { *self * *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Div<f32,f32> for f32 {
|
||||
#[inline(always)]
|
||||
#[cfg(stage0,notest)]
|
||||
impl Div<f32,f32> for f32 {
|
||||
fn div(&self, other: &f32) -> f32 { *self / *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Modulo<f32,f32> for f32 {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,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 {
|
||||
fn modulo(&self, other: &f32) -> f32 { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Neg<f32> for f32 {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
impl Rem<f32,f32> for f32 {
|
||||
#[inline(always)]
|
||||
fn rem(&self, other: &f32) -> f32 { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl Neg<f32> for f32 {
|
||||
fn neg(&self) -> f32 { -*self }
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,13 @@ use option::Option;
|
||||
use to_str;
|
||||
use from_str;
|
||||
|
||||
#[cfg(notest)] use cmp;
|
||||
#[cfg(notest)] use ops;
|
||||
#[cfg(notest)] use cmp::{Eq, Ord};
|
||||
#[cfg(stage0,notest)]
|
||||
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
|
||||
|
||||
pub use cmath::c_double_targ_consts::*;
|
||||
pub use cmp::{min, max};
|
||||
@ -155,7 +160,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 div(x: f64, y: f64) -> f64 { return x / y; }
|
||||
pub fn quot(x: f64, y: f64) -> f64 { return x / y; }
|
||||
|
||||
#[inline(always)]
|
||||
pub fn rem(x: f64, y: f64) -> f64 { return x % y; }
|
||||
@ -284,7 +289,7 @@ pub fn logarithm(n: f64, b: f64) -> f64 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl cmp::Eq for f64 {
|
||||
impl Eq for f64 {
|
||||
#[inline(always)]
|
||||
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
|
||||
#[inline(always)]
|
||||
@ -292,7 +297,7 @@ impl cmp::Eq for f64 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl cmp::Ord for f64 {
|
||||
impl Ord for f64 {
|
||||
#[inline(always)]
|
||||
fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
|
||||
#[inline(always)]
|
||||
@ -314,33 +319,41 @@ impl num::One for f64 {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::Add<f64,f64> for f64 {
|
||||
#[inline(always)]
|
||||
impl Add<f64,f64> for f64 {
|
||||
fn add(&self, other: &f64) -> f64 { *self + *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Sub<f64,f64> for f64 {
|
||||
#[inline(always)]
|
||||
impl Sub<f64,f64> for f64 {
|
||||
fn sub(&self, other: &f64) -> f64 { *self - *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Mul<f64,f64> for f64 {
|
||||
#[inline(always)]
|
||||
impl Mul<f64,f64> for f64 {
|
||||
fn mul(&self, other: &f64) -> f64 { *self * *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Div<f64,f64> for f64 {
|
||||
#[inline(always)]
|
||||
#[cfg(stage0,notest)]
|
||||
impl Div<f64,f64> for f64 {
|
||||
fn div(&self, other: &f64) -> f64 { *self / *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Modulo<f64,f64> for f64 {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,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 }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Neg<f64> for f64 {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
impl Rem<f64,f64> for f64 {
|
||||
#[inline(always)]
|
||||
fn rem(&self, other: &f64) -> f64 { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl Neg<f64> for f64 {
|
||||
fn neg(&self) -> f64 { -*self }
|
||||
}
|
||||
|
||||
|
@ -28,9 +28,14 @@ use to_str;
|
||||
use from_str;
|
||||
|
||||
#[cfg(notest)] use cmp::{Eq, Ord};
|
||||
#[cfg(notest)] use ops;
|
||||
#[cfg(stage0,notest)]
|
||||
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
|
||||
|
||||
pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt};
|
||||
pub use f64::{add, sub, mul, quot, 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};
|
||||
@ -449,33 +454,41 @@ impl num::Round for float {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::Add<float,float> for float {
|
||||
#[inline(always)]
|
||||
impl Add<float,float> for float {
|
||||
fn add(&self, other: &float) -> float { *self + *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Sub<float,float> for float {
|
||||
#[inline(always)]
|
||||
impl Sub<float,float> for float {
|
||||
fn sub(&self, other: &float) -> float { *self - *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Mul<float,float> for float {
|
||||
#[inline(always)]
|
||||
impl Mul<float,float> for float {
|
||||
fn mul(&self, other: &float) -> float { *self * *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Div<float,float> for float {
|
||||
#[inline(always)]
|
||||
#[cfg(stage0,notest)]
|
||||
impl Div<float,float> for float {
|
||||
fn div(&self, other: &float) -> float { *self / *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Modulo<float,float> for float {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,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 {
|
||||
fn modulo(&self, other: &float) -> float { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Neg<float> for float {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
impl Rem<float,float> for float {
|
||||
#[inline(always)]
|
||||
fn rem(&self, other: &float) -> float { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl Neg<float> for float {
|
||||
fn neg(&self) -> float { -*self }
|
||||
}
|
||||
|
||||
|
@ -17,8 +17,6 @@ use num::strconv;
|
||||
use num;
|
||||
use prelude::*;
|
||||
|
||||
#[cfg(notest)] use cmp::{Eq, Ord};
|
||||
|
||||
pub use cmp::{min, max};
|
||||
|
||||
pub static bits : uint = inst::bits;
|
||||
@ -34,7 +32,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 div(x: T, y: T) -> T { x / y }
|
||||
pub fn quot(x: T, y: T) -> T { x / y }
|
||||
|
||||
/**
|
||||
* Returns the remainder of y / x.
|
||||
@ -176,63 +174,71 @@ impl num::One for T {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::Add<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Add<T,T> for T {
|
||||
fn add(&self, other: &T) -> T { *self + *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Sub<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Sub<T,T> for T {
|
||||
fn sub(&self, other: &T) -> T { *self - *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Mul<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Mul<T,T> for T {
|
||||
fn mul(&self, other: &T) -> T { *self * *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Div<T,T> for T {
|
||||
#[inline(always)]
|
||||
#[cfg(stage0,notest)]
|
||||
impl Div<T,T> for T {
|
||||
fn div(&self, other: &T) -> T { *self / *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Modulo<T,T> for T {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,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 {
|
||||
fn modulo(&self, other: &T) -> T { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Neg<T> for T {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
impl Rem<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn rem(&self, other: &T) -> T { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl Neg<T> for T {
|
||||
fn neg(&self) -> T { -*self }
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::BitOr<T,T> for T {
|
||||
impl BitOr<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitor(&self, other: &T) -> T { *self | *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::BitAnd<T,T> for T {
|
||||
impl BitAnd<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitand(&self, other: &T) -> T { *self & *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::BitXor<T,T> for T {
|
||||
impl BitXor<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitxor(&self, other: &T) -> T { *self ^ *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Shl<T,T> for T {
|
||||
impl Shl<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn shl(&self, other: &T) -> T { *self << *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Shr<T,T> for T {
|
||||
impl Shr<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn shr(&self, other: &T) -> T { *self >> *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Not<T> for T {
|
||||
impl Not<T> for T {
|
||||
#[inline(always)]
|
||||
fn not(&self) -> T { !*self }
|
||||
}
|
||||
|
@ -10,7 +10,16 @@
|
||||
|
||||
//! An interface for numeric types
|
||||
use cmp::{Eq, Ord};
|
||||
use ops::{Neg, Add, Sub, Mul, Div, Modulo};
|
||||
#[cfg(stage0)]
|
||||
use ops::{Add, Sub, Mul, Neg};
|
||||
#[cfg(stage0)]
|
||||
use Quot = ops::Div;
|
||||
#[cfg(stage0)]
|
||||
use Rem = ops::Modulo;
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
|
||||
use option::Option;
|
||||
use kinds::Copy;
|
||||
|
||||
@ -21,8 +30,8 @@ pub trait Num: Eq + Zero + One
|
||||
+ Add<Self,Self>
|
||||
+ Sub<Self,Self>
|
||||
+ Mul<Self,Self>
|
||||
+ Div<Self,Self>
|
||||
+ Modulo<Self,Self> {}
|
||||
+ Quot<Self,Self>
|
||||
+ Rem<Self,Self> {}
|
||||
|
||||
impl Num for u8 {}
|
||||
impl Num for u16 {}
|
||||
@ -174,7 +183,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+Div<T,T>+Mul<T,T>>(
|
||||
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Quot<T,T>+Mul<T,T>>(
|
||||
radix: uint, pow: uint) -> T {
|
||||
let _0: T = Zero::zero();
|
||||
let _1: T = One::one();
|
||||
@ -194,7 +203,7 @@ pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
|
||||
total
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(stage0,test)]
|
||||
fn test_num<T:Num + NumCast>(ten: T, two: T) {
|
||||
assert_eq!(ten.add(&two), cast(12));
|
||||
assert_eq!(ten.sub(&two), cast(8));
|
||||
@ -208,6 +217,22 @@ fn test_num<T:Num + NumCast>(ten: T, two: T) {
|
||||
assert_eq!(ten.div(&two), ten / two);
|
||||
assert_eq!(ten.modulo(&two), ten % two);
|
||||
}
|
||||
#[cfg(stage1,test)]
|
||||
#[cfg(stage2,test)]
|
||||
#[cfg(stage3,test)]
|
||||
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.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.rem(&two), ten % two);
|
||||
}
|
||||
|
||||
#[test] fn test_u8_num() { test_num(10u8, 2u8) }
|
||||
#[test] fn test_u16_num() { test_num(10u16, 2u16) }
|
||||
|
@ -9,7 +9,16 @@
|
||||
// except according to those terms.
|
||||
|
||||
use core::cmp::{Ord, Eq};
|
||||
use ops::{Add, Div, Modulo, Mul, Neg, Sub};
|
||||
#[cfg(stage0)]
|
||||
use ops::{Add, Sub, Mul, Neg};
|
||||
#[cfg(stage0)]
|
||||
use Quot = ops::Div;
|
||||
#[cfg(stage0)]
|
||||
use Rem = ops::Modulo;
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
|
||||
use option::{None, Option, Some};
|
||||
use char;
|
||||
use str;
|
||||
@ -58,7 +67,7 @@ fn is_neg_inf<T:Eq+NumStrConv>(num: &T) -> bool {
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn is_neg_zero<T:Eq+One+Zero+NumStrConv+Div<T,T>>(num: &T) -> bool {
|
||||
fn is_neg_zero<T:Eq+One+Zero+NumStrConv+Quot<T,T>>(num: &T) -> bool {
|
||||
let _0: T = Zero::zero();
|
||||
let _1: T = One::one();
|
||||
|
||||
@ -171,7 +180,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+
|
||||
Div<T,T>+Neg<T>+Modulo<T,T>+Mul<T,T>>(
|
||||
Quot<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 {
|
||||
@ -379,7 +388,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+
|
||||
Div<T,T>+Neg<T>+Modulo<T,T>+Mul<T,T>>(
|
||||
Quot<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,
|
||||
@ -432,7 +441,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+Div<T,T>+
|
||||
pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Quot<T,T>+
|
||||
Mul<T,T>+Sub<T,T>+Neg<T>+Add<T,T>+
|
||||
NumStrConv>(
|
||||
buf: &[u8], radix: uint, negative: bool, fractional: bool,
|
||||
@ -629,7 +638,7 @@ pub fn from_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+
|
||||
* `from_str_bytes_common()`, for details see there.
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Div<T,T>+Mul<T,T>+
|
||||
pub fn from_str_common<T:NumCast+Zero+One+Eq+Ord+Copy+Quot<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,
|
||||
|
@ -19,8 +19,6 @@ use num;
|
||||
use option::Option;
|
||||
use prelude::*;
|
||||
|
||||
#[cfg(notest)] use cmp::{Eq, Ord};
|
||||
|
||||
pub use cmp::{min, max};
|
||||
|
||||
pub static bits : uint = inst::bits;
|
||||
@ -36,7 +34,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 div(x: T, y: T) -> T { x / y }
|
||||
pub fn quot(x: T, y: T) -> T { x / y }
|
||||
#[inline(always)]
|
||||
pub fn rem(x: T, y: T) -> T { x % y }
|
||||
|
||||
@ -141,63 +139,71 @@ impl num::One for T {
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::Add<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Add<T,T> for T {
|
||||
fn add(&self, other: &T) -> T { *self + *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Sub<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Sub<T,T> for T {
|
||||
fn sub(&self, other: &T) -> T { *self - *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Mul<T,T> for T {
|
||||
#[inline(always)]
|
||||
impl Mul<T,T> for T {
|
||||
fn mul(&self, other: &T) -> T { *self * *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Div<T,T> for T {
|
||||
#[inline(always)]
|
||||
#[cfg(stage0,notest)]
|
||||
impl Div<T,T> for T {
|
||||
fn div(&self, other: &T) -> T { *self / *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Modulo<T,T> for T {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,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 {
|
||||
fn modulo(&self, other: &T) -> T { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Neg<T> for T {
|
||||
#[cfg(stage1,notest)]
|
||||
#[cfg(stage2,notest)]
|
||||
#[cfg(stage3,notest)]
|
||||
impl Rem<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn rem(&self, other: &T) -> T { *self % *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl Neg<T> for T {
|
||||
fn neg(&self) -> T { -*self }
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl ops::BitOr<T,T> for T {
|
||||
impl BitOr<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitor(&self, other: &T) -> T { *self | *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::BitAnd<T,T> for T {
|
||||
impl BitAnd<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitand(&self, other: &T) -> T { *self & *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::BitXor<T,T> for T {
|
||||
impl BitXor<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn bitxor(&self, other: &T) -> T { *self ^ *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Shl<T,T> for T {
|
||||
impl Shl<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn shl(&self, other: &T) -> T { *self << *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Shr<T,T> for T {
|
||||
impl Shr<T,T> for T {
|
||||
#[inline(always)]
|
||||
fn shr(&self, other: &T) -> T { *self >> *other }
|
||||
}
|
||||
#[cfg(notest)]
|
||||
impl ops::Not<T> for T {
|
||||
impl Not<T> for T {
|
||||
#[inline(always)]
|
||||
fn not(&self) -> T { !*self }
|
||||
}
|
||||
|
@ -31,14 +31,30 @@ pub trait Mul<RHS,Result> {
|
||||
}
|
||||
|
||||
#[lang="div"]
|
||||
#[cfg(stage0)]
|
||||
pub trait Div<RHS,Result> {
|
||||
fn div(&self, rhs: &RHS) -> Result;
|
||||
}
|
||||
#[lang="quot"]
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
pub trait Quot<RHS,Result> {
|
||||
fn quot(&self, rhs: &RHS) -> Result;
|
||||
}
|
||||
|
||||
#[lang="modulo"]
|
||||
#[cfg(stage0)]
|
||||
pub trait Modulo<RHS,Result> {
|
||||
fn modulo(&self, rhs: &RHS) -> Result;
|
||||
}
|
||||
#[lang="rem"]
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
pub trait Rem<RHS,Result> {
|
||||
fn rem(&self, rhs: &RHS) -> Result;
|
||||
}
|
||||
|
||||
#[lang="neg"]
|
||||
pub trait Neg<Result> {
|
||||
|
@ -14,7 +14,12 @@
|
||||
|
||||
pub use either::{Either, Left, Right};
|
||||
pub use kinds::{Const, Copy, Owned, Durable};
|
||||
#[cfg(stage0)]
|
||||
pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
pub use ops::{Add, Sub, Mul, Quot, Rem, Neg, Not};
|
||||
pub use ops::{BitAnd, BitOr, BitXor};
|
||||
pub use ops::{Drop};
|
||||
pub use ops::{Shl, Shr, Index};
|
||||
|
@ -283,7 +283,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)),
|
||||
div => Ok(const_float(a / b)),
|
||||
quot => Ok(const_float(a / b)),
|
||||
rem => Ok(const_float(a % b)),
|
||||
eq => fromb(a == b),
|
||||
lt => fromb(a < b),
|
||||
@ -299,9 +299,9 @@ 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)),
|
||||
div if b == 0 => Err(~"divide by zero"),
|
||||
div => Ok(const_int(a / b)),
|
||||
rem if b == 0 => Err(~"modulo zero"),
|
||||
quot if b == 0 => Err(~"quotient zero"),
|
||||
quot => Ok(const_int(a / b)),
|
||||
rem if b == 0 => Err(~"remainder zero"),
|
||||
rem => Ok(const_int(a % b)),
|
||||
and | bitand => Ok(const_int(a & b)),
|
||||
or | bitor => Ok(const_int(a | b)),
|
||||
@ -321,9 +321,9 @@ 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)),
|
||||
div if b == 0 => Err(~"divide by zero"),
|
||||
div => Ok(const_uint(a / b)),
|
||||
rem if b == 0 => Err(~"modulo zero"),
|
||||
quot if b == 0 => Err(~"quotient zero"),
|
||||
quot => Ok(const_uint(a / b)),
|
||||
rem if b == 0 => Err(~"remainder zero"),
|
||||
rem => Ok(const_uint(a % b)),
|
||||
and | bitand => Ok(const_uint(a & b)),
|
||||
or | bitor => Ok(const_uint(a | b)),
|
||||
|
@ -480,7 +480,7 @@ pub fn check_durable(tcx: ty::ctxt, ty: ty::t, sp: span) -> bool {
|
||||
|
||||
/// This is rather subtle. When we are casting a value to a instantiated
|
||||
/// trait like `a as trait<'r>`, regionck already ensures that any borrowed
|
||||
/// pointers that appear in the type of `a` are bounded by `'r` (ed.: modulo
|
||||
/// pointers that appear in the type of `a` are bounded by `'r` (ed.: rem
|
||||
/// FIXME(#5723)). However, it is possible that there are *type parameters*
|
||||
/// in the type of `a`, and those *type parameters* may have borrowed pointers
|
||||
/// within them. We have to guarantee that the regions which appear in those
|
||||
|
@ -45,8 +45,8 @@ pub enum LangItem {
|
||||
AddTraitLangItem, // 5
|
||||
SubTraitLangItem, // 6
|
||||
MulTraitLangItem, // 7
|
||||
DivTraitLangItem, // 8
|
||||
ModuloTraitLangItem, // 9
|
||||
QuotTraitLangItem, // 8
|
||||
RemTraitLangItem, // 9
|
||||
NegTraitLangItem, // 10
|
||||
NotTraitLangItem, // 11
|
||||
BitXorTraitLangItem, // 12
|
||||
@ -108,8 +108,8 @@ pub impl LanguageItems {
|
||||
5 => "add",
|
||||
6 => "sub",
|
||||
7 => "mul",
|
||||
8 => "div",
|
||||
9 => "modulo",
|
||||
8 => "quot",
|
||||
9 => "rem",
|
||||
10 => "neg",
|
||||
11 => "not",
|
||||
12 => "bitxor",
|
||||
@ -170,11 +170,11 @@ pub impl LanguageItems {
|
||||
pub fn mul_trait(&const self) -> def_id {
|
||||
self.items[MulTraitLangItem as uint].get()
|
||||
}
|
||||
pub fn div_trait(&const self) -> def_id {
|
||||
self.items[DivTraitLangItem as uint].get()
|
||||
pub fn quot_trait(&const self) -> def_id {
|
||||
self.items[QuotTraitLangItem as uint].get()
|
||||
}
|
||||
pub fn modulo_trait(&const self) -> def_id {
|
||||
self.items[ModuloTraitLangItem as uint].get()
|
||||
pub fn rem_trait(&const self) -> def_id {
|
||||
self.items[RemTraitLangItem as uint].get()
|
||||
}
|
||||
pub fn neg_trait(&const self) -> def_id {
|
||||
self.items[NegTraitLangItem as uint].get()
|
||||
@ -271,8 +271,8 @@ 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(@~"div", DivTraitLangItem as uint);
|
||||
item_refs.insert(@~"modulo", ModuloTraitLangItem as uint);
|
||||
item_refs.insert(@~"quot", QuotTraitLangItem as uint);
|
||||
item_refs.insert(@~"rem", RemTraitLangItem as uint);
|
||||
item_refs.insert(@~"neg", NegTraitLangItem as uint);
|
||||
item_refs.insert(@~"not", NotTraitLangItem as uint);
|
||||
item_refs.insert(@~"bitxor", BitXorTraitLangItem as uint);
|
||||
|
@ -37,7 +37,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, div, eq};
|
||||
use syntax::ast::{def_upvar, def_use, def_variant, quot, 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};
|
||||
@ -4899,13 +4899,13 @@ pub impl Resolver {
|
||||
self.add_fixed_trait_for_expr(expr.id,
|
||||
self.lang_items.mul_trait());
|
||||
}
|
||||
expr_binary(div, _, _) | expr_assign_op(div, _, _) => {
|
||||
expr_binary(quot, _, _) | expr_assign_op(quot, _, _) => {
|
||||
self.add_fixed_trait_for_expr(expr.id,
|
||||
self.lang_items.div_trait());
|
||||
self.lang_items.quot_trait());
|
||||
}
|
||||
expr_binary(rem, _, _) | expr_assign_op(rem, _, _) => {
|
||||
self.add_fixed_trait_for_expr(expr.id,
|
||||
self.lang_items.modulo_trait());
|
||||
self.lang_items.rem_trait());
|
||||
}
|
||||
expr_binary(bitxor, _, _) | expr_assign_op(bitxor, _, _) => {
|
||||
self.add_fixed_trait_for_expr(expr.id,
|
||||
|
@ -782,12 +782,12 @@ pub fn cast_shift_rhs(op: ast::binop,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fail_if_zero(cx: block, span: span, divmod: ast::binop,
|
||||
pub fn fail_if_zero(cx: block, span: span, quotrem: ast::binop,
|
||||
rhs: ValueRef, rhs_t: ty::t) -> block {
|
||||
let text = if divmod == ast::div {
|
||||
@~"divide by zero"
|
||||
let text = if quotrem == ast::quot {
|
||||
@~"quotient zero"
|
||||
} else {
|
||||
@~"modulo zero"
|
||||
@~"remainder zero"
|
||||
};
|
||||
let is_zero = match ty::get(rhs_t).sty {
|
||||
ty::ty_int(t) => {
|
||||
|
@ -272,7 +272,7 @@ fn const_expr_unadjusted(cx: @CrateContext, e: @ast::expr) -> ValueRef {
|
||||
if is_float { llvm::LLVMConstFMul(te1, te2) }
|
||||
else { llvm::LLVMConstMul(te1, te2) }
|
||||
}
|
||||
ast::div => {
|
||||
ast::quot => {
|
||||
if is_float { llvm::LLVMConstFDiv(te1, te2) }
|
||||
else if signed { llvm::LLVMConstSDiv(te1, te2) }
|
||||
else { llvm::LLVMConstUDiv(te1, te2) }
|
||||
|
@ -1437,7 +1437,7 @@ fn trans_eager_binop(bcx: block,
|
||||
if is_float { FMul(bcx, lhs, rhs) }
|
||||
else { Mul(bcx, lhs, rhs) }
|
||||
}
|
||||
ast::div => {
|
||||
ast::quot => {
|
||||
if is_float {
|
||||
FDiv(bcx, lhs, rhs)
|
||||
} else {
|
||||
|
@ -4255,7 +4255,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::div => opcat_mult,
|
||||
ast::quot => opcat_mult,
|
||||
ast::rem => opcat_mult,
|
||||
ast::and => opcat_logic,
|
||||
ast::or => opcat_logic,
|
||||
|
@ -118,7 +118,7 @@ pub trait FromBase64 {
|
||||
impl FromBase64 for ~[u8] {
|
||||
/**
|
||||
* Convert base64 `u8` vector into u8 byte values.
|
||||
* Every 4 encoded characters is converted into 3 octets, modulo padding.
|
||||
* Every 4 encoded characters is converted into 3 octets, rem padding.
|
||||
*
|
||||
* *Example*:
|
||||
*
|
||||
|
@ -262,16 +262,16 @@ impl Mul<BigUint, BigUint> for BigUint {
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<BigUint, BigUint> for BigUint {
|
||||
fn div(&self, other: &BigUint) -> BigUint {
|
||||
let (d, _) = self.divmod(other);
|
||||
impl Quot<BigUint, BigUint> for BigUint {
|
||||
fn quot(&self, other: &BigUint) -> BigUint {
|
||||
let (d, _) = self.quot_rem(other);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
impl Modulo<BigUint, BigUint> for BigUint {
|
||||
fn modulo(&self, other: &BigUint) -> BigUint {
|
||||
let (_, m) = self.divmod(other);
|
||||
impl Rem<BigUint, BigUint> for BigUint {
|
||||
fn rem(&self, other: &BigUint) -> BigUint {
|
||||
let (_, m) = self.quot_rem(other);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@ -304,7 +304,7 @@ impl ToStrRadix for BigUint {
|
||||
let mut result = ~[];
|
||||
let mut r = n;
|
||||
while r > divider {
|
||||
let (d, r0) = r.divmod(÷r);
|
||||
let (d, r0) = r.quot_rem(÷r);
|
||||
result += [r0.to_uint() as BigDigit];
|
||||
r = d;
|
||||
}
|
||||
@ -384,7 +384,7 @@ pub impl BigUint {
|
||||
|
||||
fn abs(&self) -> BigUint { copy *self }
|
||||
|
||||
fn divmod(&self, other: &BigUint) -> (BigUint, BigUint) {
|
||||
fn quot_rem(&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()); }
|
||||
@ -402,10 +402,10 @@ pub impl BigUint {
|
||||
shift += 1;
|
||||
}
|
||||
assert!(shift < BigDigit::bits);
|
||||
let (d, m) = divmod_inner(self << shift, other << shift);
|
||||
let (d, m) = quot_rem_inner(self << shift, other << shift);
|
||||
return (d, m >> shift);
|
||||
|
||||
fn divmod_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
|
||||
fn quot_rem_inner(a: BigUint, b: BigUint) -> (BigUint, BigUint) {
|
||||
let mut r = a;
|
||||
let mut d = Zero::zero::<BigUint>();
|
||||
let mut n = 1;
|
||||
@ -464,7 +464,7 @@ pub impl BigUint {
|
||||
return r;
|
||||
}
|
||||
fn quotrem(&self, other: &BigUint) -> (BigUint, BigUint) {
|
||||
self.divmod(other)
|
||||
self.quot_rem(other)
|
||||
}
|
||||
|
||||
fn is_zero(&self) -> bool { self.data.is_empty() }
|
||||
@ -737,16 +737,16 @@ impl Mul<BigInt, BigInt> for BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<BigInt, BigInt> for BigInt {
|
||||
fn div(&self, other: &BigInt) -> BigInt {
|
||||
let (d, _) = self.divmod(other);
|
||||
impl Quot<BigInt, BigInt> for BigInt {
|
||||
fn quot(&self, other: &BigInt) -> BigInt {
|
||||
let (d, _) = self.quot_rem(other);
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
impl Modulo<BigInt, BigInt> for BigInt {
|
||||
fn modulo(&self, other: &BigInt) -> BigInt {
|
||||
let (_, m) = self.divmod(other);
|
||||
impl Rem<BigInt, BigInt> for BigInt {
|
||||
fn rem(&self, other: &BigInt) -> BigInt {
|
||||
let (_, m) = self.quot_rem(other);
|
||||
return m;
|
||||
}
|
||||
}
|
||||
@ -841,9 +841,9 @@ pub impl BigInt {
|
||||
BigInt::from_biguint(Plus, copy self.data)
|
||||
}
|
||||
|
||||
fn divmod(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
fn quot_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
// m.sign == other.sign
|
||||
let (d_ui, m_ui) = self.data.divmod(&other.data);
|
||||
let (d_ui, m_ui) = self.data.quot_rem(&other.data);
|
||||
let d = BigInt::from_biguint(Plus, d_ui),
|
||||
m = BigInt::from_biguint(Plus, m_ui);
|
||||
match (self.sign, other.sign) {
|
||||
@ -1150,7 +1150,7 @@ mod biguint_tests {
|
||||
(&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1])
|
||||
];
|
||||
|
||||
static divmod_quadruples: &'static [(&'static [BigDigit],
|
||||
static quot_rem_quadruples: &'static [(&'static [BigDigit],
|
||||
&'static [BigDigit],
|
||||
&'static [BigDigit],
|
||||
&'static [BigDigit])]
|
||||
@ -1174,7 +1174,7 @@ mod biguint_tests {
|
||||
assert!(b * a == c);
|
||||
}
|
||||
|
||||
for divmod_quadruples.each |elm| {
|
||||
for quot_rem_quadruples.each |elm| {
|
||||
let (aVec, bVec, cVec, dVec) = *elm;
|
||||
let a = BigUint::from_slice(aVec);
|
||||
let b = BigUint::from_slice(bVec);
|
||||
@ -1187,7 +1187,7 @@ mod biguint_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divmod() {
|
||||
fn test_quot_rem() {
|
||||
for mul_triples.each |elm| {
|
||||
let (aVec, bVec, cVec) = *elm;
|
||||
let a = BigUint::from_slice(aVec);
|
||||
@ -1195,21 +1195,21 @@ mod biguint_tests {
|
||||
let c = BigUint::from_slice(cVec);
|
||||
|
||||
if a.is_not_zero() {
|
||||
assert!(c.divmod(&a) == (b, Zero::zero()));
|
||||
assert!(c.quot_rem(&a) == (b, Zero::zero()));
|
||||
}
|
||||
if b.is_not_zero() {
|
||||
assert!(c.divmod(&b) == (a, Zero::zero()));
|
||||
assert!(c.quot_rem(&b) == (a, Zero::zero()));
|
||||
}
|
||||
}
|
||||
|
||||
for divmod_quadruples.each |elm| {
|
||||
for quot_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_not_zero() { assert!(a.divmod(&b) == (c, d)); }
|
||||
if b.is_not_zero() { assert!(a.quot_rem(&b) == (c, d)); }
|
||||
}
|
||||
}
|
||||
|
||||
@ -1516,7 +1516,7 @@ mod bigint_tests {
|
||||
(&[ 0, 0, 1], &[ 0, 0, 0, 1], &[0, 0, 0, 0, 0, 1])
|
||||
];
|
||||
|
||||
static divmod_quadruples: &'static [(&'static [BigDigit],
|
||||
static quot_rem_quadruples: &'static [(&'static [BigDigit],
|
||||
&'static [BigDigit],
|
||||
&'static [BigDigit],
|
||||
&'static [BigDigit])]
|
||||
@ -1543,7 +1543,7 @@ mod bigint_tests {
|
||||
assert!((-b) * a == -c);
|
||||
}
|
||||
|
||||
for divmod_quadruples.each |elm| {
|
||||
for quot_rem_quadruples.each |elm| {
|
||||
let (aVec, bVec, cVec, dVec) = *elm;
|
||||
let a = BigInt::from_slice(Plus, aVec);
|
||||
let b = BigInt::from_slice(Plus, bVec);
|
||||
@ -1556,9 +1556,9 @@ mod bigint_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_divmod() {
|
||||
fn test_quot_rem() {
|
||||
fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
|
||||
let (d, m) = a.divmod(b);
|
||||
let (d, m) = a.quot_rem(b);
|
||||
if m.is_not_zero() {
|
||||
assert!(m.sign == b.sign);
|
||||
}
|
||||
@ -1592,7 +1592,7 @@ mod bigint_tests {
|
||||
if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); }
|
||||
}
|
||||
|
||||
for divmod_quadruples.each |elm| {
|
||||
for quot_rem_quadruples.each |elm| {
|
||||
let (aVec, bVec, cVec, dVec) = *elm;
|
||||
let a = BigInt::from_slice(Plus, aVec);
|
||||
let b = BigInt::from_slice(Plus, bVec);
|
||||
@ -1635,7 +1635,7 @@ mod bigint_tests {
|
||||
if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); }
|
||||
}
|
||||
|
||||
for divmod_quadruples.each |elm| {
|
||||
for quot_rem_quadruples.each |elm| {
|
||||
let (aVec, bVec, cVec, dVec) = *elm;
|
||||
let a = BigInt::from_slice(Plus, aVec);
|
||||
let b = BigInt::from_slice(Plus, bVec);
|
||||
|
@ -32,8 +32,7 @@ pub type Complex = Cmplx<float>;
|
||||
pub type Complex32 = Cmplx<f32>;
|
||||
pub type Complex64 = Cmplx<f64>;
|
||||
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Cmplx<T> {
|
||||
impl<T: Copy + Num> Cmplx<T> {
|
||||
/// Create a new Cmplx
|
||||
#[inline]
|
||||
pub fn new(re: T, im: T) -> Cmplx<T> {
|
||||
@ -80,24 +79,21 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
|
||||
/* arithmetic */
|
||||
// (a + i b) + (c + i d) == (a + c) + i (b + d)
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Add<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
|
||||
impl<T: Copy + Num> 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: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Sub<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
|
||||
impl<T: Copy + Num> 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: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Mul<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
|
||||
impl<T: Copy + Num> 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,
|
||||
@ -107,18 +103,16 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<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 + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Div<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
|
||||
impl<T: Copy + Num> Quot<Cmplx<T>, Cmplx<T>> for Cmplx<T> {
|
||||
#[inline]
|
||||
fn div(&self, other: &Cmplx<T>) -> Cmplx<T> {
|
||||
fn quot(&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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
Neg<Cmplx<T>> for Cmplx<T> {
|
||||
impl<T: Copy + Num> Neg<Cmplx<T>> for Cmplx<T> {
|
||||
#[inline]
|
||||
fn neg(&self) -> Cmplx<T> {
|
||||
Cmplx::new(-self.re, -self.im)
|
||||
@ -126,16 +120,14 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T>>
|
||||
}
|
||||
|
||||
/* constants */
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero>
|
||||
Zero for Cmplx<T> {
|
||||
impl<T: Copy + Num> Zero for Cmplx<T> {
|
||||
#[inline]
|
||||
fn zero() -> Cmplx<T> {
|
||||
Cmplx::new(Zero::zero(), Zero::zero())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero + One>
|
||||
One for Cmplx<T> {
|
||||
impl<T: Copy + Num> One for Cmplx<T> {
|
||||
#[inline]
|
||||
fn one() -> Cmplx<T> {
|
||||
Cmplx::new(One::one(), Zero::zero())
|
||||
@ -143,7 +135,7 @@ impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Neg<T> + Zero + One>
|
||||
}
|
||||
|
||||
/* string conversions */
|
||||
impl<T: ToStr + Zero + Ord + Neg<T>> ToStr for Cmplx<T> {
|
||||
impl<T: ToStr + Num + 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())
|
||||
@ -153,7 +145,7 @@ impl<T: ToStr + Zero + Ord + Neg<T>> ToStr for Cmplx<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToStrRadix + Zero + Ord + Neg<T>> ToStrRadix for Cmplx<T> {
|
||||
impl<T: ToStrRadix + Num + 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))
|
||||
@ -280,7 +272,7 @@ mod test {
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn test_div() {
|
||||
fn test_quot() {
|
||||
assert_eq!(_neg1_1i / _0_1i, _1_1i);
|
||||
for all_consts.each |&c| {
|
||||
if c != Zero::zero() {
|
||||
|
@ -33,7 +33,7 @@ pub type Rational64 = Ratio<i64>;
|
||||
/// Alias for arbitrary precision rationals.
|
||||
pub type BigRational = Ratio<BigInt>;
|
||||
|
||||
impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
Ratio<T> {
|
||||
/// Create a ratio representing the integer `t`.
|
||||
#[inline(always)]
|
||||
@ -51,7 +51,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
#[inline(always)]
|
||||
pub fn new(numer: T, denom: T) -> Ratio<T> {
|
||||
if denom == Zero::zero() {
|
||||
fail!(~"divide by 0");
|
||||
fail!(~"quotient of 0");
|
||||
}
|
||||
let mut ret = Ratio::new_raw(numer, denom);
|
||||
ret.reduce();
|
||||
@ -85,7 +85,7 @@ Compute the greatest common divisor of two numbers, via Euclid's algorithm.
|
||||
The result can be negative.
|
||||
*/
|
||||
#[inline]
|
||||
pub fn gcd_raw<T: Modulo<T,T> + Zero + Eq>(n: T, m: T) -> T {
|
||||
pub fn gcd_raw<T: Num>(n: T, m: T) -> T {
|
||||
let mut m = m, n = n;
|
||||
while m != Zero::zero() {
|
||||
let temp = m;
|
||||
@ -101,7 +101,7 @@ Compute the greatest common divisor of two numbers, via Euclid's algorithm.
|
||||
The result is always positive.
|
||||
*/
|
||||
#[inline]
|
||||
pub fn gcd<T: Modulo<T,T> + Neg<T> + Zero + Ord + Eq>(n: T, m: T) -> T {
|
||||
pub fn gcd<T: Num + Ord>(n: T, m: T) -> T {
|
||||
let g = gcd_raw(n, m);
|
||||
if g < Zero::zero() { -g }
|
||||
else { g }
|
||||
@ -136,7 +136,7 @@ cmp_impl!(impl TotalOrd, cmp -> cmp::Ordering)
|
||||
|
||||
/* Arithmetic */
|
||||
// a/b * c/d = (a*c)/(b*d)
|
||||
impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
Mul<Ratio<T>,Ratio<T>> for Ratio<T> {
|
||||
#[inline]
|
||||
fn mul(&self, rhs: &Ratio<T>) -> Ratio<T> {
|
||||
@ -145,10 +145,10 @@ impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + E
|
||||
}
|
||||
|
||||
// (a/b) / (c/d) = (a*d)/(b*c)
|
||||
impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
Div<Ratio<T>,Ratio<T>> for Ratio<T> {
|
||||
impl<T: Copy + Num + Ord>
|
||||
Quot<Ratio<T>,Ratio<T>> for Ratio<T> {
|
||||
#[inline]
|
||||
fn div(&self, rhs: &Ratio<T>) -> Ratio<T> {
|
||||
fn quot(&self, rhs: &Ratio<T>) -> Ratio<T> {
|
||||
Ratio::new(self.numer * rhs.denom, self.denom * rhs.numer)
|
||||
}
|
||||
}
|
||||
@ -156,9 +156,7 @@ impl<T: Copy + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + E
|
||||
// Abstracts the a/b `op` c/d = (a*d `op` b*d) / (b*d) pattern
|
||||
macro_rules! arith_impl {
|
||||
(impl $imp:ident, $method:ident) => {
|
||||
impl<T: Copy +
|
||||
Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> +
|
||||
Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
$imp<Ratio<T>,Ratio<T>> for Ratio<T> {
|
||||
#[inline]
|
||||
fn $method(&self, rhs: &Ratio<T>) -> Ratio<T> {
|
||||
@ -176,9 +174,9 @@ arith_impl!(impl Add, add)
|
||||
arith_impl!(impl Sub, sub)
|
||||
|
||||
// a/b % c/d = (a*d % b*c)/(b*d)
|
||||
arith_impl!(impl Modulo, modulo)
|
||||
arith_impl!(impl Rem, rem)
|
||||
|
||||
impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
Neg<Ratio<T>> for Ratio<T> {
|
||||
#[inline]
|
||||
fn neg(&self) -> Ratio<T> {
|
||||
@ -187,7 +185,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
}
|
||||
|
||||
/* Constants */
|
||||
impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
Zero for Ratio<T> {
|
||||
#[inline]
|
||||
fn zero() -> Ratio<T> {
|
||||
@ -195,7 +193,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
One for Ratio<T> {
|
||||
#[inline]
|
||||
fn one() -> Ratio<T> {
|
||||
@ -204,8 +202,7 @@ impl<T: Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
}
|
||||
|
||||
/* Utils */
|
||||
impl<T: Copy + Add<T,T> + Sub<T,T> + Mul<T,T> + Div<T,T> + Modulo<T,T> + Neg<T> +
|
||||
Zero + One + Ord + Eq>
|
||||
impl<T: Copy + Num + Ord>
|
||||
Round for Ratio<T> {
|
||||
fn round(&self, mode: num::RoundMode) -> Ratio<T> {
|
||||
match mode {
|
||||
@ -256,7 +253,7 @@ impl<T: ToStrRadix> ToStrRadix for Ratio<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: FromStr + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: FromStr + Copy + Num + Ord>
|
||||
FromStr for Ratio<T> {
|
||||
/// Parses `numer/denom`.
|
||||
fn from_str(s: &str) -> Option<Ratio<T>> {
|
||||
@ -273,7 +270,7 @@ impl<T: FromStr + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T: FromStrRadix + Copy + Div<T,T> + Modulo<T,T> + Neg<T> + Zero + One + Ord + Eq>
|
||||
impl<T: FromStrRadix + Copy + Num + Ord>
|
||||
FromStrRadix for Ratio<T> {
|
||||
/// Parses `numer/denom` where the numbers are in base `radix`.
|
||||
fn from_str_radix(s: &str, radix: uint) -> Option<Ratio<T>> {
|
||||
@ -386,14 +383,14 @@ mod test {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_div() {
|
||||
fn test_quot() {
|
||||
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]
|
||||
fn test_modulo() {
|
||||
fn test_rem() {
|
||||
assert_eq!(_3_2 % _1, _1_2);
|
||||
assert_eq!(_2 % _neg1_2, _0);
|
||||
assert_eq!(_1_2 % _2, _1_2);
|
||||
@ -415,7 +412,7 @@ mod test {
|
||||
}
|
||||
#[test]
|
||||
#[should_fail]
|
||||
fn test_div_0() {
|
||||
fn test_quot_0() {
|
||||
let _a = _1 / _0;
|
||||
}
|
||||
}
|
||||
|
@ -98,10 +98,19 @@ pub mod cmp;
|
||||
pub mod base64;
|
||||
pub mod rl;
|
||||
pub mod workcache;
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
#[path="num/bigint.rs"]
|
||||
pub mod bigint;
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
#[path="num/rational.rs"]
|
||||
pub mod rational;
|
||||
#[cfg(stage1)]
|
||||
#[cfg(stage2)]
|
||||
#[cfg(stage3)]
|
||||
#[path="num/complex.rs"]
|
||||
pub mod complex;
|
||||
pub mod stats;
|
||||
|
@ -389,7 +389,7 @@ pub enum binop {
|
||||
add,
|
||||
subtract,
|
||||
mul,
|
||||
div,
|
||||
quot,
|
||||
rem,
|
||||
and,
|
||||
or,
|
||||
|
@ -80,7 +80,7 @@ pub fn binop_to_str(op: binop) -> ~str {
|
||||
add => return ~"+",
|
||||
subtract => return ~"-",
|
||||
mul => return ~"*",
|
||||
div => return ~"/",
|
||||
quot => return ~"/",
|
||||
rem => return ~"%",
|
||||
and => return ~"&&",
|
||||
or => return ~"||",
|
||||
@ -103,8 +103,8 @@ pub fn binop_to_method_name(op: binop) -> Option<~str> {
|
||||
add => return Some(~"add"),
|
||||
subtract => return Some(~"sub"),
|
||||
mul => return Some(~"mul"),
|
||||
div => return Some(~"div"),
|
||||
rem => return Some(~"modulo"),
|
||||
quot => return Some(~"quot"),
|
||||
rem => return Some(~"rem"),
|
||||
bitxor => return Some(~"bitxor"),
|
||||
bitand => return Some(~"bitand"),
|
||||
bitor => return Some(~"bitor"),
|
||||
@ -348,7 +348,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 | div | rem => 12u,
|
||||
mul | quot | rem => 12u,
|
||||
// 'as' sits between here with 11
|
||||
add | subtract => 10u,
|
||||
shl | shr => 9u,
|
||||
|
@ -21,7 +21,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, by_copy, by_ref};
|
||||
use ast::{crate, crate_cfg, decl, decl_item};
|
||||
use ast::{decl_local, default_blk, deref, div, enum_def};
|
||||
use ast::{decl_local, default_blk, deref, quot, enum_def};
|
||||
use ast::{expl, 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};
|
||||
@ -1786,7 +1786,7 @@ pub impl Parser {
|
||||
token::PLUS => aop = add,
|
||||
token::MINUS => aop = subtract,
|
||||
token::STAR => aop = mul,
|
||||
token::SLASH => aop = div,
|
||||
token::SLASH => aop = quot,
|
||||
token::PERCENT => aop = rem,
|
||||
token::CARET => aop = bitxor,
|
||||
token::AND => aop = bitand,
|
||||
|
@ -31,7 +31,7 @@ pub static as_prec: uint = 11u;
|
||||
pub fn token_to_binop(tok: Token) -> Option<ast::binop> {
|
||||
match tok {
|
||||
BINOP(STAR) => Some(mul),
|
||||
BINOP(SLASH) => Some(div),
|
||||
BINOP(SLASH) => Some(quot),
|
||||
BINOP(PERCENT) => Some(rem),
|
||||
// 'as' sits between here with 11
|
||||
BINOP(PLUS) => Some(add),
|
||||
|
@ -1,6 +1,6 @@
|
||||
enum test {
|
||||
div_zero = 1/0, //~ERROR expected constant: divide by zero
|
||||
rem_zero = 1%0 //~ERROR expected constant: modulo zero
|
||||
quot_zero = 1/0, //~ERROR expected constant: quotient zero
|
||||
rem_zero = 1%0 //~ERROR expected constant: remainder zero
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// error-pattern:divide by zero
|
||||
// error-pattern:quotient zero
|
||||
fn main() {
|
||||
let y = 0;
|
||||
let z = 1 / y;
|
||||
|
@ -8,7 +8,7 @@
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// error-pattern:modulo zero
|
||||
// error-pattern:remainder zero
|
||||
fn main() {
|
||||
let y = 0;
|
||||
let z = 1 % y;
|
||||
|
Loading…
Reference in New Issue
Block a user