Skip to content

Commit 11b0784

Browse files
committed
auto merge of rust-lang#10435 : jayanderson/rust/docs, r=huonw
This fills in some missing docs in the nums package. Let me know if this is on the right track for what's wanted for docs. I can probably fill in more in the future. Thanks. (As a side note the precedence of the unary negative operator '-' tripped me up for a bit. Essentially I would expect `-25.0f32.sqrt()` to result in NaN instead of `-5.0`.)
2 parents 3403503 + 8a4be80 commit 11b0784

File tree

1 file changed

+131
-2
lines changed

1 file changed

+131
-2
lines changed

src/libstd/num/mod.rs

Lines changed: 131 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,87 @@ pub trait Integer: Num
136136
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
137137
#[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
138138

139+
/// A collection of rounding operations.
139140
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+
/// ```
140149
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+
/// ```
141159
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+
/// ```
142172
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+
/// ```
143184
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+
/// ```
144196
fn fract(&self) -> Self;
145197
}
146198

199+
/// Trait for common fractional operations.
147200
pub trait Fractional: Num
148201
+ Orderable
149202
+ Round
150203
+ Div<Self,Self> {
204+
/// Take the reciprocal (inverse) of a number, `1/x`.
151205
fn recip(&self) -> Self;
152206
}
153207

208+
/// A collection of algebraic operations.
154209
pub trait Algebraic {
210+
/// Raise a number to a power.
155211
fn pow(&self, n: &Self) -> Self;
212+
/// Take the squre root of a number.
156213
fn sqrt(&self) -> Self;
214+
/// Take the reciprocal (inverse) square root of a number, `1/sqrt(x)`.
157215
fn rsqrt(&self) -> Self;
216+
/// Take the cubic root of a number.
158217
fn cbrt(&self) -> Self;
218+
/// Calculate the length of the hypotenuse of a right-angle triangle given
219+
/// legs of length `x` and `y`.
159220
fn hypot(&self, other: &Self) -> Self;
160221
}
161222

@@ -178,16 +239,42 @@ pub trait Algebraic {
178239
/// `y`.
179240
#[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }
180241

242+
/// A trait for trigonometric functions.
181243
pub trait Trigonometric {
244+
/// Computes the sine of a number (in radians).
182245
fn sin(&self) -> Self;
246+
/// Computes the cosine of a number (in radians).
183247
fn cos(&self) -> Self;
248+
/// Computes the tangent of a number (in radians).
184249
fn tan(&self) -> Self;
185250

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].
186254
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].
187258
fn acos(&self) -> Self;
259+
/// Computes the arctangent of a number. Return value is in radians in the
260+
/// range [-pi/2, pi/2];
188261
fn atan(&self) -> Self;
189262

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+
/// ```
190274
fn atan2(&self, other: &Self) -> Self;
275+
276+
/// Simultaneously computes the sine and cosine of the number, `x`. Returns
277+
/// `(sin(x), cos(x))`.
191278
fn sin_cos(&self) -> (Self, Self);
192279
}
193280

@@ -210,13 +297,20 @@ pub trait Trigonometric {
210297
/// Simultaneously computes the sine and cosine of the number.
211298
#[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }
212299

300+
/// A trait exponential functions.
213301
pub trait Exponential {
302+
/// Returns `e^(self)`, (the exponential function).
214303
fn exp(&self) -> Self;
304+
/// Returns 2 raised to the power of the number, `2^(self)`.
215305
fn exp2(&self) -> Self;
216306

307+
/// Returns the natural logarithm of the number.
217308
fn ln(&self) -> Self;
309+
/// Returns the logarithm of the number with respect to an arbitrary base.
218310
fn log(&self, base: &Self) -> Self;
311+
/// Returns the base 2 logarithm of the number.
219312
fn log2(&self) -> Self;
313+
/// Returns the base 10 logarithm of the number.
220314
fn log10(&self) -> Self;
221315
}
222316

@@ -234,19 +328,26 @@ pub trait Exponential {
234328
/// Returns the base 10 logarithm of the number.
235329
#[inline(always)] pub fn log10<T: Exponential>(value: T) -> T { value.log10() }
236330

331+
/// A trait hyperbolic functions.
237332
pub trait Hyperbolic: Exponential {
333+
/// Hyperbolic sine function.
238334
fn sinh(&self) -> Self;
335+
/// Hyperbolic cosine function.
239336
fn cosh(&self) -> Self;
337+
/// Hyperbolic tangent function.
240338
fn tanh(&self) -> Self;
241339

340+
/// Inverse hyperbolic sine function.
242341
fn asinh(&self) -> Self;
342+
/// Inverse hyperbolic cosine function.
243343
fn acosh(&self) -> Self;
344+
/// Inverse hyperbolic tangent function.
244345
fn atanh(&self) -> Self;
245346
}
246347

247-
/// Hyperbolic cosine function.
248-
#[inline(always)] pub fn sinh<T: Hyperbolic>(value: T) -> T { value.sinh() }
249348
/// Hyperbolic sine function.
349+
#[inline(always)] pub fn sinh<T: Hyperbolic>(value: T) -> T { value.sinh() }
350+
/// Hyperbolic cosine function.
250351
#[inline(always)] pub fn cosh<T: Hyperbolic>(value: T) -> T { value.cosh() }
251352
/// Hyperbolic tangent function.
252353
#[inline(always)] pub fn tanh<T: Hyperbolic>(value: T) -> T { value.tanh() }
@@ -285,7 +386,10 @@ pub trait Real: Signed
285386
fn ln_10() -> Self;
286387

287388
// Angular conversions
389+
390+
/// Convert radians to degrees.
288391
fn to_degrees(&self) -> Self;
392+
/// Convert degrees to radians.
289393
fn to_radians(&self) -> Self;
290394
}
291395

@@ -315,9 +419,34 @@ pub trait Bitwise: Not<Self>
315419
+ Shl<Self,Self>
316420
+ Shr<Self,Self> {}
317421

422+
/// A trait for common counting operations on bits.
318423
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+
/// ```
319432
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+
/// ```
320441
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+
/// ```
321450
fn trailing_zeros(&self) -> Self;
322451
}
323452

0 commit comments

Comments
 (0)