immutable class INT < $NUMBER{INT}, $HASH, $FMT
****
The most fundamental integer class. Signed, unsigned, and modular versions of arithmetic operations are provided. The names of unsigned operations begin with the letter "u". The names of modular operations begin with the letter "m". Negative numbers are represented using 2's complement. INT objects inherit from AVAL{BOOL}. The number of bits in the representation is `asize' and is implementation dependent. It must be at least 32, however (to ensure portability of INT literals up to this size). Many of the operations are specified to raise exceptions on overflow. They are guaranteed to do this if checking is enabled, but this may affect performance. Certain machines (with appropriate hardware) may perform these checks even when checking is not enabled. The modular operations are performed modulo 2^asize.
_
References: Keith O. Geddes, Stephen R. Czapor, and George Labahn, "Algorithms for Computer Algebra", Kluwer Academic Publishers, Boston, 1992.


Flattened version is here

Ancestors
$FMT $HASH $IS_EQ $NUMBER{_}
$STR $NIL $IS_NIL $IS_LT{_}



Public


Constants
const asize:INT:=32;
**** Size in bits. This should be overridden in the compiler file MACROS if not 32.
const zero:INT := 0;
**** See $NUMBER.

Features
abs:SAME
**** The absolute value of self. Built-in.
aget(i:INT):BOOL
aset(i:INT,j:BOOL):INT
at_least(x:SAME):SAME
**** Same as `max(x)'.
at_most(x:SAME):SAME
**** Same as `min(x)'.
band(i:SAME):SAME
**** The bitwise and of self and `i'.
bcount:INT
**** The number of bits in self which are set to 1.
beqv(i:SAME):SAME
**** The bits of res are 1 in positions where self and `i' have the same bit values.
binary_str:STR
**** The binary representation of self of the form "0b100100". Self is interpreted as an unsigned integer.
bit(i:INT):BOOL
**** True if bit `i' of self is 1.
bnand(i:SAME):SAME
**** The bitwise nand of self and `i'.
bnor(i:SAME):SAME
**** The bitwise nor of self and `i'.
bnot:SAME
**** The bitwise complement of self.
bool:BOOL
**** A boolean from self. Built-in.
boole(i:SAME, rule:INT):SAME
**** The bits of res are combinations of the corresponding bits of self and `i' combined according to a rule specified by `rule'. This must be a value between 0 and 15. The low order bit says what to map a 0 in self and a 0 in `i' to, the second bit of `rule' says what to map 0,1 to, the third bit defines 1,0 and the fourth 1,1.
bor(i:SAME):SAME
**** The bitwise inclusive or of self and `i'.
bxor(i:SAME):SAME
**** The bitwise exclusive or of self and `i'.
char:CHAR
**** A character corresponding to the value of self. Built-in.
create(f:FLT):SAME
create(f:FLTD):SAME
create(x:INT):SAME
create(x:INTI):SAME
create(s:STR): SAME
cube:SAME
**** The cube of self.
digit_char:CHAR
**** A character representing self. If self is between 0 and 9, it returns a digit. If between 10 and 15, returns 'A' thru 'F'.
div(i:SAME):SAME
**** The signed quotient of self and `i'. This and `mod' have the property that for non-zero `y', `x=x.div(y)*x + x.mod(y)'. Raises an exception when `i' is 0, when enabled. Built-in.
evenly_divides(i:SAME):BOOL
**** True if self evenly divides `i'.
exp10:SAME
**** Ten to the self power. Small values use lookup table for speed.
exp2:SAME
extended_gcd(i:SAME, out self_factor,out i_factor: SAME): SAME
**** The three parts of the return value `g', `g1', and `g2' are such that `g' is the greatest common divisor of self and `i' and `g1*self+g2*i=g'. Uses the extended Euclidean algorithm. Geddes, et. al. p. 36.
factorial:SAME
**** The factorial of self. Replace by faster algorithm.
flt:FLT
**** A floating point version of self. It is an error if the value cannot be held in a FLT. Built-in.
fltd:FLTD
**** A double floating point version of self. It is an error if the value cannot be held in a FLTD. Built-in.
fmt( f: STR ): STR
from_int(i:INT):SAME
**** Returns `i'.
gcd(i:SAME):SAME
**** The greatest common divisor of self and `i'. The result is non-negative and `x.gcd(0)=x.abs'. Uses Euclidean algorithm. Geddes, et. al. p. 34.
hash:INT
hex_str:STR
**** The hexadecimal representation of self of the form "0x5A". Self is interpreted as an unsigned integer.
high_bits(i:INT):INT
**** The high `i' bits of self with 0 in the lower positions.
highest_bit:INT
**** The position of the highest non-zero bit of self. -1 if none.
int:INT
**** An integer version of self.
inti:INTI
**** An infinite precision version of self.
is_bet(l,u:SAME):BOOL
**** True if self between l and u. Built-in.
is_between(l,u:SAME):BOOL
**** True if self between l and u. Built-in.
is_eq(i:SAME):BOOL
**** True if self and `i' represent the same value. return self=i
is_eq(i1,i2:SAME):BOOL
**** True if self equals `i1' and `i2'.
is_eq(i1,i2,i3:SAME):BOOL
**** True if self equals `i1', `i2', and `i3'.
is_even:BOOL
is_exp2:BOOL
**** returns true iff self is positive and a power of two
is_geq(i:SAME):BOOL
**** True if self is greater than or equal to `i' as signed integers. return is_gt(i) or is_eq(i)
is_gt(i:SAME):BOOL
**** True if self is greater than `i' as signed integers. return i.is_lt(self)
is_leq(i:SAME):BOOL
**** True if self is less than or equal to `i' as signed integers. return is_lt(i) or is_eq(i)
is_lt(i:SAME):BOOL
**** True if self is less than `i' as signed integers. Built-in.
is_neg:BOOL
**** True if self is less than zero.
is_neq(i:SAME):BOOL
**** True if self and `i' represent different values.
is_nil:BOOL
is_non_neg:BOOL
**** True if self is non-negative.
is_non_pos:BOOL
**** True if self is non-positive.
is_non_zero:BOOL
**** True if self is non-zero.
is_odd:BOOL
is_pos:BOOL
**** True if self is greater than zero.
is_pow_of_2:BOOL is return is_exp2 end;
is_prime:BOOL
**** True if self is a prime number. Replace by a faster algorithm.
is_relatively_prime_to(i:SAME):BOOL
**** True if self is relatively prime to `i'.
is_ugeq(i:SAME):BOOL
**** True if self is greater than or equal to `i' as unsigned integers.
is_ugt(i:SAME):BOOL
**** True if self is greater than `i' as unsigned integers.
is_uleq(i:SAME):BOOL
**** True if self is less than or equal to `i' as unsigned integers.
is_ult(lhs:SAME):BOOL
is_within(tolerance,val:SAME):BOOL
is_zero:BOOL
**** True if self is zero.
lcm(i:SAME):SAME
**** The least common multiple of self and `i'. Geddes, et. al. p. 28.
log2:INT
low_bits(i:INT):INT
**** The low `i' bits of self with 0 in the higher positions.
lowest_bit:INT
**** The position of the lowest non-zero bit of self. -1 if none.
lrotate(i:INT):SAME
**** Left rotate the bits of self by `i' places.
lshift(i:INT):SAME
**** The bits of self shifted left by `i' places with zeroes shifted in on the right.
max(i:SAME):SAME
**** The larger of self and `i' as signed integers. Built-in.
maxint:INT
maxval:INT
**** See $NUMBER.
mdiv(i:SAME):SAME
**** The unsigned quotient of self and `i'. Raises an exception when `i' is 0, when enabled. Built-in.
min(i:SAME):SAME
**** The smaller of self and `i' as signed integers. Built-in.
minint:INT
minus(i:SAME):SAME
**** The signed difference between self and `i'. Raises an exception on overflow, when enabled. Built-in.
minval:INT
**** See $NUMBER.
mminus(i:SAME):SAME
**** The difference between self and `i' modulo 2^asize. Never raises an exception. Built-in.
mmod(i:SAME):SAME
**** Unsigned remainder of self divided by `i'. Raises an exception when `i' is 0, when enabled.
mnegate:SAME
**** The additive inverse of self modulo 2^asize. Never raises an exception.
mod(i:SAME):SAME
**** Signed remainder of self divided by `i'. This and `mod' have the property that for non-zero `y', `x=x.div(y)*x + x.mod(y)'. It's also true that `0 <= x.mod(y) < y.abs'. Raises an exception when `i' is 0, when enabled. Built-in.
mplus(i:SAME):SAME
**** The sum of self and `i' modulo 2^asize. Never raises an exception. Built-in.
mtimes(i:SAME):SAME
**** The product of self and `i' modulo 2^asize. Never raises an exception. Built-in.
negate:SAME
**** The signed negation of self. Same as zero minus self. Only raises an exception on the most negative value (which doesn't have a corresponding positive value in 2's complement.) Built-in.
next_exp2:INT
**** for self positive it returns the p so that the following holds: p.is_pow_of_2 and p>=self>(p/2)
next_multiple_of(i:SAME):SAME
**** The smallest value greater than or equal to self which is a multiple of `i'.
next_pow_of_2:INT
nil:SAME
**** The value to be used to represent no element in sets. The most negative value.
num_digits:INT
**** The number of decimal digits in non-negative self. Use binary search so that small values take only 3 compares.
octal_str:STR
**** The octal representation of self of the form "0o15". Self is interpreted as an unsigned integer.
plus(i:SAME):SAME
**** The signed sum of self and `i'. Raises an exception on overflow, when enabled. Built-in.
pow(i:INT):SAME
**** Self raised to the power `i'.
rrotate(i:INT):SAME
**** Right rotate the bits of self by `i' places.
rshift(i:INT):SAME
**** The bits of self shifted right by `i' places with bits equal to the first bit of self shifted in on the left.
set_bit(i:INT):SAME
**** Self with bit `i' set to 1.
set_bit(i:INT,b:BOOL):SAME
**** Self with bit `i' set to b.
sign:SAME
**** -1,0,1 depending on the sign of self. Steele, 304
sqrt:SAME
**** The largest integer whose square is smaller than self.
square:SAME
**** The square of self. Built-in.
str:STR
str_in(s:FSTR):FSTR
**** Append a decimal string version of self to `s' and return it.
str_in (s: FSTR, n, b: INT, f: CHAR): FSTR
**** Append a string representation of self to s using at least n digits to the base b and return s. If less then n digits are used for the representation of self (including its sign), the remaining left_most positions are filled with character f.
ten_pow:SAME
times(i:SAME):SAME
**** The signed product of self and `i'. Raises an exception if the product can't be held in the result, when enabled. Built-in.
udiv(i:SAME):SAME
**** The unsigned quotient of self and `i'. Raises an exception when `i' is 0, when enabled. Built-in.
umax(i:SAME):SAME
**** The larger of self and `i' as unsigned integers.
umin(i:SAME):SAME
**** The smaller of self and `i' as unsigned integers.
uminus(i:SAME):SAME
**** The unsigned difference between self and `i'. Raises an exception on overflow or if the result would be negative, when enabled. Built-in.
umod(i:SAME):SAME
**** Unsigned remainder of self divided by `i'. Raises an exception when `i' is 0, when enabled. Built-in.
unset_bit(i:INT):SAME
**** Self with bit `i' set to 0.
uplus(i:SAME):SAME
**** The unsigned sum of self and `i'. Raises an exception on overflow, when enabled. Built-in.
urshift(i:INT):SAME
**** The bits of self shifted right by `i' places with zeroes shifted in on the left.
utimes(i:SAME):SAME
**** The unsigned product of self and `i'. Raises an exception if the product can't be held in the result, when enabled. Built-in.
within(x,y:SAME):SAME
**** Same as `max(x).min(y)'.

Iters
downto!(once i:SAME):SAME
**** Yield successive integers from self down to `i' inclusive.
for!(once i:SAME):SAME
**** Yields `i' successive integers starting with self.
product!(i:SAME):SAME
**** Yields the product of all previous values of `i'.
step!(once num,once step:SAME):SAME
**** Yield `num' integers starting with self and stepping by `step'.
stepto!(once to,once by:SAME): SAME
**** Yield succeeding integers from self to `to' by step `by'. Might quit immediately if self is aleady `beyond'.
sum!(i:SAME):SAME
**** Yields the sum of all previous values of `i'.
times!
**** Yields self times without returning anything.
times!:SAME
**** Yield successive integers from 0 up to self-1.
up!:SAME
**** Yield successive integers from self up.
upto!(once i:SAME):SAME
**** Yield successive integers from self to `i' inclusive.