Data
Types,
Variables
and
Identifiers
NUMERIC
OPERATIONS
50
Function Effect Examples Returned values
•
ASS
Absolute or ABS(7) 7
unsigned value
ABS(-4.3)
4.3
Integer part of a
INT(2A)
2
INT
floating pOint
INT(O.4)
0
number
INT(-2.7)
-3
SORT(2)
1.414214
SORT
Square root
SORT(16)
4
SORT(2.6)
1.612452
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 halt 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 compute 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
THEN
EXIT
root
160
LET
approx
=newva 1
170
END
REPeat
root
180
PRINT
'Square
root
of
1
!
number
I
';SI
! 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
td:
that
is
equal
to
within .0000001 •
of the values being compared.
SuperBASIC allows the usual mathematical operations.
You
may notice that they are like
functions with exactly two operands each.
It
is
also conventional
in
these cases to put
an
operand on each side
of
the symbol. Sometimes the operation
is
denoted by a familiar
symbol such as
+ or
*.
Sometimes the operation
is
denoted by a keyword like
DIV
or
MOD
but there
IS
no
real
difference. Numeric operations have an order of priority.
For example, the result of:
PRINT 7
+
3*2
is
13
because the multiplication has a higher
priOrity.
However:
PRINT
(7
+
3)*2
will output
20,
because brackets over-ride the usual priority.
As
you
Will
see later so many
things can
be
done
with SuperBASIC expressions that a full statement about priority
cannot be
made
at
this stage (see the Concept Reference Guide
if
you wish) but the
operations we now deal
With
have the
follOWing
order of priority:
highest
- raising to a power •
multiplication and division (including DIV, MOD)
lowest
-
add
and subtract
12/84