8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- //! Numeric traits and functions for generic mathematics.
12
- //!
13
- //! These are implemented for the primitive numeric types in `std::{u8, u16,
14
- //! u32, u64, uint, i8, i16, i32, i64, int, f32, f64, float}`.
11
+ //! An interface for numeric types
15
12
16
13
#[ allow( missing_doc) ] ;
17
14
@@ -22,7 +19,9 @@ use option::Option;
22
19
23
20
pub mod strconv;
24
21
22
+ ///
25
23
/// The base trait for numeric types
24
+ ///
26
25
pub trait Num : Eq + Zero + One
27
26
+ Neg < Self >
28
27
+ Add < Self , Self >
@@ -46,23 +45,18 @@ pub trait Orderable: Ord {
46
45
fn clamp ( & self , mn : & Self , mx : & Self ) -> Self ;
47
46
}
48
47
49
- #[ inline( always) ] pub fn min < T : Orderable > ( x : T , y : T ) -> T { x. min ( & y) }
50
- #[ inline( always) ] pub fn max < T : Orderable > ( x : T , y : T ) -> T { x. max ( & y) }
51
- #[ inline( always) ] pub fn clamp < T : Orderable > ( value : T , mn : T , mx : T ) -> T { value. clamp ( & mn, & mx) }
48
+ #[ inline( always) ] pub fn min < T : Orderable > ( a : T , b : T ) -> T { a. min ( & b) }
49
+ #[ inline( always) ] pub fn max < T : Orderable > ( a : T , b : T ) -> T { a. max ( & b) }
52
50
53
51
pub trait Zero {
54
52
fn zero ( ) -> Self ; // FIXME (#5527): This should be an associated constant
55
53
fn is_zero ( & self ) -> bool ;
56
54
}
57
55
58
- #[ inline( always) ] pub fn zero < T : Zero > ( ) -> T { Zero :: zero ( ) }
59
-
60
56
pub trait One {
61
57
fn one ( ) -> Self ; // FIXME (#5527): This should be an associated constant
62
58
}
63
59
64
- #[ inline( always) ] pub fn one < T : One > ( ) -> T { One :: one ( ) }
65
-
66
60
pub trait Signed : Num
67
61
+ Neg < Self > {
68
62
fn abs ( & self ) -> Self ;
@@ -74,7 +68,6 @@ pub trait Signed: Num
74
68
}
75
69
76
70
#[ inline( always) ] pub fn abs < T : Signed > ( value : T ) -> T { value. abs ( ) }
77
- #[ inline( always) ] pub fn abs_sub < T : Signed > ( x : T , y : T ) -> T { x. abs_sub ( & y) }
78
71
#[ inline( always) ] pub fn signum < T : Signed > ( value : T ) -> T { value. signum ( ) }
79
72
80
73
pub trait Unsigned : Num { }
@@ -97,9 +90,6 @@ pub trait Integer: Num
97
90
fn is_odd ( & self ) -> bool ;
98
91
}
99
92
100
- #[ inline( always) ] pub fn gcd < T : Integer > ( x : T , y : T ) -> T { x. gcd ( & y) }
101
- #[ inline( always) ] pub fn lcm < T : Integer > ( x : T , y : T ) -> T { x. lcm ( & y) }
102
-
103
93
pub trait Round {
104
94
fn floor ( & self ) -> Self ;
105
95
fn ceil ( & self ) -> Self ;
@@ -123,21 +113,15 @@ pub trait Algebraic {
123
113
fn hypot ( & self , other : & Self ) -> Self ;
124
114
}
125
115
126
- #[ inline( always) ] pub fn pow < T : Algebraic > ( value : T , n : T ) -> T { value. pow ( & n) }
127
116
#[ inline( always) ] pub fn sqrt < T : Algebraic > ( value : T ) -> T { value. sqrt ( ) }
128
- #[ inline( always) ] pub fn rsqrt < T : Algebraic > ( value : T ) -> T { value. rsqrt ( ) }
129
- #[ inline( always) ] pub fn cbrt < T : Algebraic > ( value : T ) -> T { value. cbrt ( ) }
130
- #[ inline( always) ] pub fn hypot < T : Algebraic > ( x : T , y : T ) -> T { x. hypot ( & y) }
131
117
132
118
pub trait Trigonometric {
133
119
fn sin ( & self ) -> Self ;
134
120
fn cos ( & self ) -> Self ;
135
121
fn tan ( & self ) -> Self ;
136
-
137
122
fn asin ( & self ) -> Self ;
138
123
fn acos ( & self ) -> Self ;
139
124
fn atan ( & self ) -> Self ;
140
-
141
125
fn atan2 ( & self , other : & Self ) -> Self ;
142
126
fn sin_cos ( & self ) -> ( Self , Self ) ;
143
127
}
@@ -151,12 +135,10 @@ pub trait Trigonometric {
151
135
#[ inline( always) ] pub fn atan < T : Trigonometric > ( value : T ) -> T { value. atan ( ) }
152
136
153
137
#[ inline( always) ] pub fn atan2 < T : Trigonometric > ( x : T , y : T ) -> T { x. atan2 ( & y) }
154
- #[ inline( always) ] pub fn sin_cos < T : Trigonometric > ( value : T ) -> ( T , T ) { value. sin_cos ( ) }
155
138
156
139
pub trait Exponential {
157
140
fn exp ( & self ) -> Self ;
158
141
fn exp2 ( & self ) -> Self ;
159
-
160
142
fn ln ( & self ) -> Self ;
161
143
fn log ( & self , base : & Self ) -> Self ;
162
144
fn log2 ( & self ) -> Self ;
@@ -175,7 +157,6 @@ pub trait Hyperbolic: Exponential {
175
157
fn sinh ( & self ) -> Self ;
176
158
fn cosh ( & self ) -> Self ;
177
159
fn tanh ( & self ) -> Self ;
178
-
179
160
fn asinh ( & self ) -> Self ;
180
161
fn acosh ( & self ) -> Self ;
181
162
fn atanh ( & self ) -> Self ;
@@ -189,7 +170,9 @@ pub trait Hyperbolic: Exponential {
189
170
#[ inline( always) ] pub fn acosh < T : Hyperbolic > ( value : T ) -> T { value. acosh ( ) }
190
171
#[ inline( always) ] pub fn atanh < T : Hyperbolic > ( value : T ) -> T { value. atanh ( ) }
191
172
173
+ ///
192
174
/// Defines constants and methods common to real numbers
175
+ ///
193
176
pub trait Real : Signed
194
177
+ Fractional
195
178
+ Algebraic
@@ -220,7 +203,9 @@ pub trait Real: Signed
220
203
fn to_radians ( & self ) -> Self ;
221
204
}
222
205
206
+ ///
223
207
/// Methods that are harder to implement and not commonly used.
208
+ ///
224
209
pub trait RealExt : Real {
225
210
// FIXME (#5527): usages of `int` should be replaced with an associated
226
211
// integer type once these are implemented
@@ -238,7 +223,9 @@ pub trait RealExt: Real {
238
223
fn yn ( & self , n : int ) -> Self ;
239
224
}
240
225
226
+ ///
241
227
/// Collects the bitwise operators under one trait.
228
+ ///
242
229
pub trait Bitwise : Not < Self >
243
230
+ BitAnd < Self , Self >
244
231
+ BitOr < Self , Self >
@@ -258,9 +245,11 @@ pub trait Bounded {
258
245
fn max_value ( ) -> Self ;
259
246
}
260
247
248
+ ///
261
249
/// Specifies the available operations common to all of Rust's core numeric primitives.
262
250
/// These may not always make sense from a purely mathematical point of view, but
263
251
/// may be useful for systems programming.
252
+ ///
264
253
pub trait Primitive : Num
265
254
+ NumCast
266
255
+ Bounded
@@ -275,13 +264,17 @@ pub trait Primitive: Num
275
264
fn bytes ( ) -> uint ;
276
265
}
277
266
267
+ ///
278
268
/// A collection of traits relevant to primitive signed and unsigned integers
269
+ ///
279
270
pub trait Int : Integer
280
271
+ Primitive
281
272
+ Bitwise
282
273
+ BitCount { }
283
274
275
+ ///
284
276
/// Used for representing the classification of floating point numbers
277
+ ///
285
278
#[ deriving( Eq ) ]
286
279
pub enum FPCategory {
287
280
/// "Not a Number", often obtained by dividing by zero
@@ -296,7 +289,9 @@ pub enum FPCategory {
296
289
FPNormal ,
297
290
}
298
291
292
+ ///
299
293
/// Primitive floating point numbers
294
+ ///
300
295
pub trait Float : Real
301
296
+ Signed
302
297
+ Primitive
@@ -330,10 +325,7 @@ pub trait Float: Real
330
325
fn next_after ( & self , other : Self ) -> Self ;
331
326
}
332
327
333
- #[ inline( always) ] pub fn exp_m1 < T : Float > ( value : T ) -> T { value. exp_m1 ( ) }
334
- #[ inline( always) ] pub fn ln_1p < T : Float > ( value : T ) -> T { value. ln_1p ( ) }
335
- #[ inline( always) ] pub fn mul_add < T : Float > ( a : T , b : T , c : T ) -> T { a. mul_add ( b, c) }
336
-
328
+ ///
337
329
/// Cast from one machine scalar to another
338
330
///
339
331
/// # Example
@@ -348,7 +340,9 @@ pub fn cast<T:NumCast,U:NumCast>(n: T) -> U {
348
340
NumCast :: from ( n)
349
341
}
350
342
343
+ ///
351
344
/// An interface for casting between machine scalars
345
+ ///
352
346
pub trait NumCast {
353
347
fn from < T : NumCast > ( n : T ) -> Self ;
354
348
@@ -420,6 +414,7 @@ pub trait FromStrRadix {
420
414
pub fn from_str_radix ( str : & str , radix : uint ) -> Option < Self > ;
421
415
}
422
416
417
+ ///
423
418
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
424
419
///
425
420
/// Returns `radix^pow` as `T`.
0 commit comments