Skip to content

Add some missing method wrappers to std::num #8115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions src/libstd/num/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
// option. This file may not be copied, modified, or distributed
// 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)];

Expand All @@ -19,9 +22,7 @@ use option::Option;

pub mod strconv;

///
/// The base trait for numeric types
///
pub trait Num: Eq + Zero + One
+ Neg<Self>
+ Add<Self,Self>
Expand All @@ -45,18 +46,23 @@ pub trait Orderable: Ord {
fn clamp(&self, mn: &Self, mx: &Self) -> Self;
}

#[inline(always)] pub fn min<T: Orderable>(a: T, b: T) -> T { a.min(&b) }
#[inline(always)] pub fn max<T: Orderable>(a: T, b: T) -> T { a.max(&b) }
#[inline(always)] pub fn min<T: Orderable>(x: T, y: T) -> T { x.min(&y) }
#[inline(always)] pub fn max<T: Orderable>(x: T, y: T) -> T { x.max(&y) }
#[inline(always)] pub fn clamp<T: Orderable>(value: T, mn: T, mx: T) -> T { value.clamp(&mn, &mx) }

pub trait Zero {
fn zero() -> Self; // FIXME (#5527): This should be an associated constant
fn is_zero(&self) -> bool;
}

#[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }

pub trait One {
fn one() -> Self; // FIXME (#5527): This should be an associated constant
}

#[inline(always)] pub fn one<T: One>() -> T { One::one() }

pub trait Signed: Num
+ Neg<Self> {
fn abs(&self) -> Self;
Expand All @@ -68,6 +74,7 @@ pub trait Signed: Num
}

#[inline(always)] pub fn abs<T: Signed>(value: T) -> T { value.abs() }
#[inline(always)] pub fn abs_sub<T: Signed>(x: T, y: T) -> T { x.abs_sub(&y) }
#[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }

pub trait Unsigned: Num {}
Expand All @@ -90,6 +97,9 @@ pub trait Integer: Num
fn is_odd(&self) -> bool;
}

#[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
#[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }

pub trait Round {
fn floor(&self) -> Self;
fn ceil(&self) -> Self;
Expand All @@ -113,15 +123,21 @@ pub trait Algebraic {
fn hypot(&self, other: &Self) -> Self;
}

#[inline(always)] pub fn pow<T: Algebraic>(value: T, n: T) -> T { value.pow(&n) }
#[inline(always)] pub fn sqrt<T: Algebraic>(value: T) -> T { value.sqrt() }
#[inline(always)] pub fn rsqrt<T: Algebraic>(value: T) -> T { value.rsqrt() }
#[inline(always)] pub fn cbrt<T: Algebraic>(value: T) -> T { value.cbrt() }
#[inline(always)] pub fn hypot<T: Algebraic>(x: T, y: T) -> T { x.hypot(&y) }

pub trait Trigonometric {
fn sin(&self) -> Self;
fn cos(&self) -> Self;
fn tan(&self) -> Self;

fn asin(&self) -> Self;
fn acos(&self) -> Self;
fn atan(&self) -> Self;

fn atan2(&self, other: &Self) -> Self;
fn sin_cos(&self) -> (Self, Self);
}
Expand All @@ -135,10 +151,12 @@ pub trait Trigonometric {
#[inline(always)] pub fn atan<T: Trigonometric>(value: T) -> T { value.atan() }

#[inline(always)] pub fn atan2<T: Trigonometric>(x: T, y: T) -> T { x.atan2(&y) }
#[inline(always)] pub fn sin_cos<T: Trigonometric>(value: T) -> (T, T) { value.sin_cos() }

pub trait Exponential {
fn exp(&self) -> Self;
fn exp2(&self) -> Self;

fn ln(&self) -> Self;
fn log(&self, base: &Self) -> Self;
fn log2(&self) -> Self;
Expand All @@ -157,6 +175,7 @@ pub trait Hyperbolic: Exponential {
fn sinh(&self) -> Self;
fn cosh(&self) -> Self;
fn tanh(&self) -> Self;

fn asinh(&self) -> Self;
fn acosh(&self) -> Self;
fn atanh(&self) -> Self;
Expand All @@ -170,9 +189,7 @@ pub trait Hyperbolic: Exponential {
#[inline(always)] pub fn acosh<T: Hyperbolic>(value: T) -> T { value.acosh() }
#[inline(always)] pub fn atanh<T: Hyperbolic>(value: T) -> T { value.atanh() }

///
/// Defines constants and methods common to real numbers
///
pub trait Real: Signed
+ Fractional
+ Algebraic
Expand Down Expand Up @@ -203,9 +220,7 @@ pub trait Real: Signed
fn to_radians(&self) -> Self;
}

///
/// Methods that are harder to implement and not commonly used.
///
pub trait RealExt: Real {
// FIXME (#5527): usages of `int` should be replaced with an associated
// integer type once these are implemented
Expand All @@ -223,9 +238,7 @@ pub trait RealExt: Real {
fn yn(&self, n: int) -> Self;
}

///
/// Collects the bitwise operators under one trait.
///
pub trait Bitwise: Not<Self>
+ BitAnd<Self,Self>
+ BitOr<Self,Self>
Expand All @@ -245,11 +258,9 @@ pub trait Bounded {
fn max_value() -> Self;
}

///
/// 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
/// may be useful for systems programming.
///
pub trait Primitive: Num
+ NumCast
+ Bounded
Expand All @@ -264,17 +275,13 @@ pub trait Primitive: Num
fn bytes() -> uint;
}

///
/// A collection of traits relevant to primitive signed and unsigned integers
///
pub trait Int: Integer
+ Primitive
+ Bitwise
+ BitCount {}

///
/// Used for representing the classification of floating point numbers
///
#[deriving(Eq)]
pub enum FPCategory {
/// "Not a Number", often obtained by dividing by zero
Expand All @@ -289,9 +296,7 @@ pub enum FPCategory {
FPNormal,
}

///
/// Primitive floating point numbers
///
pub trait Float: Real
+ Signed
+ Primitive
Expand Down Expand Up @@ -325,7 +330,10 @@ pub trait Float: Real
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 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) }

/// Cast from one machine scalar to another
///
/// # Example
Expand All @@ -340,9 +348,7 @@ pub fn cast<T:NumCast,U:NumCast>(n: T) -> U {
NumCast::from(n)
}

///
/// An interface for casting between machine scalars
///
pub trait NumCast {
fn from<T:NumCast>(n: T) -> Self;

Expand Down Expand Up @@ -414,7 +420,6 @@ pub trait FromStrRadix {
pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
}

///
/// Calculates a power to a given radix, optimized for uint `pow` and `radix`.
///
/// Returns `radix^pow` as `T`.
Expand Down