Improve std::num module description, and fix some formatting

This commit is contained in:
Brendan Zabarauskas 2013-07-30 08:59:43 +10:00
parent b6ea0538a9
commit 4f65fc7ef2

View file

@ -8,7 +8,10 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
//! An interface for numeric types //! Numeric traits and functions for generic mathematics.
//!
//! These are implemented for the primitive numeric types in `std::{u8, u16,
//! u32, u64, uint, i8, i16, i32, i64, int, f32, f64, float}`.
#[allow(missing_doc)]; #[allow(missing_doc)];
@ -19,9 +22,7 @@ use option::Option;
pub mod strconv; pub mod strconv;
///
/// The base trait for numeric types /// The base trait for numeric types
///
pub trait Num: Eq + Zero + One pub trait Num: Eq + Zero + One
+ Neg<Self> + Neg<Self>
+ Add<Self,Self> + Add<Self,Self>
@ -188,9 +189,7 @@ pub trait Hyperbolic: Exponential {
#[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() } #[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
#[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() } #[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }
///
/// Defines constants and methods common to real numbers /// Defines constants and methods common to real numbers
///
pub trait Real: Signed pub trait Real: Signed
+ Fractional + Fractional
+ Algebraic + Algebraic
@ -221,9 +220,7 @@ pub trait Real: Signed
fn to_radians(&self) -> Self; fn to_radians(&self) -> Self;
} }
///
/// Methods that are harder to implement and not commonly used. /// Methods that are harder to implement and not commonly used.
///
pub trait RealExt: Real { pub trait RealExt: Real {
// FIXME (#5527): usages of `int` should be replaced with an associated // FIXME (#5527): usages of `int` should be replaced with an associated
// integer type once these are implemented // integer type once these are implemented
@ -241,9 +238,7 @@ pub trait RealExt: Real {
fn yn(&self, n: int) -> Self; fn yn(&self, n: int) -> Self;
} }
///
/// Collects the bitwise operators under one trait. /// Collects the bitwise operators under one trait.
///
pub trait Bitwise: Not<Self> pub trait Bitwise: Not<Self>
+ BitAnd<Self,Self> + BitAnd<Self,Self>
+ BitOr<Self,Self> + BitOr<Self,Self>
@ -263,11 +258,9 @@ pub trait Bounded {
fn max_value() -> Self; fn max_value() -> Self;
} }
///
/// Specifies the available operations common to all of Rust's core numeric primitives. /// Specifies the available operations common to all of Rust's core numeric primitives.
/// These may not always make sense from a purely mathematical point of view, but /// These may not always make sense from a purely mathematical point of view, but
/// may be useful for systems programming. /// may be useful for systems programming.
///
pub trait Primitive: Num pub trait Primitive: Num
+ NumCast + NumCast
+ Bounded + Bounded
@ -282,17 +275,13 @@ pub trait Primitive: Num
fn bytes() -> uint; fn bytes() -> uint;
} }
///
/// A collection of traits relevant to primitive signed and unsigned integers /// A collection of traits relevant to primitive signed and unsigned integers
///
pub trait Int: Integer pub trait Int: Integer
+ Primitive + Primitive
+ Bitwise + Bitwise
+ BitCount {} + BitCount {}
///
/// Used for representing the classification of floating point numbers /// Used for representing the classification of floating point numbers
///
#[deriving(Eq)] #[deriving(Eq)]
pub enum FPCategory { pub enum FPCategory {
/// "Not a Number", often obtained by dividing by zero /// "Not a Number", often obtained by dividing by zero
@ -307,9 +296,7 @@ pub enum FPCategory {
FPNormal, FPNormal,
} }
///
/// Primitive floating point numbers /// Primitive floating point numbers
///
pub trait Float: Real pub trait Float: Real
+ Signed + Signed
+ Primitive + Primitive
@ -343,7 +330,6 @@ pub trait Float: Real
fn next_after(&self, other: Self) -> Self; fn next_after(&self, other: Self) -> Self;
} }
///
#[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() } #[inline(always)] pub fn exp_m1<T: Float>(value: T) -> T { value.exp_m1() }
#[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() } #[inline(always)] pub fn ln_1p<T: Float>(value: T) -> T { value.ln_1p() }
#[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) } #[inline(always)] pub fn mul_add<T: Float>(a: T, b: T, c: T) -> T { a.mul_add(b, c) }
@ -362,9 +348,7 @@ pub fn cast<T:NumCast,U:NumCast>(n: T) -> U {
NumCast::from(n) NumCast::from(n)
} }
///
/// An interface for casting between machine scalars /// An interface for casting between machine scalars
///
pub trait NumCast { pub trait NumCast {
fn from<T:NumCast>(n: T) -> Self; fn from<T:NumCast>(n: T) -> Self;
@ -436,7 +420,6 @@ pub trait FromStrRadix {
pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>; pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
} }
///
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`. /// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
/// ///
/// Returns `radix^pow` as `T`. /// Returns `radix^pow` as `T`.