aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian Anderson <[email protected]>2011-03-13 18:29:10 -0400
committerGraydon Hoare <[email protected]>2011-03-14 15:52:48 -0700
commit922f69387dab9f751ab110055d5b9638dbae6a78 (patch)
tree699fef780ab5b74a092b9a0e009e683189771a15 /src
parentMention test/bench in README (diff)
downloadrust-922f69387dab9f751ab110055d5b9638dbae6a78.tar.xz
rust-922f69387dab9f751ab110055d5b9638dbae6a78.zip
Add _int.pow
Diffstat (limited to 'src')
-rw-r--r--src/lib/_int.rs17
-rw-r--r--src/test/run-pass/lib-int.rs11
2 files changed, 28 insertions, 0 deletions
diff --git a/src/lib/_int.rs b/src/lib/_int.rs
index ee660f01..ef1b3b66 100644
--- a/src/lib/_int.rs
+++ b/src/lib/_int.rs
@@ -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;
diff --git a/src/test/run-pass/lib-int.rs b/src/test/run-pass/lib-int.rs
index 153c3683..2e85abf6 100644
--- a/src/test/run-pass/lib-int.rs
+++ b/src/test/run-pass/lib-int.rs
@@ -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();
}