2013-02-14 20:29:36 -06:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 18:48:01 -06:00
|
|
|
// 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.
|
|
|
|
|
2012-09-19 18:52:32 -05:00
|
|
|
//! An interface for numeric types
|
2013-03-26 15:38:07 -05:00
|
|
|
use cmp::Ord;
|
|
|
|
use ops::{Div, Mul, Neg};
|
|
|
|
use option::Option;
|
2013-01-26 20:13:33 -06:00
|
|
|
use kinds::Copy;
|
2012-06-07 19:25:54 -05:00
|
|
|
|
2013-02-14 20:29:36 -06:00
|
|
|
pub mod strconv;
|
|
|
|
|
2013-01-25 18:57:39 -06:00
|
|
|
pub trait IntConvertible {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_int(&self) -> int;
|
|
|
|
fn from_int(n: int) -> Self;
|
2013-01-25 18:57:39 -06:00
|
|
|
}
|
|
|
|
|
2012-12-20 09:14:38 -06:00
|
|
|
pub trait Zero {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn zero() -> Self;
|
2012-12-20 09:14:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait One {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn one() -> Self;
|
2012-12-20 09:14:38 -06:00
|
|
|
}
|
2013-01-26 20:05:20 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
pub 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-03-21 23:20:48 -05:00
|
|
|
fn round(&self, mode: RoundMode) -> Self;
|
2013-01-26 20:05:20 -06:00
|
|
|
|
2013-03-21 23:20:48 -05:00
|
|
|
fn floor(&self) -> Self;
|
|
|
|
fn ceil(&self) -> Self;
|
|
|
|
fn fract(&self) -> Self;
|
2013-01-26 20:05:20 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 22:46:07 -06:00
|
|
|
pub enum RoundMode {
|
|
|
|
RoundDown,
|
|
|
|
RoundUp,
|
|
|
|
RoundToZero,
|
|
|
|
RoundFromZero
|
|
|
|
}
|
|
|
|
|
2013-02-10 19:33:05 -06:00
|
|
|
/**
|
|
|
|
* Cast a number the the enclosing type
|
|
|
|
*
|
|
|
|
* # Example
|
|
|
|
*
|
|
|
|
* ~~~
|
|
|
|
* let twenty: f32 = num::cast(0x14);
|
2013-03-06 15:58:02 -06:00
|
|
|
* fail_unless!(twenty == 20f32);
|
2013-02-10 19:33:05 -06:00
|
|
|
* ~~~
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn cast<T:NumCast,U:NumCast>(n: T) -> U {
|
2013-02-10 19:33:05 -06:00
|
|
|
NumCast::from(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An interface for generic numeric type casts
|
|
|
|
*/
|
|
|
|
pub trait NumCast {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from<T:NumCast>(n: T) -> Self;
|
|
|
|
|
|
|
|
fn to_u8(&self) -> u8;
|
|
|
|
fn to_u16(&self) -> u16;
|
|
|
|
fn to_u32(&self) -> u32;
|
|
|
|
fn to_u64(&self) -> u64;
|
|
|
|
fn to_uint(&self) -> uint;
|
|
|
|
|
|
|
|
fn to_i8(&self) -> i8;
|
|
|
|
fn to_i16(&self) -> i16;
|
|
|
|
fn to_i32(&self) -> i32;
|
|
|
|
fn to_i64(&self) -> i64;
|
|
|
|
fn to_int(&self) -> int;
|
|
|
|
|
|
|
|
fn to_f32(&self) -> f32;
|
|
|
|
fn to_f64(&self) -> f64;
|
|
|
|
fn to_float(&self) -> float;
|
2013-02-10 19:33:05 -06:00
|
|
|
}
|
|
|
|
|
2013-01-24 14:09:58 -06:00
|
|
|
pub trait ToStrRadix {
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_radix(&self, radix: uint) -> ~str;
|
2013-01-24 14:09:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FromStrRadix {
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn from_str_radix(str: &str, radix: uint) -> Option<Self>;
|
2013-01-20 17:40:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generic math functions:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2013-01-26 20:13:33 -06:00
|
|
|
* undefined number. The reason for this is twofold:
|
2013-01-20 17:40:02 -06:00
|
|
|
* - 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`.
|
2013-01-26 20:13:33 -06:00
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(
|
2013-02-12 19:07:26 -06:00
|
|
|
radix: uint, pow: uint) -> T {
|
2013-01-20 17:40:02 -06:00
|
|
|
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;
|
2013-02-10 19:33:05 -06:00
|
|
|
let mut multiplier = cast(radix as int);
|
2013-01-20 17:40:02 -06:00
|
|
|
while (my_pow > 0u) {
|
|
|
|
if my_pow % 2u == 1u {
|
|
|
|
total *= multiplier;
|
|
|
|
}
|
|
|
|
my_pow /= 2u;
|
|
|
|
multiplier *= multiplier;
|
|
|
|
}
|
|
|
|
total
|
2013-01-26 20:13:33 -06:00
|
|
|
}
|
|
|
|
|