rust/src/libcore/num/num.rs

191 lines
4.8 KiB
Rust
Raw Normal View History

// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! An interface for numeric types
use core::cmp::{Ord, Eq};
use ops::{Add, Div, Modulo, Mul, Neg, Sub};
use option::{None, Option, Some};
use char;
use str;
use kinds::Copy;
use vec;
2012-06-07 19:25:54 -05:00
pub mod strconv;
pub trait IntConvertible {
pure fn to_int(&self) -> int;
static pure fn from_int(n: int) -> Self;
}
pub trait Zero {
static pure fn zero() -> Self;
}
pub trait One {
static pure fn one() -> Self;
}
2013-01-26 20:05:20 -06:00
pub pure fn abs<T:Ord + Zero + Neg<T>>(v: T) -> T {
2013-01-15 19:30:16 -06:00
if v < Zero::zero() { v.neg() } else { v }
}
2013-01-26 20:05:20 -06:00
pub trait Round {
2013-02-03 07:13:06 -06:00
pure fn round(&self, mode: RoundMode) -> Self;
2013-01-26 20:05:20 -06:00
2013-02-03 07:13:06 -06:00
pure fn floor(&self) -> Self;
pure fn ceil(&self) -> Self;
pure fn fract(&self) -> Self;
2013-01-26 20:05:20 -06:00
}
/**
* Cast a number the the enclosing type
*
* # Example
*
* ~~~
* let twenty: f32 = num::cast(0x14);
* assert twenty == 20f32;
* ~~~
*/
#[inline(always)]
pub pure fn cast<T:NumCast, U:NumCast>(n: T) -> U {
NumCast::from(n)
}
/**
* An interface for generic numeric type casts
*/
pub trait NumCast {
static pure fn from<T:NumCast>(n: T) -> Self;
pure fn to_u8(&self) -> u8;
pure fn to_u16(&self) -> u16;
pure fn to_u32(&self) -> u32;
pure fn to_u64(&self) -> u64;
pure fn to_uint(&self) -> uint;
pure fn to_i8(&self) -> i8;
pure fn to_i16(&self) -> i16;
pure fn to_i32(&self) -> i32;
pure fn to_i64(&self) -> i64;
pure fn to_int(&self) -> int;
pure fn to_f32(&self) -> f32;
pure fn to_f64(&self) -> f64;
pure fn to_float(&self) -> float;
}
2013-01-26 20:05:20 -06:00
pub enum RoundMode {
RoundDown,
RoundUp,
RoundToZero,
RoundFromZero
}
pub trait ToStrRadix {
pub pure fn to_str_radix(&self, radix: uint) -> ~str;
}
pub trait FromStrRadix {
2013-02-03 07:13:06 -06:00
static pub pure fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
}
// Generic math functions:
/// Dynamically calculates the value `inf` (`1/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn infinity<T:One+Zero+Div<T,T>>() -> T {
let _0: T = Zero::zero();
let _1: T = One::one();
_1 / _0
}
/// Dynamically calculates the value `-inf` (`-1/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn neg_infinity<T:One+Zero+Div<T,T>+Neg<T>>() -> T {
let _0: T = Zero::zero();
let _1: T = One::one();
- _1 / _0
}
/// Dynamically calculates the value `NaN` (`0/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn NaN<T:Zero+Div<T,T>>() -> T {
let _0: T = Zero::zero();
_0 / _0
}
/// Returns `true` if `num` has the value `inf` (`1/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn is_infinity<T:One+Zero+Eq+Div<T,T>>(num: &T) -> bool {
(*num) == (infinity::<T>())
}
/// Returns `true` if `num` has the value `-inf` (`-1/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn is_neg_infinity<T:One+Zero+Eq+Div<T,T>+Neg<T>>(num: &T)
-> bool {
(*num) == (neg_infinity::<T>())
}
/// Returns `true` if `num` has the value `NaN` (is not equal to itself).
#[inline(always)]
pub pure fn is_NaN<T:Eq>(num: &T) -> bool {
(*num) != (*num)
}
/// Returns `true` if `num` has the value `-0` (`1/num == -1/0`).
/// Can fail on integer types.
#[inline(always)]
pub pure fn is_neg_zero<T:One+Zero+Eq+Div<T,T>+Neg<T>>(num: &T) -> bool {
let _1: T = One::one();
let _0: T = Zero::zero();
*num == _0 && is_neg_infinity(&(_1 / *num))
}
/**
* Calculates a power to a given radix, optimized for uint `pow` and `radix`.
*
* Returns `radix^pow` as `T`.
*
* Note:
* Also returns `1` for `0^0`, despite that technically being an
* undefined number. The reason for this is twofold:
* - If code written to use this function cares about that special case, it's
* probably going to catch it before making the call.
* - If code written to use this function doesn't care about it, it's
* probably assuming that `x^0` always equals `1`.
*/
pub pure fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
radix: uint, pow: uint) -> T {
let _0: T = Zero::zero();
let _1: T = One::one();
if pow == 0u { return _1; }
if radix == 0u { return _0; }
let mut my_pow = pow;
let mut total = _1;
let mut multiplier = cast(radix as int);
while (my_pow > 0u) {
if my_pow % 2u == 1u {
total *= multiplier;
}
my_pow /= 2u;
multiplier *= multiplier;
}
total
}