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