-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path05-03-maths.html
623 lines (466 loc) · 23 KB
/
05-03-maths.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
<!doctype html>
<html lang="en">
<head>
<!--
This Amos Professional Manual is written by asymetrix for the Amiga community and should stay completely FREE FOREVER.
Created 2008. :)
It was created from the original AMOS Professional Manual by Europress Software Ltd.
It has been updated by Fredrik Rambris.
-->
<title>Maths - AMOS Professional Manual</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="keywords" content="Amos Professional, Amiga, Programming, Basic, Francois Lionet, Europress Software Ltd, Amos, computing, code, AmigaDOS">
<meta name="author" content="asymetrix,Fredrik Rambris">
<link rel="GitHub" href="https://github.com/fredrik-rambris/amospromanual">
<meta property="og:site_name" content="AMOS Professional Manual">
<meta property="og:image" content="https://amospromanual.dev/images/cover.jpg">
<link rel="stylesheet" type="text/css" href="styles.css">
<link rel="canonical" href="https://amospromanual.dev/05-03-maths.html">
</head>
<body>
<section>
<h1>Maths</h1>
<br>
<P>This Chapter provides a full explanation of using standard mathematical and
trigonometric functions, as well an insight into how AMOS Professional exploits numbers.</P>
</section>
<section id="01-arithmetical-calculations">
<h2>Arithmetical calculations</h2>
<p>Nothing could be simpler than asking AMOS Professional to run this sum:</P>
<code class="prefix direct">Print 2+2</code>
<P>Arithmetical operations are straightforward, provided the correct symbols are used, as follows:</P>
<P>+ the plus sign always signals addition</P>
<p>- the minus sign is used for subtraction</p>
<p>* for multiplication, an asterisk character must be used</p>
<p>/ divisions are made using the forward-slash symbol</p>
<p>^ the circumflex character is used as the exponential symbol, and it means "raise this
number to a given power", which is exactly the same as multiplying a number with itself.</p>
<P>So the following two lines are interchangable:</P>
<code class="prefix direct">Print 3^5
Print 3*3*3*3*3
</code>
<P>The following logical operations can also be used in calculations:</P>
<P><b>MOD</b> is the "modulo" operator, which acts as a constant multiplier.
<b>AND</b>, <b>OR</b> and <b>XOR</b> are the three logical operations.</p>
</section>
<section id="02-calculation-priorities">
<h2>Calculation priorities</h2>
<p>Arithmetical instructions are taken literally, using a set of built-in priorities. So the
following lines give the results 6 and 8 respectively:</P>
<code class="prefix direct">Print 2+2*2
Print (2+2)*2
</code>
<P>AMOS Professional handles a combination of calculations that make up an "expression" in
the following strict order of priority:</P>
<UL>
<LI>exponential numbers are always calculated first ( ^ ).</LI>
<LI>multiplications and divisions are then calculated in order of appearance, from left
to right (*/). Remainders of divisions will be dealt with by any modulo operations (MOD).
</LI>
<LI>additions and subtractions are calculated last, again in order, from left to right
(+-).
</LI>
<LI>any logical operations will not be taken into account until after all the above
calculations have been completed (AND, OR, XOR).
</LI>
</UL>
<P>Any calculation placed inside a pair of round brackets is evaluated first, and treated
as a single number.</P>
<p>
The next calculation gives a result of 43, because it evaluated in the following order:</P>
<code class="prefix direct">Print 10+2*5-8/4+5^2
5^2 = 25
2*5 = 10
8/4 = 2
10+10 = 20
20-2 = 18
18+25 = 43
</code>
<P>By adding two strategic pairs of brackets to the same calculation, the logical
interpretation is transformed, resulting in an answer of 768, like this:</P>
<code class="prefix direct">Print (10+2)*(5-8/4+5)^2
10+2 = 12
5-8/4+5 = 5-2+5
5-2+5 = 8
8^2 = 64
12*64 = 768
</code>
</section>
<section id="03-fast-calculations">
<h2>Fast calculations</h2>
<p>There are three instructions that can be used to speedflip the process of simple calculations.</P>
<h3 class="command" id="i-inc">INC</h3>
<p><i>instruction: increment an integer variable by 1</i><BR>
<b>Inc</b> variable</P>
<P>This command adds 1 to an integer (whole number) variable, using a single instruction to
perform the expression variable=variable+1 very quickly. For example:</P>
<code class="prefix direct">V=10 : Inc V : Print V</code>
<h3 class="command" id="i-dec">DEC</h3>
<p><i>instruction: decrement an integer variable by 1</i><BR>
<b>Dec</b> variable</P>
<P>Similarly to INC, the DEC command performs a rapid subtraction of 1 from an integer
variable. For example:</P>
<code class="prefix direct">V=10 : Dec V : Print V</code>
<h3 class="command" id="i-add">ADD</h3>
<p><i>instruction: perform fast integer addition</i><BR>
<b>Add</b> variable,expression<BR>
<b>Add</b> variable,expression,base <b>To</b> top</P>
<p>
The ADD command can be used to add the result of an expression to a whole number variabk.
immediately. It is the equivalent to variable=variable+ expression but performs the
addition nearly twice as fast.</P>
<P>
There is a more complex version of ADD, which is ideal for handling certain loops much
more quickly than the equivalent separate instructions. When Base number and Top number
parameters are included, ADD is the equivalent to the following lines:</P>
<code class="prefix ex">V=V+A
If V<BASE Then V=TOP
If V>TOP Then V=BASE
</code>
<P>Here is an example:</P>
<code class="prefix edit">Dim A(10)
For X=0 To 10:A(X)=X:Next X
V=0
Repeat
Add V,1,1 To 10
Print A(V)
Until V=11 : <comment>Rem This loop is infinite as V is always <11</comment>
</code>
</section>
<section id="04-relative-values">
<h2>Relative values</h2>
<p>It is obvious that every expression has a value, but expressions are not restricted to
whole numbers (integers), or any sort of numbers. Expressions can be created from real numbers
or strings of characters. If you need to compare two expressions, the following functions are
provided to examine them and establish their relative values.</P>
<h3 class="command" id="fn-max">MAX</h3>
<p><i>function: return the maximum of two values</i><BR>
value=<b>Max</b>(a,b)<BR>
value#=<b>Max</b>(a#,b#)<BR>
value$=<b>Max</b>(a$,b$)</P>
<P>
MAX compares two expressions and returns the largest. Different types of expressions
cannot be compared in one instruction, so they must not be mixed.</P>
<P>Here are some examples:</P>
<code class="prefix direct">Print Max(99,1)
Print Max("AMOS Professional","AMOS")</P>
</code>
<h3 class="command" id="fn-min">MIN</h3>
<p><i>function: return the minimum of two values</i><BR>
value=<b>Min</b>(a,b)<BR>
value#=<b>Min</b>(a#,b#)<BR>
value$=<b>Min</b>(a$,b$)</P>
<P>
Similarly, the MIN function returns the smaller value of two expressions. Expressions can consist
of strings, integers or real numbers, but only compare like with like, as follows:</P>
<code class="prefix direct">A=Min(99,1) : Print A
Print Min("AMOS Professional","AMOS")
</code>
</section>
<section id="05-values-and-signs">
<h2>Values and signs</h2>
<p>Any number can have one of three values: negative, positive or zero, and these are represented
by the "sign" of a number.</P>
<h3 class="command" id="fn-sgn">SGN</h3>
<p><i>function: return the sign of a number</i><BR>
sign=<b>Sgn</b>(value)<BR>
sign=<b>Sgn</b>(value#)</P>
<P>
The SGN function returns a value representing the sign of a number. The three possible results
are these:</P>
<pre>
-1 if the value is negative
1 if the value is positive
0 if the value is zero
</pre>
<h3 class="command" id="fn-abs">ABS</h3>
<p><i>function: return an absolute value</i><BR>
a=<b>Abs</b>(value)<BR>
a=<b>Abs</b>(value#)</P>
<P>
This function is used to convert arguments into a positive number. ABS returns an
absolute value of an integer or fractional number, paying no attention to whether that number is
positive or negative, in other words, ignoring its sign.</P>
<P>For example:</P>
<code class="prefix direct">Print Abs(-1),Abs(1)</code>
</section>
<section id="06-floating-point-numbers">
<h2>Floating point numbers</h2>
<p>Numbers that consist of many digits either side of a decimal point can often give very
messy results in Basic programming. The movement of the decimal point slows down the processing,
and levels of accuracy may be too great for your needs.</P>
<h3 class="command" id="fn-int">INT</h3>
<p><i>function: convert floating point number into an integer</i><BR>
integer=<b>Int</b>(number#)</P>
<P>
The INT function rounds <b>down</b> a floating point number to the nearest whole number (integer),
so that the result of the following two example lines is 3 and -2, respectively:</P>
<code class="prefix direct">Print Int(3.9999)
Print Int(-1.1)
</code>
<h3 class="command" id="i-fix">FIX</h3>
<p><i>instruction: fix precision of floating point</i><BR>
<b>Fix</b>(number)</P>
<P>The FIX command changes the way floating point numbers are displayed on screen, or
output to a printer. The precision of these floating point numbers is determined by a number (n)
that is specified in brackets, and there can be four possibilities, as follows:</P>
<UL>
<LI>If (n) is greater than 0 and less than 16, the number of figures shown after the
decimal point will be n.
</LI>
<LI>If (n) equals 16 then the format is returned to normal.</LI>
<LI>If (n) is greater than 16, any trailing zeros will be removed and the display will
be proportional.
</LI>
<LI>If (n) is less than 0, the absolute value ABS(n) will determine the number of digits
after the decimal point, and all floating point numbers will be displayed in exponential format.
</LI>
</UL>
<P>Here are some examples:</P>
<code class="prefix edit">Fix (2) : Print Pi# : <comment>Rem Two digits after decimal point</comment>
Fix(-4) : Print Pi# : <comment>Rem Exponential with four digits after decimal point</comment>
Fix(16) : Print Pi# : <comment>Rem Revert to normal mode</comment>
</code>
</section>
<section id="07-single-and-double-precision">
<h2>Single and double precision</h2>
<p>Although the standard floating point system is perfect for general use, it may not be
accurate enough for genuine scientific applications, or advanced simulations. AMOS Professional
offers a choice of two separate calculation systems.</P>
<P><b>Single Precision</b><br>
This is the default mode, and is automatically used whenever an AMOS Professional
program is RUN. Single precision is accurate to about seven decimal digits, it is very fast
and it is ideal for the vast majority of applications.</P>
<P><b>Double precision</b><BR>
Double precision mode offers double the normal degree of accuracy, and is capable of
dealing with extremely precise values. Unlike most pocket calculators, AMOS Professional double
precision can handle numbers with up to 16 significant digits.</P>
<P>This extent of accuracy will consume twice as much memory as the standard version, and
it will also cause a great slowing down of calculations. It should only be used when extra
accuracy is absolutely vital.</P>
<h3 class="command" id="i-set-double-precision">SET DOUBLE PRECISION</h3>
<p><i>instruction: engage double precision accuracy</i><BR>
<b>Set Double Precision</b></P>
<P>
Double precision should be set at the start of your program, and all floating point
calculations will be performed using the more accurate mode. Because the two modes are completely
separate, single precision and double precision modes cannot be mixed in the same
program.</P>
</section>
<section id="08-standard-mathematical-functions">
<h2>Standard mathematical functions</h2>
<h3 class="command" id="fn-sqr">SQR</h3>
<p><i>function: calculate square root</i><BR>
square=<b>Sqr</b>(number)<BR>
square#=<b>Sqr</b>(number#)</P>
<P>
This function calculates the square root of a positive number, that is to say, it
returns a number that must be multiplied by itself to give the specified value. For example:</P>
<code class="prefix direct">Print Sqr(25)
Print Sqr(11.1111)
</code>
<h3 class="command" id="fn-exp">EXP</h3>
<p><i>function: calculate exponential</i><BR>
exponential#=<b>Exp</b>(value#)</P>
<P>Use the EXP function to return the exponential of a specified value. For example:</P>
<code class="prefix direct">Print Exp(1)</code>
<h3 class="command" id="fn-log">LOG</h3>
<p><i>function: return logarithm</i><BR>
a=<b>Log</b>(value)<BR>
a#=<b>Log</b>(value#)</P>
<P>LOG returns the logarithm in base 10 (log 10) of the given value. For example:</P>
<code class="prefix edit">Print Log(10)
A#=Log(100)
</code>
<h3 class="command" id="fn-ln">LN</h3>
<p><i>function: return natural logarithm</i><BR>
a#=<b>Ln</b>(value#)</P>
<P>
The LN Function calculates the natural logarithm (Naperian logarithm) of the given value.
For example:</P>
<code class="prefix edit">Print Ln(10)
A#=Ln(100) : Print A#
</code>
</section>
<section id="09-trigonometry">
<h2>Trigonometry</h2>
<p>The AMOS Professional trigonometric functions are often used for calculating angles,
creating graphic design effects, calculating trajectories in gameplay, as well as making intricate
musical wave forms.</P>
<h3 class="command" id="fn-pi-pound">PI#</h3>
<p><i>function: return a constant &pi</i><br>
p#=<b>Pi#</b></P>
<P>
Pi is the Greek letter it that is used to summon up a number which begins 3.141592653 and
on for ever. This number is the ratio of the circumference of a circle to its diameter, and it is used
in trigonometry as the tool for calculating aspects of circles and spheres. Note that in order to
avoid clashes with your own variable names, a # character is part of the token name. The PI#
function gives a constant value of Pi in your calculations.</P>
<P>
In the following diagram of a circle, a point is moved from the right hand side of the x-axis up
along the perimeter for a distance <b>a</b>, stopping at position <b>b</b>.</P>
<img src="images/524pi.jpg"
alt="Circle perimeter">
<p>
In conventional trigonometry, a circle is divided into 360 degrees, so <b>a</b> defines the
number of degrees in the angle between the x-axis and the line from the centre of the circle to
point <b>b</b>. However, your Amiga uses a default by which it expects all angles to be given in "radians"
and not degrees.
<h3 class="command" id="i-degree">DEGREE</h3>
<p><i>instruction: use degrees</i><BR>
<b>Degree</b></P>
<P>
If, for any reason, you are unhappy with the complexities of radians, AMOS Professional
is happy to accept your trigonometric instructions in degrees. Once the DEGREE command has
been activated, all subsequent calls to the trigonometric functions will expect degrees to
be used.</P>
<code class="prefix edit">Degree
Print Sin(45)
</code>
<h3 class="command" id="i-radian">RADIAN</h3>
<p><i>instruction: use radians</i><BR>
<b>Radian</b></P>
<P>If DEGREE has already been called, the RADIAN function returns to the default status,
where all future angles are expected to be entered in radians.</P>
<h3 class="command" id="fn-sin">SIN</h3>
<p><i>function: calculate sine of an angle</i><BR>
s#=<b>Sin</b>(angle)<BR>
s#=<b>Sin</b>(angle#)</P>
<P>The SIN function calculates how far point <b>b</b> is above the x-axis, known as the sine of
the angle <b>a</b>.<br>
Note that SIN always returns a floating point number. For example:</P>
<code class="prefix edit">Degree
For X=0 To 319
Y#=Sin(X)
Plot X,Y#*50+100
Next X
</code>
<h3 class="command" id="fn-cos">COS</h3>
<p><i>function: calculate cosine of an angle</i><BR>
c#=<b>Cos</b>(angle)<BR>
c#=<b>Cos</b>(angle#)</P>
<P>In the above diagram, the distance that point <b>b</b> is to the right of the y-axis is known
as the cosine. If <b>b</b> goes to the left of the y-axis, its cosine value becomes negative. (Similarly,
if it drops below the x-axis, its sine value is negative.) The COS function gives the cosine of a
given angle.<br>
To demonstrate this, add the following two lines to your last example between the PLOT and
NEXT instructions:</P>
<code class="prefix edit">Y#=Cos(X)
Plot X,Y#*50+100
</code>
<h3 class="command" id="fn-tan">TAN</h3>
<p><i>function: calculate tangent of an angle</i><BR>
t#=<b>Tan</b>(angle)<BR>
t#=<b>Tan</b>(angle#)</P>
<P>For any angle, the tangent is the result of when its sine is divided by its cosine. The
TAN function generates the tangent of a given angle. For example:</P>
<code class="prefix edit">Degree : Print Tan(45)
Radian : Print Tan(Pi#/8)
</code>
<h3 class="command" id="fn-acos">ACOS</h3>
<p><i>function: calculate arc cosine</i><BR>
a#=<b>Acos</b>(number#)</P>
<P>The ACOS function takes a number between -1 and +1, and calculates the angle which
would be needed to generate this value with COS. For example:</P>
<code class="prefix edit">A#=Cos(45)
Print Acos(A#)
</code>
<h3 class="command" id="fn-asin">ASIN</h3>
<p><i>function: calculate arc sine</i><BR>
a#=<b>Asin</b>(number#)</P>
<P>Similarly to ACOS, the ASIN function calculates the angle needed to generate a value
with SIN.
<h3 class="command" id="fn-atan">ATAN</h3>
<p><i>function: calculate arc tangent</i><BR>
a#=<b>Atan</b>(number#)</P>
<P>ATAN returns the arctan of a given number, like this:</P>
<code class="prefix edit">Degree : Print Tan(2)
Degree : Print Atan(0.03492082)
</code>
<P>
A hyperbola is a conical section, formed by a plane that cuts both bases of a cone. In
other words, an asymmetrical curve. Wave forms and trajectories are much more likely to follow
this sort of eccentric curve, than perfect arcs of circles. The hyperbolic functions
express the relationship between various distances of a point on the hyperbolic curve and the
coordinate axes.</P>
<h3 class="command" id="fn-hsin">HSIN</h3>
<p><i>function: calculate hyperbolic sine</i><BR>
h#=<b>Hsin</b>(angle)<BR>
h#=<b>Hsin</b>(angle#)</P>
<P>
The HSIN function calculates the hyperbolic sine of a given angle.</P>
<h3 class="command" id="fn-hcos">HCOS</h3>
<p><i>function: calculate hyperbolic cosine</i><BR>
h#=<b>Hcos</b>(angle)<BR>
h#=<b>Hcos</b>(angle#)</P>
<P>Use this function to find the hyperbolic cosine of an angle.</P>
<h3 class="command" id="fn-htan">HTAN</h3>
<p><i>function: calculate hyperbolic tangent</i><BR>
h#=<b>Htan</b>(angle)<BR>
h#=<b>Htan</b>(angle#)</P>
<P> HTAN returns the hyperbolic tangent of the given angle.</p>
</section>
<section id="10-random-numbers">
<h2>Random numbers</h2>
<p>The easiest way to introduce an element of chance or surprise into a program is to throw
some numbered options into an electronic pot and allow AMOS Professional to pull one out at
random. After a number has been selected and used, it is thrown back into the pot once
again. It then has the same chance as any other number offered for selection, when the next random
choice is made.</P>
<h3 class="command" id="fn-rnd">RND</h3>
<p><i>function: generate a random number</i><BR>
value=<b>Rnd</b>(number)</P>
<P>
The RND function generates integers at random, between zero and any number specified in
brackets. If your specified number is greater than zero, random numbers will be generated
up to that maximum number. However, if you specify 0, then RND will return the last random value
it generated. This is useful for debugging programs. Here is an example:</P>
<code class="prefix edit">Do
C=Rnd(15) : X=Rnd(320) : Y=Rnd(200)
Ink C : Text X,Y,"AMOS Professional at RANDOM"
Loop
</code>
<h3 class="command" id="i-randomize">RANDOMIZE</h3>
<p><i>instruction: set the seed for a random number</i><BR>
<b>Randomize</b> seed</P>
<P>
In practice, the numbers produced by the RND function are not genuinely random at all.
They are computed by an internal mathematical formula, whose starting point is taken from a
number known as a "seed". This seed is set to a standard value whenever AMOS Professional is
loaded into your Amiga, and that means that the sequence of numbers generated by the RND function
will be exactly the same each time your program is run.</P>
<!-- section -->
<P>
This may well be acceptable for arcade games, where pre-set random patterns generated by
RND can be used to advantage, but it is a useless system for more serious
applications.</P>
<P>
The RANDOMIZE command solves this problem by setting the value of the seed directly.
This seed can be any value you choose, and each seed will generate an individual sequence of
numbers. RANDOMIZE can also be used in conjunction with the TIMER variable, to generate
genuine random numbers.</P>
<h3 class="command" id="resv-timer">TIMER</h3>
<p><i>reserved variable: count in 50ths of a second</i><BR>
v=<b>Timer</b><BR>
<b>Timer</b>=v</P>
<P>
The TIMER reserved variable is incremented by 1 unit every 50th of a second, in other
words, it returns the amount of time that has elapsed since your Amiga was last switched on. As
explained above, this makes it a perfect "seed" to be used with the RANDOMIZE function, as
follows:</P>
<code class="prefix ex">Randomize Timer</code>
<P>
The best place to use this technique is immediately after the user has entered some
data into the computer. Even a simple key-press to start a game will work perfectly, and generate truly
random numbers.</P>
</section>
<footer>
<a href="05-02-string-functions.html" rel="prev">String Functions</a>
<a href="./">Contents</a>
<a href="14-appendix-g-command-index.html">Index</a>
<a href="05-04-control-structures.html" rel="next">Control Structures</a>
</footer>
</body>
</html>