Numbers¶
Datatypes and constructors¶
The numeric datatypes of ABS are Int (arbitrary-length integers),
Rat (arbitrary-precision rational numbers) and Float (64-bit
floating point). See Built-in types for their syntax.
Note
Support for floating-point calculations is under
development; calculations resulting in Inf or NaN
currently have unspecified runtime behavior.
Operators¶
Expression |
Meaning |
Associativity |
Result type |
|---|---|---|---|
|
equality |
left |
|
|
inequality |
left |
|
|
less than |
left |
|
|
less or equal |
left |
|
|
greater than |
left |
|
|
greater or equal |
left |
|
|
addition |
left |
number |
|
subtraction |
left |
number |
|
multiplication |
left |
number |
|
division |
left |
|
|
modulo |
left |
number |
Functions¶
min, max¶
These functions calculate the maximum and minimum of their two arguments. Since ABS datatypes are ordered, they can be applied to arguments of all types.
max(5, 3)
// => 5
min(2.5, 1.5)
// => 1.5
abs¶
This function calculates the absolute (positive) value of its argument.
max(-2)
// => 2
truncate¶
Converts a rational number to an integer by truncating towards zero.
truncate(7/5)
// => 1
float¶
Converts an integer or rational number into a floating-point number.
float(1)
// => 1.0
float(1/2)
// => 0.5
Note
Very large integers and some rational numbers cannot be
converted exactly into floating-point numbers. More in
general, rat(float(x)) == x might not be true.
rat¶
Converts a floating-point number into a rational number.
rat(0.5)
// => 1/2
Note
Conversion from floating-point to rational numbers is
inexact and backend-specific. In general, float(rat(x))
== x might not be true.
floor¶
Returns the largest integer smaller or equal to the floating-point argument.
floor(5.3)
// => 5
ceil¶
Returns the smallest integer larger or equal to the floating-point argument.
ceil(5.3)
// => 6
numerator¶
Returns the numerator of a rational number, or the number itself for an integer.
numerator(1/2)
// => 1
denominator¶
Returns the denominator of a rational number, or 1 for an integer.
denominator(1/2)
// => 2
pow¶
This function calculates \(b^n\).
pow(1/2, 4)
// => 1/16
sqrt_newton¶
This function approximates \(\sqrt{x}\); it stops when two
subsequent estimates (as per Newton’s algorithm) differ by less than
epsilon, its third argument. The second argument is an initial
estimate of the square root.
sqrt_newton(4, 2, 1/100)
// => 2
exp_newton¶
This function approximates \(e^x\); it stops when two subsequent
estimates (as per Newton’s algorithm) differ by less than its second
argument epsilon.
exp_newton(1, 1/100)
// => 163/60
sqrt¶
This function returns \(\sqrt{x}\). It is an error if its argument is negative.
sqrt(4.0)
// => 2.0
log¶
This function returns \(ln(x)\), the natural logarithm of its argument.
log(1.0)
// => 0.0
exp¶
This function returns \(e^x\), Euler’s number \(e\) raised to
the power of x.
exp(1.0)
// => 2.7182818284590455
random¶
Returns an integer between 0 (inclusive) and its argument (exclusive).
random(5)
// => 4