10
10
//
11
11
// ignore-lexer-test FIXME #15679
12
12
13
- //! Numeric traits and functions for generic mathematics
13
+ //! Numeric traits and functions for the built-in numeric types.
14
14
15
15
#![ allow( missing_docs) ]
16
16
@@ -33,45 +33,36 @@ pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
33
33
( x / y, x % y)
34
34
}
35
35
36
- /// Useful functions for signed numbers (i.e. numbers that can be negative) .
36
+ /// A built-in signed number .
37
37
pub trait Signed : Neg < Self > {
38
- /// Computes the absolute value.
39
- ///
40
- /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
41
- ///
42
- /// For signed integers, `::MIN` will be returned if the number is `::MIN`.
38
+ /// Computes the absolute value of `self`.
43
39
fn abs ( self ) -> Self ;
44
40
45
- /// Returns the sign of the number.
46
- ///
47
- /// For `f32` and `f64`:
48
- ///
49
- /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
50
- /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
51
- /// * `NaN` if the number is `NaN`
52
- ///
53
- /// For signed integers:
54
- ///
55
- /// * `0` if the number is zero
56
- /// * `1` if the number is positive
57
- /// * `-1` if the number is negative
41
+ /// Returns a number (either `-1`, `0` or `1`) representing sign of `self`.
58
42
fn signum ( self ) -> Self ;
59
43
60
- /// Returns true if the number is positive and false if the number is zero or negative .
44
+ /// Returns ` true` if `self` is a positive value .
61
45
fn is_positive ( self ) -> bool ;
62
46
63
- /// Returns true if the number is negative and false if the number is zero or positive .
47
+ /// Returns ` true` if `self` is a negative value .
64
48
fn is_negative ( self ) -> bool ;
65
49
}
66
50
67
51
macro_rules! signed_int_impl {
68
52
( $T: ty) => {
69
53
impl Signed for $T {
54
+ /// Computes the absolute value. `Int::min_value()` will be returned
55
+ /// if the number is `Int::min_value()`.
70
56
#[ inline]
71
57
fn abs( self ) -> $T {
72
58
if self . is_negative( ) { -self } else { self }
73
59
}
74
60
61
+ /// # Returns
62
+ ///
63
+ /// - `0` if the number is zero
64
+ /// - `1` if the number is positive
65
+ /// - `-1` if the number is negative
75
66
#[ inline]
76
67
fn signum( self ) -> $T {
77
68
match self {
@@ -81,9 +72,13 @@ macro_rules! signed_int_impl {
81
72
}
82
73
}
83
74
75
+ /// Returns `true` if `self` is positive and `false` if the number
76
+ /// is zero or negative.
84
77
#[ inline]
85
78
fn is_positive( self ) -> bool { self > 0 }
86
79
80
+ /// Returns `true` if `self` is negative and `false` if the number
81
+ /// is zero or positive.
87
82
#[ inline]
88
83
fn is_negative( self ) -> bool { self < 0 }
89
84
}
@@ -99,7 +94,8 @@ signed_int_impl!(int)
99
94
macro_rules! signed_float_impl {
100
95
( $T: ty, $fabs: path, $fcopysign: path) => {
101
96
impl Signed for $T {
102
- /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
97
+ /// Computes the absolute value. Returns `Float::nan()` if the
98
+ /// number is `Float::nan()`.
103
99
#[ inline]
104
100
fn abs( self ) -> $T {
105
101
unsafe { $fabs( self ) }
@@ -144,7 +140,7 @@ signed_float_impl!(f64,
144
140
intrinsics:: fabsf64,
145
141
intrinsics:: copysignf64)
146
142
147
- /// Raises a value to the power of exp, using exponentiation by squaring.
143
+ /// Raises a `base` to the power of ` exp` , using exponentiation by squaring.
148
144
///
149
145
/// # Example
150
146
///
@@ -169,8 +165,7 @@ pub fn pow<T: Int>(mut base: T, mut exp: uint) -> T {
169
165
}
170
166
}
171
167
172
- /// A primitive signed or unsigned integer equipped with various bitwise
173
- /// operators, bit counting methods, and endian conversion functions.
168
+ /// A built-in signed or unsigned integer.
174
169
pub trait Int
175
170
: Copy + Clone
176
171
+ NumCast
@@ -188,23 +183,23 @@ pub trait Int
188
183
+ Shl < uint , Self >
189
184
+ Shr < uint , Self >
190
185
{
191
- /// Returns the `0` value of this integer.
186
+ /// Returns the `0` value of this integer type .
192
187
// FIXME (#5527): Should be an associated constant
193
188
fn zero ( ) -> Self ;
194
189
195
- /// Returns the `1` value of this integer.
190
+ /// Returns the `1` value of this integer type .
196
191
// FIXME (#5527): Should be an associated constant
197
192
fn one ( ) -> Self ;
198
193
199
- /// Returns the smallest value that can be represented by this integer.
194
+ /// Returns the smallest value that can be represented by this integer type .
200
195
// FIXME (#5527): Should be and associated constant
201
196
fn min_value ( ) -> Self ;
202
197
203
- /// Returns the largest value that can be represented by this integer.
198
+ /// Returns the largest value that can be represented by this integer type .
204
199
// FIXME (#5527): Should be and associated constant
205
200
fn max_value ( ) -> Self ;
206
201
207
- /// Returns the number of ones in the binary representation of the integer .
202
+ /// Returns the number of ones in the binary representation of `self` .
208
203
///
209
204
/// # Example
210
205
///
@@ -215,7 +210,7 @@ pub trait Int
215
210
/// ```
216
211
fn count_ones ( self ) -> uint ;
217
212
218
- /// Returns the number of zeros in the binary representation of the integer .
213
+ /// Returns the number of zeros in the binary representation of `self` .
219
214
///
220
215
/// # Example
221
216
///
@@ -230,7 +225,7 @@ pub trait Int
230
225
}
231
226
232
227
/// Returns the number of leading zeros in the binary representation
233
- /// of the integer .
228
+ /// of `self` .
234
229
///
235
230
/// # Example
236
231
///
@@ -242,7 +237,7 @@ pub trait Int
242
237
fn leading_zeros ( self ) -> uint ;
243
238
244
239
/// Returns the number of trailing zeros in the binary representation
245
- /// of the integer .
240
+ /// of `self` .
246
241
///
247
242
/// # Example
248
243
///
@@ -331,7 +326,7 @@ pub trait Int
331
326
if cfg ! ( target_endian = "little" ) { x } else { x. swap_bytes ( ) }
332
327
}
333
328
334
- /// Convert the integer to big endian from the target's endianness.
329
+ /// Convert `self` to big endian from the target's endianness.
335
330
///
336
331
/// On big endian this is a no-op. On little endian the bytes are swapped.
337
332
///
@@ -351,7 +346,7 @@ pub trait Int
351
346
if cfg ! ( target_endian = "big" ) { self } else { self . swap_bytes ( ) }
352
347
}
353
348
354
- /// Convert the integer to little endian from the target's endianness.
349
+ /// Convert `self` to little endian from the target's endianness.
355
350
///
356
351
/// On little endian this is a no-op. On big endian the bytes are swapped.
357
352
///
@@ -371,8 +366,8 @@ pub trait Int
371
366
if cfg ! ( target_endian = "little" ) { self } else { self . swap_bytes ( ) }
372
367
}
373
368
374
- /// Adds two numbers, checking for overflow. If overflow occurs, `None` is
375
- /// returned .
369
+ /// Checked integer addition. Computes `self + other`, returning `None` if
370
+ /// overflow occurred .
376
371
///
377
372
/// # Example
378
373
///
@@ -384,8 +379,8 @@ pub trait Int
384
379
/// ```
385
380
fn checked_add ( self , other : Self ) -> Option < Self > ;
386
381
387
- /// Subtracts two numbers, checking for underflow. If underflow occurs,
388
- /// `None` is returned .
382
+ /// Checked integer subtraction. Computes `self + other`, returning `None`
383
+ /// if underflow occurred .
389
384
///
390
385
/// # Example
391
386
///
@@ -397,8 +392,8 @@ pub trait Int
397
392
/// ```
398
393
fn checked_sub ( self , other : Self ) -> Option < Self > ;
399
394
400
- /// Multiplies two numbers, checking for underflow or overflow. If underflow
401
- /// or overflow occurs, `None` is returned .
395
+ /// Checked integer multiplication. Computes `self + other`, returning
396
+ /// `None` if underflow or overflow occurred .
402
397
///
403
398
/// # Example
404
399
///
@@ -410,8 +405,8 @@ pub trait Int
410
405
/// ```
411
406
fn checked_mul ( self , other : Self ) -> Option < Self > ;
412
407
413
- /// Divides two numbers, checking for underflow, overflow and division by
414
- /// zero. If underflow occurs, `None` is returned .
408
+ /// Checked integer division. Computes `self + other` returning `None` if
409
+ /// `self == 0` or the operation results in underflow or overflow .
415
410
///
416
411
/// # Example
417
412
///
@@ -425,8 +420,8 @@ pub trait Int
425
420
#[ inline]
426
421
fn checked_div ( self , other : Self ) -> Option < Self > ;
427
422
428
- /// Saturating addition. Returns `self + other`, saturating at the
429
- /// numeric bounds instead of overflowing.
423
+ /// Saturating integer addition. Computes `self + other`, saturating at
424
+ /// the numeric bounds instead of overflowing.
430
425
#[ inline]
431
426
fn saturating_add ( self , other : Self ) -> Self {
432
427
match self . checked_add ( other) {
@@ -436,8 +431,8 @@ pub trait Int
436
431
}
437
432
}
438
433
439
- /// Saturating subtraction. Returns `self - other`, saturating at the
440
- /// numeric bounds instead of overflowing.
434
+ /// Saturating integer subtraction. Computes `self - other`, saturating at
435
+ /// the numeric bounds instead of overflowing.
441
436
#[ inline]
442
437
fn saturating_sub ( self , other : Self ) -> Self {
443
438
match self . checked_sub ( other) {
@@ -685,7 +680,7 @@ int_impl!(int = i64, u64, 64,
685
680
intrinsics:: i64_sub_with_overflow,
686
681
intrinsics:: i64_mul_with_overflow)
687
682
688
- /// Unsigned integers
683
+ /// A built-in unsigned integer.
689
684
pub trait UnsignedInt : Int {
690
685
/// Returns `true` iff `self == 2^k` for some `k`.
691
686
fn is_power_of_two ( self ) -> bool {
@@ -1229,7 +1224,7 @@ pub enum FPCategory {
1229
1224
FPNormal ,
1230
1225
}
1231
1226
1232
- /// Operations on the built-in floating point numbers .
1227
+ /// A built-in floating point number .
1233
1228
// FIXME(#5527): In a future version of Rust, many of these functions will
1234
1229
// become constants.
1235
1230
//
0 commit comments