Add _int.pow
This commit is contained in:
parent
ac72f42b1c
commit
922f69387d
@ -34,6 +34,23 @@ fn to_str(mutable int n, uint radix) -> str
|
||||
}
|
||||
}
|
||||
|
||||
fn pow(int base, uint exponent) -> int {
|
||||
|
||||
if (exponent == 0u) {
|
||||
ret 1;
|
||||
} else if (base == 0) {
|
||||
ret 0;
|
||||
} else {
|
||||
auto accum = base;
|
||||
auto count = exponent;
|
||||
while (count > 1u) {
|
||||
accum *= base;
|
||||
count -= 1u;
|
||||
}
|
||||
ret accum;
|
||||
}
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust;
|
||||
// fill-column: 78;
|
||||
|
@ -11,6 +11,17 @@ fn test_to_str() {
|
||||
check (eq(_int.to_str(100, 10u), "100"));
|
||||
}
|
||||
|
||||
fn test_pow() {
|
||||
check (_int.pow(0, 0u) == 1);
|
||||
check (_int.pow(0, 1u) == 0);
|
||||
check (_int.pow(0, 2u) == 0);
|
||||
check (_int.pow(-1, 0u) == -1);
|
||||
check (_int.pow(1, 0u) == 1);
|
||||
check (_int.pow(-3, 2u) == 9);
|
||||
check (_int.pow(-3, 3u) == -27);
|
||||
check (_int.pow(4, 9u) == 262144);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_to_str();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user