Next: Floating-point numbers
Up: Numbers
Previous: Numbers
Index
In integer.diesel:
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.
module Integer;
abstract class integer isa num;
Modulo and remainder operations:
fun mod(l:integer,r:`T <= integer):T; - modulus
fun %(:integer, :`T <= integer):T; - same as mod
fun rem(:integer, :`T <= integer):T; - remainder
fun %_ov(:integer, :`T <= integer):T; - version of % that handles overflow
Bitwise operations:
fun «(l:integer,r:integer):integer; - left shift
fun »(l:integer,r:integer):integer; - right arithmetic shift (extends sign)
fun «_ov(l:integer,r:integer):integer;
fun »_ov(l:integer,r:integer):integer;
fun bit_and (l:integer, r:integer):integer;
fun bit_or (l:integer, r:integer):integer;
fun bit_xor (l:integer, r:integer):integer;
fun bit_xnor (l:integer, r:integer):integer;
fun bit_not(l:integer):integer;
fun get_bit(i:integer, bit:integer):int; - extract ith bit (0 or 1)
fun set_bit(i:integer, bit:integer):integer; - set ith bit to 1
fun clear_bit(i:integer, bit:integer):integer; - set ith bit to 0
Conversion operations:
fun as_small_int(l:integer):int;
fun as_small_int(:integer, if_overflow:&():`T):int|T;
as_small_int_if_possible
converts its argument to use an int
representation if its value is in range, otherwise leaves the
argument in its original (big_int
) representation
fun as_small_int_if_possible(l:integer):integer;
Operations to manipulate integers in the range [-263..263),
i.e., those integers that can be represented in 8 signed bytes.
fun is_int8(val:integer):bool;
fun as_int8(val:integer, if_overflow:&():integer):integer;
Logarithm operations:
fun log_base(x:integer, base:integer):int; - compute logarithm to nearest integer; rounds up
fun exact_log_base(x:integer, base:integer, if_not_exact:&():int):int; - compute logarithm, but invoke closure if integer result not exact
Test parity:
fun is_even(i:integer):bool;
fun is_odd(i:integer):bool;
round_up
rounds toward positive infinity, while round_down
rounds
toward negative infinity. Both ignore the sign of their second
argument.
fun round_up(i:integer, nearest:integer):integer;
fun round_down(i:integer, nearest:integer):integer;
Some standard mathematical functions:
fun factorial(n:integer):integer;
fun fibonacci(n:integer):integer;
fun fibonacci_recursive(n:integer):integer;
do_digits_increasing
calls its closure on each digit,
optionally specifying the base, from least to most significant;
the position argument indicates the digit's significance and
starts at zero.
fun do_digits_increasing(i:integer,
c:&(digit:int, position:int):void):void;
fun do_digits_increasing_base(i:integer, base:int,
c:&(digit:int, position:int):void):void;
Printing behavior:
fun print_string_base(i:integer, base:int):string; - specify the base in which to print
fun print_string_padded(i:integer, len:int):string; - right-justify the number to be at least len wide
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.
fun parse_as_int(s:string):integer;
fun parse_as_int(s:string, base:int | &():integer):integer;
fun parse_as_int(s:string, base:int, fail:&():integer):integer;
fun parse_as_small_int(s:string):int;
fun parse_as_small_int(s:string, base:int | &():int):int;
fun parse_as_small_int(s:string, base:int, fail:&():int):int;
In small-int.diesel:
Small integers (int) are the main representation of integers.
They have some fixed size (slightly less than the target
machine's natural word size, in the current implementation).
Arithmetic operations by default are performed modulo this fixed
size, so that overflows silently wrap around.
(Arbitrary-precision integers, a.k.a. big_int
s, handle
overflow ``properly''.)
module SmallInteger;
abstract class int isa integer;
Small (signed) integers can be viewed as unsigned integers, and
then compared using _unsigned
operations.
fun <_unsigned (l:int, r:int):bool;
fun <=_unsigned (l:int, r:int):bool;
fun >_unsigned (l:int, r:int):bool;
fun >=_unsigned (l:int, r:int):bool;
These operations take an extra closure argument that determines
what the operation should do if there's an overflow.
fun negate_ov (l:int, if_overflow:&():`T):int|T;
fun add_ov (l:int, r:int, if_overflow:&():`T):int|T;
fun sub_ov (l:int, r:int, if_overflow:&():`T):int|T;
fun mul_ov (l:int, r:int, if_overflow:&():`T):int|T;
fun div_ov (l:int, r:int, if_overflow:&():`T):int|T;
fun mod_ov (l:int, r:int, if_overflow:&():`T):int|T;
Extra bitwise operations which depend on the fixed size nature
of a small integer:
fun »_logical (l:int, r:int):int; - logical right shift (no sign-extension)
times_do
on integers is a simple kind of for-loop. Expression
n.times_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.
fun times_do(count:int, c:&(int):void):void;
Implementation-dependent properties of a small integer:
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
In big-int.diesel:
Big integers are an arbitrary-precision representation of integers.
module BigInt;
class big_int isa integer;
fun as_big_int(:integer):big_int;
Next: Floating-point numbers
Up: Numbers
Previous: Numbers
Index
Cecil/Vortex Project