INTERMEDIATE SECTION B FILE INTER.B
MATHEMATICS
***********
******************************************************************
Despite common impressions, calculations done by computer
are not as accurate as, say, a pocket calculator, unless the
computer has a dedicated number cruncher chip, which is not the case
with the PCW. However I stress that results are correct for
practical purposes.
*******************************************************************
In PRIMER we saw how to do simple addition (+), subtraction (-),
multiplication (*) and division (/) and how to display sums on the
screen using the PRINT command,
PRINT " 6 x 4 = " 6*4
MALLARD BASIC has a number of other maths functions which
provided that you have the necessary knowledge of maths are quick
and easy to use. They can be used within the program or display
results on screen using the PRINT command.A brief summary is shown
below; where necessary a more detailed explanation is given later.
TRIGONOMETRY - Values for SIN, COS, TAN and ArcTaN (ATN) are
readily obtained using the form SIN(angle) where the angle is in
radians.
LOGARITHMS - There are two logarithmic functions, for natural
logs (LOG) and logs to base 10 (LOG10) They are used simply
LOG10(number) The antilogarith of a natural log is found using the
function EXP(log)
EXPONENTIAL - a number can be raised to a power by using the
operator ^ ( press EXTRA and U together) e.g 6^3 would yield 6
cubed.
SQUARE ROOT - SQR( number) yields the square root
INTEGER MATHS - as well as the operators +,-,* and / there are
two others which are mostly useful with integers
\ (EXTRA + ©) for division without remainder (press key twice)
MOD for division showing remainder only
e.g PRINT 9\2 ( answer 4 )
PRINT 9 MOD 2 ( answer 1)
MAXIMA and MINIMA -MAX and MIN are functions which select the
maximum or minimum values in a list of numbers and are used so :-
MAX( list of numbers ) will select the maximum value in the list,
even taking into account negative numbers. What's the point ?,you
might say, I can do that myself easily enough; but think- the
numbers may be in the form of variables and buried in the program. A
very useful function.
ABSOLUTE VALUE - ABS determines the absolute value of a number-
in effect a positive number remains unchanged, a negative number has
the sign stripped off, showing it positive.e.g
ABS(-5) = 5 : ABS(67) = 67 : Useful for some number
manipulations
CHECKING SIGNS - SGN( number) determines whether the number is
positive, negative or zero, and returns a coded reply.
positive = +1
zero = 0
negative = -1
This can be used to check any value which might be in the three
states e.g Bank balance [ overdrawn,nil funds,in credit ] or
Temperature [ low , correct , high ] etc.
ROUNDING - Functions exist to round figures in various ways
INT(number) rounds the number downward to integer e.g
INT(3.45) = 3 : INT( -5.6) = -6 etc.
FIX(number) rounds the number towards zero e.g FIX(3.45) = 3
: FIX( -5.6) = -5 etc.
ROUND(number) rounds the number to the nearest integer
e.g ROUND(3.7)= 4 ; ROUND(10.2)= 10 etc.
ROUND( number, decimals) rounds the number to the number of
decimal places specified e.g ROUND(8.5674,2) = 8.57 ;
ROUND(23.136284,4) = 23.1363 etc.
GENERAL NOTES
**************
In the above we have simply shown what the various functions do. You
see that in most cases the simple usage is FUNCTION(number). Usually
in a program the number will be represented by a variable e.g
FUNCTION(a%) , and if it is to be displayed on screen
PRINT FUNCTION(a%).
Numbers are handled in various ways in MALLARD BASIC - integer,
single precision and double precision. Integers take up less memory
and are faster to process, double precision uses most memory and is
slowest. Unless specified, single precision is used (default).
Three types of suffix are applied to a variable to specify it's type
a% integer
a! single precision
a# double precision
Commands are available to reserve letters, or groups of letters, at
the beginning of a program for the three types
(DEFINT,DEFSNG,DEFDBL) or you can use the appropriate suffix as you
go along. This is dealt with more fully in the section on variables.
In everyday life we use the decimal system of numbers, that is the
system is based on 10. In the computer world other systems are also
used; BINARY (based on 2), OCTAL (based on 8) and HEXADECIMAL (based
on 16)
In MALLARD BASIC octal system numbers are prefixed with &O and hex.
numbers are prefixed with &H; decimal system numbers have no
prefix.You are unlikely to meet binary or octal unless you become
interested in low level programming(machine code) ,but you may well
come across hex. numbers in BASIC programs. Suffice it to say that
hex numbers have capital letters A,B,C,D,E and F representing
10,11,12,13,14 and 15; so a typical hex number could be &H3F or
&HA34D etc. I have only mentioned this as a point of interest- it is
not covered further in this course.
Finally MALLARD BASIC uses scientific notation or 'scaled numbers'
for numbers which a very small or very large. Scientific notation
describes a decimal number as a number raised to a power of 10 ,
thus 2,000,000 might be represented as 2E6 or 2D6 i.e 2 x 10 to the
power of 6. The E or D acts as a separator, but also shows to what
accuracy the number is stored E = single, D = double. All magnitudes
of number can be represented e.g .00345 = 345E-4; -10 = -1E2
There are limits to the power of the computer in dealing with
manipulation of very large or very small numbers, such as one might
need for evaluating engineering formulae. If the computer refuses
and prints an 'overflow' message, break the calculation into smaller
pieces, then reassemble to achieve your result.
End of file INTER.B
the calculation into smaller
pieces, then reassem