Previous | Table of Contents | Next |
6.2.5.4. Numerical Computation
Icon supports the following trigonometric functions:
sin(r) | sine of r |
cos(r) | cosine of r |
tan(r) | tangent of r |
asin(r) | arcsine of r |
acos(r) | arccosine of r |
atan(r1, r2) | arctangent of r1 / r2 |
Angles are in radians. If r2 is omitted in atan(), the default is 1.0.
The following functions convert between radians and degrees:
dtor(r) | the radian equivalent of r given in degrees |
rtod(r) | the degree equivalent of r given in radians |
The following functions for numerical calculations also are provided:
sqrt(r) | square root of r |
exp(r) | e raised to the power r |
log(r1, r2) | logarithm of r1 to the base r2 |
The default for r2 is e.
Common mathematical constants are provided by keywords:
&e | base of the natural logarithms, 2.71828 |
&phi | golden ratio, 1.61803 |
&pi | ratio of circumference to diameter of a circle, 3.14159 |
6.2.5.5. Pseudo-Random Numbers
If i is a positive integer, ?i produces a randomly selected integer j in the range 1 = j = i. For example, the expression
if ?2 = 1 then odd else even
produces the string odd or even with approximately equal probability.
If i is 0, ?i produces a randomly selected real number r in the range 0.0 = r < 1.0.
Icons pseudo-random number generator uses a linear congruence relation with an initial seed of 0. The seed can be changed by assigning an integer value to &random.
6.2.5.6. Bit Operations
Icon has five functions that operate on the bit patterns of integers:
If j is positive, the shift is to the left, and vacated bit positions are filled with zeros. If j is negative, the shift is to the right with sign extension.
Generators are powerful tools for formulating many kinds of computations. A generator, however, can only produce its values at the place in the program where it appears. Furthermore, goal-directed evaluation prevents generators from producing their values in parallel.
A co-expression captures an expression, provides an environment for its evaluation, and allows the expressions values to be produced at any desired place and time in a program.
The expression
create expr
creates a co-expression that contains expr, but the evaluation of expr is deferred until its values are needed.
Co-expressions are values that can be assigned to variables, passed as arguments to procedures, and so on. Therefore,
intseq := create 1 to 100
creates a co-expression containing 1 to 100 and assigns it to intseq.
The values of the expression in a co-expression are produced by activating the co-expression, using the operation @C. One value is produced for each activation until there are no more values, in which case activation fails. For example,
while write(L, @intseq, :) do { # process data }
writes the L1:, L2:, up to L100:, interspersed by any output produced by processing data.
As another example, consider producing a file in which the octal, decimal, and hexadecimal numbers for the 256 characters are given in columns. The expression
(0 to 3) || (0 to 7) || (0 to 7)
generates the octal numbers 0 through 377 in order. The expression
0 to 255
generates the decimal numbers 0 through 255, and the expression
!0123456789abcdef || !0123456789abcdef
generates the hexadecimal numbers 00 through ff. To write these in parallel columns, all that is needed is to create co-expressions for these expressions and activate each of them in a while loop:
octal := create (0 to 3) || (0 to 7) || (0 to 7) decimal := create 0 to 255 hexadecimal := !0123456789abcdef || !0123456789abcdef while write(@octal, , @decimal, , @hexadecimal)
Expressions in co-expressions can be procedure calls. For example,
lseq := create labels()
assigns to lseq a co-expression for producing the values from labels.
In fact, Icon program execution begins with the activation of a co-expression for main().
Previous | Table of Contents | Next |