Previous | Table of Contents | Next |
6.2.4.9. Pointers
Structure values are pointers to collections of values. The result of
S := set()
may be visualized as follows:
Every structure-creation operation creates a new structure that is different from all other structures; it points to a different object. Even if two structures have identical values, they are not the same. For example, the expressions
S1 := set() S2 := set()
create two different empty sets:
The comparison operation x1 === x2 compares the values of x1 and x2. In order for the comparison to succeed, x1 and x2 must be of the same data type, and if they are structures, they must be the same. Thus, for the preceding example,
S1 === S1
succeeds, but
S1 === S2
fails.
Assignment of a structure to a variable copies the pointer to the structure, not the structure itself. Thus, after
S3 := S1
S1 and S3 point to the same structure, and
S1 === S3
succeeds:
It is important to understand that changing a value in a structure changes it for all values that point to the structure. For example, since S1 and S3 are the same, after
insert(S1, the)
the expression
member(S3, the)
succeeds:
Similarly, if a structure is the value of an argument in a procedure call, a pointer to it is passed to the procedure. If the procedure changes values in that structure, they are, of course, changed in all pointers to it, including ones outside the procedure.
Because structure values are pointers, a structure can contain (pointers) to other structures. For example,
insert(S3, set())
produces the following situation:
The function copy(x) can be used to copy a structure and create a new structure value. For example, after
S4 := copy(S1)
S4 is a different structure from S1, but it has the values in S1 at the time of the copy.
Such a copy is a one-level copy; if the structure being copied contains pointers to other structures, the pointers are copied but what they point to is not copied. For example,
S4 := copy(S1)
produces this situation:
Although Icon emphasizes computations related to strings and structures, it has an extensive repertoire of operations for numerical computation.
Integers can be arbitrarily large. Real numbers are represented by floating-point values and depend for their precision and range on the platform on which Icon runs.
In mixed-mode operations involving both integer and real values, integers are promoted to reals and the result is real.
6.2.5.1. Literals
Integers
Integers are represented literally in the usual way. By default, integer literals are in base 10.
Radix literals can be used to specify bases other than 10. Radix literals have the form i r j, where i, in base 10, specifies the base for j. The base can be any value from 2 through 36. The letters a, b, , z are used to specify digits in j that are larger than 9. Examples are
Real Numbers
Real numbers can be represented literally using either decimal or exponent notation. For example, 31.2e3 and 31200.0 are equivalent and represent the real number 31,200.0.
6.2.5.2. Arithmetic
The precedence and associativity for infix (binary) arithmetic operations follow the conventional rules:
Expression | Operation | Precedence | Associativity |
---|---|---|---|
N1 ^ N2 | exponentiation | 3 | right to left |
N1 % N2 | remaindering | 2 | left to right |
N1 / N2 | division | 2 | left to right |
N1 * N2 | multiplication | 2 | left to right |
N1 N2 | subtraction | 1 | left to right |
N1 + N2 | addition | 1 | left to right |
In addition, the prefix (unary) operation -N produces the negative of N, and abs(N) produces the absolute value of N.
In integer division, the remainder is discarded; that is, the value is truncated toward 0. For example,
11 / 5
produces 2.
The remaindering operation, N1 % N2, produces the remainder of N1 divided by N2 with the sign of N1. For example,
11 % 2
produces 1, but
11 % 2
produces 1.
Division by zero and raising a negative real number to a real power are erroneous and cause program termination.
6.2.5.3. Numerical Comparison
The numerical comparison operations are
N1 = N2 | equal to |
N1 ~= N2 | not equal to |
N1 < N2 | less than |
N1 <= N2 | less than or equal to |
N1 >= N2 | greater than or equal to |
N1 > N2 | greater than |
Numerical comparison operations succeed and return the value of the right operand if the relationship holds. They fail otherwise.
Previous | Table of Contents | Next |