// Copyright 2012 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 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Operations and constants for `int` use num::NumCast; pub use self::inst::pow; mod inst { pub type T = int; pub static bits: uint = ::uint::bits; /// Returns `base` raised to the power of `exponent` pub fn pow(base: int, exponent: uint) -> int { if exponent == 0u { //Not mathemtically true if ~[base == 0] return 1; } if base == 0 { return 0; } let mut my_pow = exponent; let mut acc = 1; let mut multiplier = base; while(my_pow > 0u) { if my_pow % 2u == 1u { acc *= multiplier; } my_pow /= 2u; multiplier *= multiplier; } return acc; } #[test] fn test_pow() { assert!((pow(0, 0u) == 1)); assert!((pow(0, 1u) == 0)); assert!((pow(0, 2u) == 0)); assert!((pow(-1, 0u) == 1)); assert!((pow(1, 0u) == 1)); assert!((pow(-3, 2u) == 9)); assert!((pow(-3, 3u) == -27)); assert!((pow(4, 9u) == 262144)); } #[test] fn test_overflows() { assert!((::int::max_value > 0)); assert!((::int::min_value <= 0)); assert!((::int::min_value + ::int::max_value + 1 == 0)); } } impl NumCast for int { /** * Cast `n` to a `int` */ #[inline(always)] fn from(n: N) -> int { n.to_int() } #[inline(always)] fn to_u8(&self) -> u8 { *self as u8 } #[inline(always)] fn to_u16(&self) -> u16 { *self as u16 } #[inline(always)] fn to_u32(&self) -> u32 { *self as u32 } #[inline(always)] fn to_u64(&self) -> u64 { *self as u64 } #[inline(always)] fn to_uint(&self) -> uint { *self as uint } #[inline(always)] fn to_i8(&self) -> i8 { *self as i8 } #[inline(always)] fn to_i16(&self) -> i16 { *self as i16 } #[inline(always)] fn to_i32(&self) -> i32 { *self as i32 } #[inline(always)] fn to_i64(&self) -> i64 { *self as i64 } #[inline(always)] fn to_int(&self) -> int { *self } #[inline(always)] fn to_f32(&self) -> f32 { *self as f32 } #[inline(always)] fn to_f64(&self) -> f64 { *self as f64 } #[inline(always)] fn to_float(&self) -> float { *self as float } } #[test] fn test_numcast() { assert!((20u == 20i.to_uint())); assert!((20u8 == 20i.to_u8())); assert!((20u16 == 20i.to_u16())); assert!((20u32 == 20i.to_u32())); assert!((20u64 == 20i.to_u64())); assert!((20i == 20i.to_int())); assert!((20i8 == 20i.to_i8())); assert!((20i16 == 20i.to_i16())); assert!((20i32 == 20i.to_i32())); assert!((20i64 == 20i.to_i64())); assert!((20f == 20i.to_float())); assert!((20f32 == 20i.to_f32())); assert!((20f64 == 20i.to_f64())); assert!((20i == NumCast::from(20u))); assert!((20i == NumCast::from(20u8))); assert!((20i == NumCast::from(20u16))); assert!((20i == NumCast::from(20u32))); assert!((20i == NumCast::from(20u64))); assert!((20i == NumCast::from(20i))); assert!((20i == NumCast::from(20i8))); assert!((20i == NumCast::from(20i16))); assert!((20i == NumCast::from(20i32))); assert!((20i == NumCast::from(20i64))); assert!((20i == NumCast::from(20f))); assert!((20i == NumCast::from(20f32))); assert!((20i == NumCast::from(20f64))); assert!((20i == num::cast(20u))); assert!((20i == num::cast(20u8))); assert!((20i == num::cast(20u16))); assert!((20i == num::cast(20u32))); assert!((20i == num::cast(20u64))); assert!((20i == num::cast(20i))); assert!((20i == num::cast(20i8))); assert!((20i == num::cast(20i16))); assert!((20i == num::cast(20i32))); assert!((20i == num::cast(20i64))); assert!((20i == num::cast(20f))); assert!((20i == num::cast(20f32))); assert!((20i == num::cast(20f64))); }