Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


11.3.4 Maths and logic functions

We've already seen the standard arithmetic operators. The addition, +, and subtraction, -, operators use full 32-bit integers, but, for efficiency, multiplication, *, and division, /, use restricted values. You can use * only to multiply 16-bit integers, and the result will be a 32-bit integer. Similarly, you can use / only to divide a 32-bit integer by a 16-bit integer, and the result will be a 16-bit integer. The restrictions do not affect most calculations, but if you really need to use all 32-bit integers (and you can cope with overflows, etc.) you can use the Mul and Div functions. Mul(a,b) corresponds to a*b, and Div(a,b) corresponds to a/b.

We've also met the logic operators AND and OR, which we know are really bit-wise operators. You can also use the functions And and Or to do exactly the same as AND and OR (respectively). So, for instance, And(a,b) is the same as a AND b. The reason for these functions is because there are Not and Eor (bit-wise) functions, too (and there aren't operators for these). Not(a) swaps one and zero bits, so, for instance, Not(TRUE) is FALSE and Not(FALSE) is TRUE. Eor(a,b) is the exclusive version of Or and does almost the same, except that Eor(1,1) is 0 whereas Or(1,1) is 1 (and this extends to all the bits). So, basically, Eor tells you which bits are different, or, logically, if the truth values are different. Therefore, Eor(TRUE,TRUE) is FALSE and Eor(TRUE,FALSE) is TRUE.

There's a collection of other functions related to maths, logic or numbers in general:

Abs(expression)
Returns the absolute value of expression. The absolute value of a number is that number without any minus sign (i.e., its the size of a number, disregarding its sign). So, Abs(9) is 9, and Abs(-9) is also 9.

Sign(expression)
Returns the sign of expression, which is the value one if it is (strictly) positive, -1 if it is (strictly) negative and zero if it is zero.

Even(expression)
Returns TRUE if expression represents an even number, and FALSE otherwise. Obviously, a number is either odd or even!

Odd(expression)
Returns TRUE if expression represents an odd number, and FALSE otherwise.

Max(exp1, exp2)
Returns the maximum of exp1 and exp2.

Min(exp1, exp2)
Returns the minimum of exp1 and exp2.

Bounds(exp, minexp, maxexp)
Returns the value of exp bounded to the limits minexp (minimum bound) and maxexp (maximum bound). That is, if exp lies between the bounds then exp is returned, but if it is less than minexp then minexp is returned or if it is greater than maxexp then maxexp is returned. This is useful for, say, constraining a calculated value to be a valid (integer) percentage (i.e., a value between zero and one hundred).

The following code fragments are equivalent:

  y:=Bounds(x, min, max)

  y:=IF x<min THEN min ELSE IF x>max THEN max ELSE x

Mod(exp1,exp2)
Returns the 16-bit remainder (or modulus) of the division of the 32-bit exp1 by the 16-bit exp2 as the regular return value (see 7.4 Multiple Return Values), and the 16-bit result of the division as the first optional return value. For example, the first assignment in the following code sets a to 5 (since 26=(7*3)+5), b to 3, c to -5 and d to -3. It is important to notice that if exp1 is negative then the modulus will also be negative. This is because of the way integer division works: it simply discards fractional parts rather rounding.

  a,b:=Mod(26,7)
  c,d:=Mod(-26,7)

Rnd(expression)
Returns a random number in the range 0 to (n-1), where expression represents the value n. These numbers are pseudo-random, so although you appear to get a random value from each call, the sequence of numbers you get will probably be the same each time you run your program. Before you use Rnd for the first time in your program you should call it with a negative number. This decides the starting point for the pseudo-random numbers.

RndQ(expression)
Returns a random 32-bit value, based on the seed expression. This function is quicker than Rnd, but returns values in the 32-bit range, not a specified range. The seed value is used to select different sequences of pseudo-random numbers, and the first call to RndQ should use a large value for the seed.

Shl(exp1,exp2)
Returns the value represented by exp1 shifted exp2 bits to the left. For example, Shl(%0001110,2) is %0111000 and Shl(%0001011,3) is %1011000. Shifting a number one bit to the left is generally the same as multiplying it by two (although this isn't true when you shift large positive or large negative values). (The new bits shifted in at the right are always zeroes.)

Shr(exp1,exp2)
Returns the value represented by exp1 shifted exp2 bits to the right. For example, Shr(%0001110,2) is %0000011 and Shr(%1011010,3) is %0001011. For positive exp1, shifting one bit to the right is the same as dividing by two. (The new bits shifted in at the left are zeroes if exp1 is positive, and ones otherwise, hence preserving the sign of the expression.)

Long(addr), Int(addr), Char(addr)
Returns the LONG, INT or CHAR value at the address addr. These functions should be used only when setting up a pointer and dereferencing it in the normal way would make your program cluttered and less readable. Use of functions like these is often called peeking memory (especially in dialects of the BASIC language).

PutLong(addr,exp), PutInt(addr,exp), PutChar(addr,exp)
Writes the LONG, INT or CHAR value represented by exp to the address addr. Again, these functions should be used only when really necessary. Use of functions like these is often called poking memory.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.