Skip to content

Commit 6a31525

Browse files
committed
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.
2 parents 535244c + 01eb5e8 commit 6a31525

34 files changed

+344
-235
lines changed

doc/rust.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,10 +1467,10 @@ A complete list of the built-in language items follows:
14671467
: Elements can be subtracted.
14681468
`mul`
14691469
: Elements can be multiplied.
1470-
`div`
1471-
: Elements can be divided.
1472-
`mod`
1473-
: Elements have a modulo operation.
1470+
`quot`
1471+
: Elements have a quotient operation.
1472+
`rem`
1473+
: Elements have a remainder operation.
14741474
`neg`
14751475
: Elements can be negated arithmetically.
14761476
`not`
@@ -1856,11 +1856,11 @@ The default meaning of the operators on standard types is given here.
18561856
: Multiplication.
18571857
Calls the `mul` method on the `core::ops::Mul` trait.
18581858
`/`
1859-
: Division.
1860-
Calls the `div` method on the `core::ops::Div` trait.
1859+
: Quotient.
1860+
Calls the `quot` method on the `core::ops::Quot` trait.
18611861
`%`
1862-
: Modulo (a.k.a. "remainder").
1863-
Calls the `modulo` method on the `core::ops::Modulo` trait.
1862+
: Remainder.
1863+
Calls the `rem` method on the `core::ops::Rem` trait.
18641864

18651865
#### Bitwise operators
18661866

doc/tutorial.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ The nil type, written `()`, has a single value, also written `()`.
362362
## Operators
363363

364364
Rust's set of operators contains very few surprises. Arithmetic is done with
365-
`*`, `/`, `%`, `+`, and `-` (multiply, divide, take remainder, add, and subtract). `-` is
365+
`*`, `/`, `%`, `+`, and `-` (multiply, quotient, remainder, add, and subtract). `-` is
366366
also a unary prefix operator that negates numbers. As in C, the bitwise operators
367367
`>>`, `<<`, `&`, `|`, and `^` are also supported.
368368

src/etc/kate/rust.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
<item> Add </item>
5858
<item> Sub </item>
5959
<item> Mul </item>
60-
<item> Div </item>
61-
<item> Modulo </item>
60+
<item> Quot </item>
61+
<item> Rem </item>
6262
<item> Neg </item>
6363
<item> BitAnd </item>
6464
<item> BitOr </item>

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ syn keyword rustType off_t dev_t ino_t pid_t mode_t ssize_t
4646

4747
syn keyword rustTrait Const Copy Send Owned " inherent traits
4848
syn keyword rustTrait Eq Ord Num Ptr
49-
syn keyword rustTrait Drop Add Sub Mul Div Modulo Neg BitAnd BitOr
49+
syn keyword rustTrait Drop Add Sub Mul Quot Rem Neg BitAnd BitOr
5050
syn keyword rustTrait BitXor Shl Shr Index
5151

5252
syn keyword rustSelf self

src/libcore/core.rc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,12 @@ they contained the following prologue:
7575

7676
pub use kinds::{Const, Copy, Owned, Durable};
7777
pub use ops::{Drop};
78+
#[cfg(stage0)]
7879
pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
80+
#[cfg(stage1)]
81+
#[cfg(stage2)]
82+
#[cfg(stage3)]
83+
pub use ops::{Add, Sub, Mul, Quot, Rem, Neg, Not};
7984
pub use ops::{BitAnd, BitOr, BitXor};
8085
pub use ops::{Shl, Shr, Index};
8186

src/libcore/num/f32.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ use option::Option;
1616
use from_str;
1717
use to_str;
1818

19-
#[cfg(notest)] use cmp;
20-
#[cfg(notest)] use ops;
19+
#[cfg(notest)] use cmp::{Eq, Ord};
20+
#[cfg(stage0,notest)]
21+
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
22+
#[cfg(stage1,notest)]
23+
#[cfg(stage2,notest)]
24+
#[cfg(stage3,notest)]
25+
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
2126

2227
pub use cmath::c_float_targ_consts::*;
2328

@@ -131,7 +136,7 @@ pub fn sub(x: f32, y: f32) -> f32 { return x - y; }
131136
pub fn mul(x: f32, y: f32) -> f32 { return x * y; }
132137

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

136141
#[inline(always)]
137142
pub fn rem(x: f32, y: f32) -> f32 { return x % y; }
@@ -265,15 +270,15 @@ pub fn logarithm(n: f32, b: f32) -> f32 {
265270
}
266271

267272
#[cfg(notest)]
268-
impl cmp::Eq for f32 {
273+
impl Eq for f32 {
269274
#[inline(always)]
270275
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
271276
#[inline(always)]
272277
fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
273278
}
274279

275280
#[cfg(notest)]
276-
impl cmp::Ord for f32 {
281+
impl Ord for f32 {
277282
#[inline(always)]
278283
fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
279284
#[inline(always)]
@@ -295,33 +300,41 @@ impl num::One for f32 {
295300
}
296301

297302
#[cfg(notest)]
298-
impl ops::Add<f32,f32> for f32 {
299-
#[inline(always)]
303+
impl Add<f32,f32> for f32 {
300304
fn add(&self, other: &f32) -> f32 { *self + *other }
301305
}
302306
#[cfg(notest)]
303-
impl ops::Sub<f32,f32> for f32 {
304-
#[inline(always)]
307+
impl Sub<f32,f32> for f32 {
305308
fn sub(&self, other: &f32) -> f32 { *self - *other }
306309
}
307310
#[cfg(notest)]
308-
impl ops::Mul<f32,f32> for f32 {
309-
#[inline(always)]
311+
impl Mul<f32,f32> for f32 {
310312
fn mul(&self, other: &f32) -> f32 { *self * *other }
311313
}
312-
#[cfg(notest)]
313-
impl ops::Div<f32,f32> for f32 {
314-
#[inline(always)]
314+
#[cfg(stage0,notest)]
315+
impl Div<f32,f32> for f32 {
315316
fn div(&self, other: &f32) -> f32 { *self / *other }
316317
}
317-
#[cfg(notest)]
318-
impl ops::Modulo<f32,f32> for f32 {
318+
#[cfg(stage1,notest)]
319+
#[cfg(stage2,notest)]
320+
#[cfg(stage3,notest)]
321+
impl Quot<f32,f32> for f32 {
319322
#[inline(always)]
323+
fn quot(&self, other: &f32) -> f32 { *self / *other }
324+
}
325+
#[cfg(stage0,notest)]
326+
impl Modulo<f32,f32> for f32 {
320327
fn modulo(&self, other: &f32) -> f32 { *self % *other }
321328
}
322-
#[cfg(notest)]
323-
impl ops::Neg<f32> for f32 {
329+
#[cfg(stage1,notest)]
330+
#[cfg(stage2,notest)]
331+
#[cfg(stage3,notest)]
332+
impl Rem<f32,f32> for f32 {
324333
#[inline(always)]
334+
fn rem(&self, other: &f32) -> f32 { *self % *other }
335+
}
336+
#[cfg(notest)]
337+
impl Neg<f32> for f32 {
325338
fn neg(&self) -> f32 { -*self }
326339
}
327340

src/libcore/num/f64.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ use option::Option;
1616
use to_str;
1717
use from_str;
1818

19-
#[cfg(notest)] use cmp;
20-
#[cfg(notest)] use ops;
19+
#[cfg(notest)] use cmp::{Eq, Ord};
20+
#[cfg(stage0,notest)]
21+
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
22+
#[cfg(stage1,notest)]
23+
#[cfg(stage2,notest)]
24+
#[cfg(stage3,notest)]
25+
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
2126

2227
pub use cmath::c_double_targ_consts::*;
2328
pub use cmp::{min, max};
@@ -155,7 +160,7 @@ pub fn sub(x: f64, y: f64) -> f64 { return x - y; }
155160
pub fn mul(x: f64, y: f64) -> f64 { return x * y; }
156161

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

160165
#[inline(always)]
161166
pub fn rem(x: f64, y: f64) -> f64 { return x % y; }
@@ -284,15 +289,15 @@ pub fn logarithm(n: f64, b: f64) -> f64 {
284289
}
285290

286291
#[cfg(notest)]
287-
impl cmp::Eq for f64 {
292+
impl Eq for f64 {
288293
#[inline(always)]
289294
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
290295
#[inline(always)]
291296
fn ne(&self, other: &f64) -> bool { (*self) != (*other) }
292297
}
293298

294299
#[cfg(notest)]
295-
impl cmp::Ord for f64 {
300+
impl Ord for f64 {
296301
#[inline(always)]
297302
fn lt(&self, other: &f64) -> bool { (*self) < (*other) }
298303
#[inline(always)]
@@ -314,33 +319,41 @@ impl num::One for f64 {
314319
}
315320

316321
#[cfg(notest)]
317-
impl ops::Add<f64,f64> for f64 {
318-
#[inline(always)]
322+
impl Add<f64,f64> for f64 {
319323
fn add(&self, other: &f64) -> f64 { *self + *other }
320324
}
321325
#[cfg(notest)]
322-
impl ops::Sub<f64,f64> for f64 {
323-
#[inline(always)]
326+
impl Sub<f64,f64> for f64 {
324327
fn sub(&self, other: &f64) -> f64 { *self - *other }
325328
}
326329
#[cfg(notest)]
327-
impl ops::Mul<f64,f64> for f64 {
328-
#[inline(always)]
330+
impl Mul<f64,f64> for f64 {
329331
fn mul(&self, other: &f64) -> f64 { *self * *other }
330332
}
331-
#[cfg(notest)]
332-
impl ops::Div<f64,f64> for f64 {
333-
#[inline(always)]
333+
#[cfg(stage0,notest)]
334+
impl Div<f64,f64> for f64 {
334335
fn div(&self, other: &f64) -> f64 { *self / *other }
335336
}
336-
#[cfg(notest)]
337-
impl ops::Modulo<f64,f64> for f64 {
337+
#[cfg(stage1,notest)]
338+
#[cfg(stage2,notest)]
339+
#[cfg(stage3,notest)]
340+
impl Quot<f64,f64> for f64 {
338341
#[inline(always)]
342+
fn quot(&self, other: &f64) -> f64 { *self / *other }
343+
}
344+
#[cfg(stage0,notest)]
345+
impl Modulo<f64,f64> for f64 {
339346
fn modulo(&self, other: &f64) -> f64 { *self % *other }
340347
}
341-
#[cfg(notest)]
342-
impl ops::Neg<f64> for f64 {
348+
#[cfg(stage1,notest)]
349+
#[cfg(stage2,notest)]
350+
#[cfg(stage3,notest)]
351+
impl Rem<f64,f64> for f64 {
343352
#[inline(always)]
353+
fn rem(&self, other: &f64) -> f64 { *self % *other }
354+
}
355+
#[cfg(notest)]
356+
impl Neg<f64> for f64 {
344357
fn neg(&self) -> f64 { -*self }
345358
}
346359

src/libcore/num/float.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ use to_str;
2828
use from_str;
2929

3030
#[cfg(notest)] use cmp::{Eq, Ord};
31-
#[cfg(notest)] use ops;
32-
33-
pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt};
31+
#[cfg(stage0,notest)]
32+
use ops::{Add, Sub, Mul, Div, Modulo, Neg};
33+
#[cfg(stage1,notest)]
34+
#[cfg(stage2,notest)]
35+
#[cfg(stage3,notest)]
36+
use ops::{Add, Sub, Mul, Quot, Rem, Neg};
37+
38+
pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt};
3439
pub use f64::logarithm;
3540
pub use f64::{acos, asin, atan2, cbrt, ceil, copysign, cosh, floor};
3641
pub use f64::{erf, erfc, exp, expm1, exp2, abs_sub};
@@ -449,33 +454,41 @@ impl num::Round for float {
449454
}
450455
451456
#[cfg(notest)]
452-
impl ops::Add<float,float> for float {
453-
#[inline(always)]
457+
impl Add<float,float> for float {
454458
fn add(&self, other: &float) -> float { *self + *other }
455459
}
456460
#[cfg(notest)]
457-
impl ops::Sub<float,float> for float {
458-
#[inline(always)]
461+
impl Sub<float,float> for float {
459462
fn sub(&self, other: &float) -> float { *self - *other }
460463
}
461464
#[cfg(notest)]
462-
impl ops::Mul<float,float> for float {
463-
#[inline(always)]
465+
impl Mul<float,float> for float {
464466
fn mul(&self, other: &float) -> float { *self * *other }
465467
}
466-
#[cfg(notest)]
467-
impl ops::Div<float,float> for float {
468-
#[inline(always)]
468+
#[cfg(stage0,notest)]
469+
impl Div<float,float> for float {
469470
fn div(&self, other: &float) -> float { *self / *other }
470471
}
471-
#[cfg(notest)]
472-
impl ops::Modulo<float,float> for float {
472+
#[cfg(stage1,notest)]
473+
#[cfg(stage2,notest)]
474+
#[cfg(stage3,notest)]
475+
impl Quot<float,float> for float {
473476
#[inline(always)]
477+
fn quot(&self, other: &float) -> float { *self / *other }
478+
}
479+
#[cfg(stage0,notest)]
480+
impl Modulo<float,float> for float {
474481
fn modulo(&self, other: &float) -> float { *self % *other }
475482
}
476-
#[cfg(notest)]
477-
impl ops::Neg<float> for float {
483+
#[cfg(stage1,notest)]
484+
#[cfg(stage2,notest)]
485+
#[cfg(stage3,notest)]
486+
impl Rem<float,float> for float {
478487
#[inline(always)]
488+
fn rem(&self, other: &float) -> float { *self % *other }
489+
}
490+
#[cfg(notest)]
491+
impl Neg<float> for float {
479492
fn neg(&self) -> float { -*self }
480493
}
481494

0 commit comments

Comments
 (0)