Sinclair QL Beginner's Manual page 60

Hide thumbs Also See for QL:
Table of Contents

Advertisement

The following examples show common uses of the INT function.
100 REMark Rounding
110 INPUT decimal
120 PRINT INT(decimal + 0.5)
In the example you input a decimal fraction and the output is rounded. Thus 4.7 would become 5 but
4.3 would become 4.
You can achieve the same result using an integer variable and coercion.
Trigonometrical functions will be dealt with in a later section but other common numeric functions are
given in the list below.
Function
ABS
INT
SQRT
There is a way of computing square roots which is easy to understand. To compute the square root of
8 first make a guess. It doesn't matter how bad the guess maybe. Suppose you simply take half of 8
as the first guess which is 4.
Because 4 is greater than the square root of 8 then 8/4 must be less than it. The reverse is also true.
If you had guessed 2 which is less than the square root then 8/2 must be greater than it.
It follows that if we take any guess and computer number/guess we have two numbers, one too small
and one too big. We take the average of these numbers as our next approximation and thus get
closer to the correct answer.
We repeat this process until successive approximations are so close as to make little difference:
100 REMark Square Roots
110 LET number = 8
120 LET approx = number/2
130 REPeat root
140
LET newval = (approx + number/approx)/2
150
IF newval == approx THE EXIT root
160
LET approx = newval
170 END REPeat root
180 PRINT 'Square root of' ! number ! 'is' ! newval
sample output:
Square root of 8 is 2.828427
Notice that the conditional EXIT from the loop must be in the middle. The traditional structures do not
cope with this situation as well as SuperBASIC does. The == sign in line 150 means "approximately
equal to", that is, equal to within .0000001 of the values being compared.
Effect
Absolute or unsigned value
Integer part of a floating point
number
Square root
Examples
Returned values
7
ABS(7)
4.3
ABS(-4.3)
2
INT(2.4)
0
INT(0.4)
-3
INT(-2.7)
1.414214
SQRT(2)
4
SQRT(16)
1.612452
SQRT(2.6)

Advertisement

Table of Contents
loading

Table of Contents