Skip to content

Commit

Permalink
auto merge of #5990 : bjz/rust/rem-quot, r=catamorphism
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Apr 21, 2013
2 parents 535244c + 01eb5e8 commit 6a31525
Show file tree
Hide file tree
Showing 34 changed files with 344 additions and 235 deletions.
16 changes: 8 additions & 8 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 2 additions & 2 deletions src/etc/kate/rust.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/etc/vim/syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
49 changes: 31 additions & 18 deletions src/libcore/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -265,15 +270,15 @@ 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)]
fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
}

#[cfg(notest)]
impl cmp::Ord for f32 {
impl Ord for f32 {
#[inline(always)]
fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
#[inline(always)]
Expand All @@ -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 }
}

Expand Down
49 changes: 31 additions & 18 deletions src/libcore/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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; }
Expand Down Expand Up @@ -284,15 +289,15 @@ 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)]
fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
}

#[cfg(notest)]
impl cmp::Ord for f64 {
impl Ord for f64 {
#[inline(always)]
fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
#[inline(always)]
Expand All @@ -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 }
}

Expand Down
45 changes: 29 additions & 16 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ use to_str;
use from_str;

#[cfg(notest)] use cmp::{Eq, Ord};
#[cfg(notest)] use ops;

pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt};
#[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, 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};
Expand Down Expand Up @@ -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 }
}
Expand Down
Loading

0 comments on commit 6a31525

Please sign in to comment.