1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
// 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 <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.
//! 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:NumCast>(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)));
}
|