@@ -136,26 +136,87 @@ pub trait Integer: Num
136
136
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
137
137
#[ inline( always) ] pub fn lcm < T : Integer > ( x : T , y : T ) -> T { x. lcm ( & y) }
138
138
139
+ /// A collection of rounding operations.
139
140
pub trait Round {
141
+ /// Return the largest integer less than or equal to a number.
142
+ ///
143
+ /// # Example
144
+ ///
145
+ /// ```rust
146
+ /// assert_approx_eq!(1.3f32.floor(), 1.0);
147
+ /// assert_approx_eq!((-1.3f32).floor(), -2.0);
148
+ /// ```
140
149
fn floor ( & self ) -> Self ;
150
+
151
+ /// Return the smallest integer greater than or equal to a number.
152
+ ///
153
+ /// # Example
154
+ ///
155
+ /// ```rust
156
+ /// assert_approx_eq!(1.3f32.ceil(), 2.0);
157
+ /// assert_approx_eq!((-1.3f32).ceil(), -1.0);
158
+ /// ```
141
159
fn ceil ( & self ) -> Self ;
160
+
161
+ /// Return the nearest integer to a number. Round half-way cases away from
162
+ /// `0.0`.
163
+ ///
164
+ /// # Example
165
+ ///
166
+ /// ```rust
167
+ /// assert_approx_eq!(1.3f32.round(), 1.0);
168
+ /// assert_approx_eq!((-1.3f32).round(), -1.0);
169
+ /// assert_approx_eq!(1.5f32.round(), 2.0);
170
+ /// assert_approx_eq!((-1.5f32).round(), -2.0);
171
+ /// ```
142
172
fn round ( & self ) -> Self ;
173
+
174
+ /// Return the integer part of a number.
175
+ ///
176
+ /// # Example
177
+ ///
178
+ /// ```rust
179
+ /// assert_approx_eq!(1.3f32.round(), 1.0);
180
+ /// assert_approx_eq!((-1.3f32).round(), -1.0);
181
+ /// assert_approx_eq!(1.5f32.round(), 1.0);
182
+ /// assert_approx_eq!((-1.5f32).round(), -1.0);
183
+ /// ```
143
184
fn trunc ( & self ) -> Self ;
185
+
186
+ /// Return the fractional part of a number.
187
+ ///
188
+ /// # Example
189
+ ///
190
+ /// ```rust
191
+ /// assert_approx_eq!(1.3f32.round(), 0.3);
192
+ /// assert_approx_eq!((-1.3f32).round(), -0.3);
193
+ /// assert_approx_eq!(1.5f32.round(), 0.5);
194
+ /// assert_approx_eq!((-1.5f32).round(), -0.5);
195
+ /// ```
144
196
fn fract ( & self ) -> Self ;
145
197
}
146
198
199
+ /// Trait for common fractional operations.
147
200
pub trait Fractional : Num
148
201
+ Orderable
149
202
+ Round
150
203
+ Div < Self , Self > {
204
+ /// Take the reciprocal (inverse) of a number, `1/x`.
151
205
fn recip ( & self ) -> Self ;
152
206
}
153
207
208
+ /// A collection of algebraic operations.
154
209
pub trait Algebraic {
210
+ /// Raise a number to a power.
155
211
fn pow ( & self , n : & Self ) -> Self ;
212
+ /// Take the squre root of a number.
156
213
fn sqrt ( & self ) -> Self ;
214
+ /// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
157
215
fn rsqrt ( & self ) -> Self ;
216
+ /// Take the cubic root of a number.
158
217
fn cbrt ( & self ) -> Self ;
218
+ /// Calculate the length of the hypotenuse of a right-angle triangle given
219
+ /// legs of length `x` and `y`.
159
220
fn hypot ( & self , other : & Self ) -> Self ;
160
221
}
161
222
@@ -178,16 +239,42 @@ pub trait Algebraic {
178
239
/// `y`.
179
240
#[ inline( always) ] pub fn hypot < T : Algebraic > ( x : T , y : T ) -> T { x. hypot ( & y) }
180
241
242
+ /// A trait for trigonometric functions.
181
243
pub trait Trigonometric {
244
+ /// Computes the sine of a number (in radians).
182
245
fn sin ( & self ) -> Self ;
246
+ /// Computes the cosine of a number (in radians).
183
247
fn cos ( & self ) -> Self ;
248
+ /// Computes the tangent of a number (in radians).
184
249
fn tan ( & self ) -> Self ;
185
250
251
+ /// Computes the arcsine of a number. Return value is in radians in
252
+ /// the range [-pi/2, pi/2] or NaN if the number is outside the range
253
+ /// [-1, 1].
186
254
fn asin ( & self ) -> Self ;
255
+ /// Computes the arccosine of a number. Return value is in radians in
256
+ /// the range [0, pi] or NaN if the number is outside the range
257
+ /// [-1, 1].
187
258
fn acos ( & self ) -> Self ;
259
+ /// Computes the arctangent of a number. Return value is in radians in the
260
+ /// range [-pi/2, pi/2];
188
261
fn atan ( & self ) -> Self ;
189
262
263
+ /// Computes the four quadrant arctangent of a number, `y`, and another
264
+ /// number `x`. Return value is in radians in the range [-pi, pi];
265
+ ///
266
+ /// # Example
267
+ ///
268
+ /// ```rust
269
+ /// let y = 3f32.sqrt();
270
+ /// let x = 1f32;
271
+ /// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32);
272
+ /// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32);
273
+ /// ```
190
274
fn atan2 ( & self , other : & Self ) -> Self ;
275
+
276
+ /// Simultaneously computes the sine and cosine of the number, `x`. Returns
277
+ /// `(sin(x), cos(x))`.
191
278
fn sin_cos ( & self ) -> ( Self , Self ) ;
192
279
}
193
280
@@ -210,13 +297,20 @@ pub trait Trigonometric {
210
297
/// Simultaneously computes the sine and cosine of the number.
211
298
#[ inline( always) ] pub fn sin_cos < T : Trigonometric > ( value : T ) -> ( T , T ) { value. sin_cos ( ) }
212
299
300
+ /// A trait exponential functions.
213
301
pub trait Exponential {
302
+ /// Returns `e^(self)`, (the exponential function).
214
303
fn exp ( & self ) -> Self ;
304
+ /// Returns 2 raised to the power of the number, `2^(self)`.
215
305
fn exp2 ( & self ) -> Self ;
216
306
307
+ /// Returns the natural logarithm of the number.
217
308
fn ln ( & self ) -> Self ;
309
+ /// Returns the logarithm of the number with respect to an arbitrary base.
218
310
fn log ( & self , base : & Self ) -> Self ;
311
+ /// Returns the base 2 logarithm of the number.
219
312
fn log2 ( & self ) -> Self ;
313
+ /// Returns the base 10 logarithm of the number.
220
314
fn log10 ( & self ) -> Self ;
221
315
}
222
316
@@ -234,19 +328,26 @@ pub trait Exponential {
234
328
/// Returns the base 10 logarithm of the number.
235
329
#[ inline( always) ] pub fn log10 < T : Exponential > ( value : T ) -> T { value. log10 ( ) }
236
330
331
+ /// A trait hyperbolic functions.
237
332
pub trait Hyperbolic : Exponential {
333
+ /// Hyperbolic sine function.
238
334
fn sinh ( & self ) -> Self ;
335
+ /// Hyperbolic cosine function.
239
336
fn cosh ( & self ) -> Self ;
337
+ /// Hyperbolic tangent function.
240
338
fn tanh ( & self ) -> Self ;
241
339
340
+ /// Inverse hyperbolic sine function.
242
341
fn asinh ( & self ) -> Self ;
342
+ /// Inverse hyperbolic cosine function.
243
343
fn acosh ( & self ) -> Self ;
344
+ /// Inverse hyperbolic tangent function.
244
345
fn atanh ( & self ) -> Self ;
245
346
}
246
347
247
- /// Hyperbolic cosine function.
248
- #[ inline( always) ] pub fn sinh < T : Hyperbolic > ( value : T ) -> T { value. sinh ( ) }
249
348
/// Hyperbolic sine function.
349
+ #[ inline( always) ] pub fn sinh < T : Hyperbolic > ( value : T ) -> T { value. sinh ( ) }
350
+ /// Hyperbolic cosine function.
250
351
#[ inline( always) ] pub fn cosh < T : Hyperbolic > ( value : T ) -> T { value. cosh ( ) }
251
352
/// Hyperbolic tangent function.
252
353
#[ inline( always) ] pub fn tanh < T : Hyperbolic > ( value : T ) -> T { value. tanh ( ) }
@@ -285,7 +386,10 @@ pub trait Real: Signed
285
386
fn ln_10 ( ) -> Self ;
286
387
287
388
// Angular conversions
389
+
390
+ /// Convert radians to degrees.
288
391
fn to_degrees ( & self ) -> Self ;
392
+ /// Convert degrees to radians.
289
393
fn to_radians ( & self ) -> Self ;
290
394
}
291
395
@@ -315,9 +419,34 @@ pub trait Bitwise: Not<Self>
315
419
+ Shl < Self , Self >
316
420
+ Shr < Self , Self > { }
317
421
422
+ /// A trait for common counting operations on bits.
318
423
pub trait BitCount {
424
+ /// Returns the number of bits set in the number.
425
+ ///
426
+ /// # Example
427
+ ///
428
+ /// ```rust
429
+ /// let n = 0b0101000u16;
430
+ /// assert_eq!(n.population_count(), 2);
431
+ /// ```
319
432
fn population_count ( & self ) -> Self ;
433
+ /// Returns the number of leading zeros in the number.
434
+ ///
435
+ /// # Example
436
+ ///
437
+ /// ```rust
438
+ /// let n = 0b0101000u16;
439
+ /// assert_eq!(n.leading_zeros(), 10);
440
+ /// ```
320
441
fn leading_zeros ( & self ) -> Self ;
442
+ /// Returns the number of trailing zeros in the number.
443
+ ///
444
+ /// # Example
445
+ ///
446
+ /// ```rust
447
+ /// let n = 0b0101000u16;
448
+ /// assert_eq!(n.trailing_zeros(), 3);
449
+ /// ```
321
450
fn trailing_zeros ( & self ) -> Self ;
322
451
}
323
452
0 commit comments