@@ -42,21 +42,26 @@ pub trait Orderable: Ord {
42
42
fn clamp ( & self , mn : & Self , mx : & Self ) -> Self ;
43
43
}
44
44
45
+ /// Return the smaller number.
45
46
#[ inline( always) ] pub fn min < T : Orderable > ( x : T , y : T ) -> T { x. min ( & y) }
47
+ /// Return the larger number.
46
48
#[ inline( always) ] pub fn max < T : Orderable > ( x : T , y : T ) -> T { x. max ( & y) }
49
+ /// Returns the number constrained within the range `mn <= self <= mx`.
47
50
#[ inline( always) ] pub fn clamp < T : Orderable > ( value : T , mn : T , mx : T ) -> T { value. clamp ( & mn, & mx) }
48
51
49
52
pub trait Zero {
50
53
fn zero ( ) -> Self ; // FIXME (#5527): This should be an associated constant
51
54
fn is_zero ( & self ) -> bool ;
52
55
}
53
56
57
+ /// Returns `0` of appropriate type.
54
58
#[ inline( always) ] pub fn zero < T : Zero > ( ) -> T { Zero :: zero ( ) }
55
59
56
60
pub trait One {
57
61
fn one ( ) -> Self ; // FIXME (#5527): This should be an associated constant
58
62
}
59
63
64
+ /// Returns `1` of appropriate type.
60
65
#[ inline( always) ] pub fn one < T : One > ( ) -> T { One :: one ( ) }
61
66
62
67
pub trait Signed : Num
@@ -69,8 +74,26 @@ pub trait Signed: Num
69
74
fn is_negative ( & self ) -> bool ;
70
75
}
71
76
77
+ /// Computes the absolute value.
78
+ ///
79
+ /// For float, f32, and f64, `NaN` will be returned if the number is `NaN`
72
80
#[ inline( always) ] pub fn abs < T : Signed > ( value : T ) -> T { value. abs ( ) }
81
+ /// The positive difference of two numbers.
82
+ ///
83
+ /// Returns `zero` if the number is less than or equal to `other`,
84
+ /// otherwise the difference between `self` and `other` is returned.
73
85
#[ inline( always) ] pub fn abs_sub < T : Signed > ( x : T , y : T ) -> T { x. abs_sub ( & y) }
86
+ /// Returns the sign of the number.
87
+ ///
88
+ /// For float, f32, f64:
89
+ /// - `1.0` if the number is positive, `+0.0` or `infinity`
90
+ /// - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
91
+ /// - `NaN` if the number is `NaN`
92
+ ///
93
+ /// For int:
94
+ /// - `0` if the number is zero
95
+ /// - `1` if the number is positive
96
+ /// - `-1` if the number is negative
74
97
#[ inline( always) ] pub fn signum < T : Signed > ( value : T ) -> T { value. signum ( ) }
75
98
76
99
pub trait Unsigned : Num { }
@@ -106,7 +129,11 @@ pub trait Integer: Num
106
129
fn is_odd ( & self ) -> bool ;
107
130
}
108
131
132
+ /// Calculates the Greatest Common Divisor (GCD) of the number and `other`.
133
+ ///
134
+ /// The result is always positive.
109
135
#[ inline( always) ] pub fn gcd < T : Integer > ( x : T , y : T ) -> T { x. gcd ( & y) }
136
+ /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
110
137
#[ inline( always) ] pub fn lcm < T : Integer > ( x : T , y : T ) -> T { x. lcm ( & y) }
111
138
112
139
pub trait Round {
@@ -132,10 +159,23 @@ pub trait Algebraic {
132
159
fn hypot ( & self , other : & Self ) -> Self ;
133
160
}
134
161
162
+ /// Raise a number to a power.
163
+ ///
164
+ /// # Example
165
+ ///
166
+ /// ```rust
167
+ /// let sixteen: float = num::pow(2.0, 4.0);
168
+ /// assert_eq!(sixteen, 16.0);
169
+ /// ```
135
170
#[ inline( always) ] pub fn pow < T : Algebraic > ( value : T , n : T ) -> T { value. pow ( & n) }
171
+ /// Take the squre root of a number.
136
172
#[ inline( always) ] pub fn sqrt < T : Algebraic > ( value : T ) -> T { value. sqrt ( ) }
173
+ /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
137
174
#[ inline( always) ] pub fn rsqrt < T : Algebraic > ( value : T ) -> T { value. rsqrt ( ) }
175
+ /// Take the cubic root of a number.
138
176
#[ inline( always) ] pub fn cbrt < T : Algebraic > ( value : T ) -> T { value. cbrt ( ) }
177
+ /// Calculate the length of the hypotenuse of a right-angle triangle given legs of length `x` and
178
+ /// `y`.
139
179
#[ inline( always) ] pub fn hypot < T : Algebraic > ( x : T , y : T ) -> T { x. hypot ( & y) }
140
180
141
181
pub trait Trigonometric {
@@ -151,15 +191,23 @@ pub trait Trigonometric {
151
191
fn sin_cos ( & self ) -> ( Self , Self ) ;
152
192
}
153
193
194
+ /// Sine function.
154
195
#[ inline( always) ] pub fn sin < T : Trigonometric > ( value : T ) -> T { value. sin ( ) }
196
+ /// Cosine function.
155
197
#[ inline( always) ] pub fn cos < T : Trigonometric > ( value : T ) -> T { value. cos ( ) }
198
+ /// Tangent function.
156
199
#[ inline( always) ] pub fn tan < T : Trigonometric > ( value : T ) -> T { value. tan ( ) }
157
200
201
+ /// Compute the arcsine of the number.
158
202
#[ inline( always) ] pub fn asin < T : Trigonometric > ( value : T ) -> T { value. asin ( ) }
203
+ /// Compute the arccosine of the number.
159
204
#[ inline( always) ] pub fn acos < T : Trigonometric > ( value : T ) -> T { value. acos ( ) }
205
+ /// Compute the arctangent of the number.
160
206
#[ inline( always) ] pub fn atan < T : Trigonometric > ( value : T ) -> T { value. atan ( ) }
161
207
208
+ /// Compute the arctangent with 2 arguments.
162
209
#[ inline( always) ] pub fn atan2 < T : Trigonometric > ( x : T , y : T ) -> T { x. atan2 ( & y) }
210
+ /// Simultaneously computes the sine and cosine of the number.
163
211
#[ inline( always) ] pub fn sin_cos < T : Trigonometric > ( value : T ) -> ( T , T ) { value. sin_cos ( ) }
164
212
165
213
pub trait Exponential {
@@ -172,12 +220,18 @@ pub trait Exponential {
172
220
fn log10 ( & self ) -> Self ;
173
221
}
174
222
223
+ /// Returns `e^(value)`, (the exponential function).
175
224
#[ inline( always) ] pub fn exp < T : Exponential > ( value : T ) -> T { value. exp ( ) }
225
+ /// Returns 2 raised to the power of the number, `2^(value)`.
176
226
#[ inline( always) ] pub fn exp2 < T : Exponential > ( value : T ) -> T { value. exp2 ( ) }
177
227
228
+ /// Returns the natural logarithm of the number.
178
229
#[ inline( always) ] pub fn ln < T : Exponential > ( value : T ) -> T { value. ln ( ) }
230
+ /// Returns the logarithm of the number with respect to an arbitrary base.
179
231
#[ inline( always) ] pub fn log < T : Exponential > ( value : T , base : T ) -> T { value. log ( & base) }
232
+ /// Returns the base 2 logarithm of the number.
180
233
#[ inline( always) ] pub fn log2 < T : Exponential > ( value : T ) -> T { value. log2 ( ) }
234
+ /// Returns the base 10 logarithm of the number.
181
235
#[ inline( always) ] pub fn log10 < T : Exponential > ( value : T ) -> T { value. log10 ( ) }
182
236
183
237
pub trait Hyperbolic : Exponential {
@@ -190,12 +244,18 @@ pub trait Hyperbolic: Exponential {
190
244
fn atanh ( & self ) -> Self ;
191
245
}
192
246
247
+ /// Hyperbolic cosine function.
193
248
#[ inline( always) ] pub fn sinh < T : Hyperbolic > ( value : T ) -> T { value. sinh ( ) }
249
+ /// Hyperbolic sine function.
194
250
#[ inline( always) ] pub fn cosh < T : Hyperbolic > ( value : T ) -> T { value. cosh ( ) }
251
+ /// Hyperbolic tangent function.
195
252
#[ inline( always) ] pub fn tanh < T : Hyperbolic > ( value : T ) -> T { value. tanh ( ) }
196
253
254
+ /// Inverse hyperbolic sine function.
197
255
#[ inline( always) ] pub fn asinh < T : Hyperbolic > ( value : T ) -> T { value. asinh ( ) }
256
+ /// Inverse hyperbolic cosine function.
198
257
#[ inline( always) ] pub fn acosh < T : Hyperbolic > ( value : T ) -> T { value. acosh ( ) }
258
+ /// Inverse hyperbolic tangent function.
199
259
#[ inline( always) ] pub fn atanh < T : Hyperbolic > ( value : T ) -> T { value. atanh ( ) }
200
260
201
261
/// Defines constants and methods common to real numbers
@@ -345,8 +405,16 @@ pub trait Float: Real
345
405
fn next_after ( & self , other : Self ) -> Self ;
346
406
}
347
407
408
+ /// Returns the exponential of the number, minus `1`, `exp(n) - 1`, in a way
409
+ /// that is accurate even if the number is close to zero.
348
410
#[ inline( always) ] pub fn exp_m1 < T : Float > ( value : T ) -> T { value. exp_m1 ( ) }
411
+ /// Returns the natural logarithm of the number plus `1`, `ln(n + 1)`, more
412
+ /// accurately than if the operations were performed separately.
349
413
#[ inline( always) ] pub fn ln_1p < T : Float > ( value : T ) -> T { value. ln_1p ( ) }
414
+ /// Fused multiply-add. Computes `(a * b) + c` with only one rounding error.
415
+ ///
416
+ /// This produces a more accurate result with better performance (on some
417
+ /// architectures) than a separate multiplication operation followed by an add.
350
418
#[ inline( always) ] pub fn mul_add < T : Float > ( a : T , b : T , c : T ) -> T { a. mul_add ( b, c) }
351
419
352
420
/// A generic trait for converting a value to a number.
@@ -788,7 +856,7 @@ impl_from_primitive!(u64, n.to_u64())
788
856
impl_from_primitive ! ( f32 , n. to_f32( ) )
789
857
impl_from_primitive ! ( f64 , n. to_f64( ) )
790
858
791
- /// Cast from one machine scalar to another
859
+ /// Cast from one machine scalar to another.
792
860
///
793
861
/// # Example
794
862
///
@@ -841,7 +909,7 @@ pub trait FromStrRadix {
841
909
fn from_str_radix ( str : & str , radix : uint ) -> Option < Self > ;
842
910
}
843
911
844
- /// A utility function that just calls FromStrRadix::from_str_radix
912
+ /// A utility function that just calls FromStrRadix::from_str_radix.
845
913
pub fn from_str_radix < T : FromStrRadix > ( str : & str , radix : uint ) -> Option < T > {
846
914
FromStrRadix :: from_str_radix ( str, radix)
847
915
}
0 commit comments