Next: Integers
Up: Basic data types and
Previous: Equality, ordering, and hashing
Index
In number.diesel:
Numbers support the standard arithmetic operations, plus the
protocol of totally-ordered objects (=, <, min, etc.). This
contract implies that all subclasses of number are freely mixable
at run-time.
module Number;
abstract class num isa ordered_hashable[num];
as_int
converts its argument to an integer, possibly
rounding up or down if not already an integer.
fun as_int(:num):integer;
as_float
converts its argument to a float representation.
Related operations exist to specify the kind of float to
produce.
fun as_float(n:num):float;
fun as_single_float(:num):single_float;
fun as_double_float(:num):double_float;
Standard arithmetic operations. When invoked on a small
integer (an int), they might overflow and wrap around. (Use
the _ov
versions below to get correct answers in the face of
overflow.)
fun +(:`T <= num, :T):T;
fun -(:`T <= num, :T):T;
fun *(:`T <= num, :T):T;
/ returns an integer if applied to integers, rounding
towards zero. Use /_float
to yield a (precise) float
result.
fun /(:`T <= num, :T):T;
fun /_float(:`T <= num, :T):T & float;
Unary arithmetic operators:
fun negate(n:`T <= num):T;
fun -(n:`T <= num):T; - same as negate
fun +(n:`T <= num):T; - silly no-op
These arithmetic operators handle int overflow by converting
to arbitrary-precision integers (big_int
s):
fun -_ov(l:num):num;
fun +_ov(l:num,r:num):num;
fun -_ov(l:num,r:num):num;
fun *_ov(l:num,r:num):num;
fun /_ov(l:num,r:num):num;
fun /_float_ov(l:num,r:num):num;
Simple convenience functions:
fun pred(i:`T <= num):T; - i-1
fun succ(i:`T <= num):T; - i+1
fun square(n:`T <= num):T; - n2
fun cube(n:`T <= num):T; - n3
fun abs(n:`T <= num):T; - absolute value
fun sign(x:num):int; - return -1, 0, or 1 depending on sign of argument
fun average(n1:`T <= num, n2:`T):T|int; - arithmetic average: (n1+n2)/2
fun power(x:`T <= num, power:integer):S where *_ov(:T,:T):`S <= T; - exponentiation
fun **(x:`T <= num, power:integer):S where *_ov(:T,:T):`S <= T; - same as power
Subsections
Next: Integers
Up: Basic data types and
Previous: Equality, ordering, and hashing
Index
Cecil/Vortex Project