next up previous index
Next: Floating-point numbers Up: Numbers Previous: Numbers   Index

Integers

In integer.cecil:

abstract object integer isa num;
method =(l@:integer,r@:integer):bool;
method <(l@:integer,r@:integer):bool;
implementation +(l@:integer,r@:integer):integer;
implementation -(l@:integer,r@:integer):integer;
implementation *(l@:integer,r@:integer):integer;
implementation /(l@:integer,r@:integer):integer;
method mod(l@:integer,r@:`T <= integer):T;  - modulus
signature %(integer, `T <= integer):T;  - same as mod
implementation %(l@:integer,r@:integer):integer;
signature rem(integer, `T <= integer):T;  - remainder
implementation rem(l@:integer,r@:integer):integer;
method -_ov(l@:integer):integer;
method +_ov(l@:integer,r@:integer):integer;
method -_ov(l@:integer,r@:integer):integer;
method *_ov(l@:integer,r@:integer):integer;
method /_ov(l@:integer,r@:integer):integer;
method %_ov(l@:integer,r@:integer):integer;
method «(l@:integer,r@:integer):integer;  - left shift
method »(l@:integer,r@:integer):integer;  - right arithmetic shift (extends sign)
method «_ov(l@:integer,r@:integer):integer;
method »_ov(l@:integer,r@:integer):integer;
method bit_and (l@:integer, r@:integer):integer;
method bit_or (l@:integer, r@:integer):integer;
method bit_xor (l@:integer, r@:integer):integer;
method bit_xnor (l@:integer, r@:integer):integer;
method bit_not(l@:integer):integer;
method get_bit(i@:integer, bit@:integer):integer;  - extract ith bit (0 or 1)
method set_bit(i@:integer, bit@:integer):integer;  - set ith bit to 1
method clear_bit(i@:integer, bit@:integer):integer;  - set ith bit to 0
method as_int(l@:`T <= integer):T;
method as_small_int(l@:integer):int;
signature as_small_int(integer, if_overflow:&():`T):int|T;
method as_small_int_if_possible(l@:integer):integer;
method is_int8(val@:integer):bool;
method as_int8(val@:integer, if_overflow:&():integer):integer;
method log_base(x@:integer, base@:integer):int;  - compute logarithm to nearest integer; rounds up
method exact_log_base(x@:integer, base@:integer,
                      if_not_exact:&():int):int;  - compute logarithm, but invoke closure if integer result not exact
method is_even(i@:integer):bool;  - test parity
method is_odd(i@:integer):bool;
method hash(i@:integer, range:`T <= integer):S
    where signature %(integer, T):`Q, signature abs(Q):`S;
signature average(integer,integer):integer;
Integers are numbers, supporting all the operations of numbers. The normal +, -, *, and / operations do not trap overflow; the +_ov, etc. functions transparently coerce to arbitrary-precision integers if overflow happens. The bitwise operations assume two's complement representation.

method round_up(i:integer, nearest:integer):integer;
signature round_up(int, int):int;
method round_down(i:integer, nearest:integer):integer;
signature round_down(int, int):int;
method factorial(n:integer):integer;
method fibonacci(n:integer):integer;
method fibonacci_recursive(n:integer):integer;
round_up rounds toward positive infinity, while round_down rounds toward negative infinity. Both ignore the sign of their second argument.

method do_digits_increasing(i@:integer, c:&(digit:int,position:int):void):void;
method do_digits_increasing_base(i@:integer, base:int,
                                 c:&(digit:int,position:int):void):void;
method print_string(i@:integer):string;
method print_string_base(i@:integer, base:int):string;
method print_string_base(i@:int, base:int):string;
method print_string(i@:integer, len:int):string;
do_digits_increasing calls its closure on each digit from least to most significant; the position argument indicates the digit's significance and starts at zero.

method parse_as_int(s@:string):integer;
method parse_as_int(s@:string, base@:int):integer;
method parse_as_int(s@:string, fail@:&():integer):integer;
method parse_as_int(s@:string, base@:int, fail:&():integer):integer;
method parse_as_small_int(s@:string):int;
method parse_as_small_int(s@:string, base@:int):int;
method parse_as_small_int(s@:string, fail@:&():int):int;
method parse_as_small_int(s@:string, base@:int, fail:&():int):int;
The parse_as_int methods try to convert a string representation of an integer into the integer representation, calling the optional if_error closure if the string doesn't contain an integer. In small-int.cecil:

extend int isa integer;
method = (l@:int, r@:int):bool;
method < (l@:int, r@:int):bool;
method <_unsigned (l@:int, r@:int):bool;
method <= (l@:int, r@:int):bool;
method <=_unsigned (l@:int, r@:int):bool;
method > (l@:int, r@:int):bool;
method >_unsigned (l@:int, r@:int):bool;
method >= (l@:int, r@:int):bool;
method >=_unsigned (l@:int, r@:int):bool;
method != (l@:int, r@:int):bool;
implementation + (l@:int, r@:int):int;
implementation - (l@:int, r@:int):int;
implementation * (l@:int, r@:int):int;
implementation / (l@:int, r@:int):int;
implementation % (l@:int, r@:int):int;
method negate_ov (l@:int, if_overflow:&():`T):int|T;
method add_ov (l@:int, r@:int, if_overflow:&():`T):int|T;
method sub_ov (l@:int, r@:int, if_overflow:&():`T):int|T;
method mul_ov (l@:int, r@:int, if_overflow:&():`T):int|T;
method div_ov (l@:int, r@:int, if_overflow:&():`T):int|T;
method mod_ov (l@:int, r@:int, if_overflow:&():`T):int|T;
method -_ov(l@:int):integer;
method +_ov (l@:int, r@:int):integer;
method -_ov (l@:int, r@:int):integer;
method *_ov (l@:int, r@:int):integer;
method /_ov (l@:int, r@:int):integer;
method %_ov (l@:int, r@:int):integer;
method as_single_float(l@:int):single_float;
method as_double_float(l@:int):double_float;
method bit_and (l@:int, r@:int):int;
method bit_or (l@:int, r@:int):int;
method bit_xor (l@:int, r@:int):int;
method bit_not(l@:int):int;
method « (l@:int, r@:int):int;
method » (l@:int, r@:int):int;
method »_logical (l@:int, r@:int):int;  - logical right shift (no sign-extension)
method get_bit(i@:int, bit@:int):int;
method set_bit(i@:int, bit@:int):int;
method clear_bit(i@:int, bit@:int):int;
method is_even(i@:int):bool;
method sqrt(x@:int):int;
signature average(int,int):int;
signature pred(int):int;
signature succ(int):int;
method as_small_int(l@:int, if_overflow:&():`T):int;
Small integers (int) are the main representation of integers.

method do(count@:int, c:&(int):void):void;
method print_string(i@:int):string;
let num_int_bits:int;  - number of bits in small int representation
let max_int:int;  - maximum small int
let min_int:int;  - minimum small int
do on integers is a simple kind of for-loop. Expression n.do(&(i:int){...}) invokes the argument closure n times, binding i to each of the numbers from 0 to n-1 in turn; it does not return a value. In big-int.cecil:

template object big_int isa integer;
method =(l@:big_int, r@:big_int):bool;
method < (l@:big_int, r@:big_int):bool;
method <=(l@:big_int, r@:big_int):bool;
method >=(l@:big_int, r@:big_int):bool;
method > (l@:big_int, r@:big_int):bool;
method compare(l@:big_int, r@:big_int,
               if_less:&():`T, if_equal:&():`T, if_greater:&():`T
               ):T;
method -(l@:big_int):big_int;
method +(l@:big_int, r@:big_int):big_int;
method -(l@:big_int, r@:big_int):big_int;
method *(l@:big_int, r@:big_int):big_int;
method /(l@:big_int, r@:big_int):big_int;
method %(l@:big_int, r@:big_int):big_int;
method %(l@:big_int, r@:int):int;
method «(l@:big_int, r@:integer):integer;
method »(l@:big_int, r@:integer):integer;
method as_small_int(l@:big_int, if_overflow:&():`T):int|T;
signature as_big_int(integer):big_int;
implementation as_big_int(l@:big_int):big_int;
implementation as_big_int(l@:int):big_int;
method as_single_float(l@:big_int):single_float;
method as_double_float(l@:big_int):double_float;
method print_string_base(x@:big_int, base:int):string;
Big integers are an arbitrary-precision representation of integers.


next up previous index
Next: Floating-point numbers Up: Numbers Previous: Numbers   Index

Cecil/Vortex Project