8 The maximum value of an integer
10 fn max_value() -> int {
17 The minumum value of an integer
19 fn min_value() -> int {
20 ret (-1 << (sys::size_of::<int>() * 8u as int - 1)) as int;
24 pure fn add(x: int, y: int) -> int { ret x + y; }
27 pure fn sub(x: int, y: int) -> int { ret x - y; }
30 pure fn mul(x: int, y: int) -> int { ret x * y; }
33 pure fn div(x: int, y: int) -> int { ret x / y; }
36 pure fn rem(x: int, y: int) -> int { ret x % y; }
39 pure fn lt(x: int, y: int) -> bool { ret x < y; }
42 pure fn le(x: int, y: int) -> bool { ret x <= y; }
45 pure fn eq(x: int, y: int) -> bool { ret x == y; }
48 pure fn ne(x: int, y: int) -> bool { ret x != y; }
51 pure fn ge(x: int, y: int) -> bool { ret x >= y; }
54 pure fn gt(x: int, y: int) -> bool { ret x > y; }
56 /* Predicate: positive */
57 pure fn positive(x: int) -> bool { ret x > 0; }
59 /* Predicate: negative */
60 pure fn negative(x: int) -> bool { ret x < 0; }
62 /* Predicate: nonpositive */
63 pure fn nonpositive(x: int) -> bool { ret x <= 0; }
65 /* Predicate: nonnegative */
66 pure fn nonnegative(x: int) -> bool { ret x >= 0; }
69 // FIXME: Make sure this works with negative integers.
73 Produce a uint suitable for use in a hash table
75 fn hash(x: int) -> uint { ret x as uint; }
77 // FIXME: This is redundant
78 fn eq_alias(x: int, y: int) -> bool { ret x == y; }
83 Iterate over the range [`lo`..`hi`)
85 fn range(lo: int, hi: int, it: block(int)) {
86 while lo < hi { it(lo); lo += 1; }
92 Parse a buffer of bytes
97 radix - The base of the number
101 buf must not be empty
103 fn parse_buf(buf: [u8], radix: uint) -> int {
104 if vec::len::<u8>(buf) == 0u {
105 log_err "parse_buf(): buf is empty";
108 let i = vec::len::<u8>(buf) - 1u;
110 if buf[0] == ('-' as u8) {
116 n += (buf[i] - ('0' as u8) as int) * power;
117 power *= radix as int;
118 if i == 0u { ret n; }
127 Parse a string to an int
133 fn from_str(s: str) -> int { parse_buf(str::bytes(s), 10u) }
138 Convert to a string in a given base
140 fn to_str(n: int, radix: uint) -> str {
141 assert (0u < radix && radix <= 16u);
143 "-" + uint::to_str(-n as uint, radix)
144 } else { uint::to_str(n as uint, radix) };
152 fn str(i: int) -> str { ret to_str(i, 10u); }
157 Returns `base` raised to the power of `exponent`
159 fn pow(base: int, exponent: uint) -> int {
160 if exponent == 0u { ret 1; } //Not mathemtically true if [base == 0]
161 if base == 0 { ret 0; }
162 let my_pow = exponent;
164 let multiplier = base;
166 if my_pow % 2u == 1u {
170 multiplier *= multiplier;
177 // indent-tabs-mode: nil
179 // buffer-file-coding-system: utf-8-unix
180 // compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";