home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
source
/
p2_17jb.seq
< prev
next >
Wrap
Text File
|
1990-04-08
|
2KB
|
56 lines
: TRIANGLE_AREA ( b h --)
2DUP SWAP
CR ." Base = " .
." Height = " .
* 2 /MOD
." Area = " 5 .R
\ 1 BACKSPACES ( 8 EMIT does not back up cursor in F-PC)
ASCII . EMIT
IF ASCII 5 EMIT
ELSE ASCII 0 EMIT
THEN ;
COMMENT:
Problem 2.17.
a) What do you observe when you use 5 and 3 for the base and height?
b) What do you observe when you use 5 and 4 for the base and height?
c) Explain in detail why you get the result you see for question 1.
d) Could you modify the idea contained in the above to compute the Area
of a circle to three decimal places? Hint: It can be done quite easily
and if you can do it you have the potential to become a Forth Super Star!
Solution to Problem 2.17
a) What do you observe for output of triangle program
with 5 3 AREA? Answer:
Base = 5 Height = 3 Area = 7.5
b) What about 5 4 AREA ? Answer:
Base = 5 Height = 4 Area = 10.0
c) Why the decimal result for question one when we only have
integer math? Answer: Because if the remainder from the
/MOD operation is 1 we can display the .5, if the remainder
is 0 then we can display the required .0
d) Computer area of a circle to three decimal places.
COMMENT;
\ Compute area of a circle to three decimal places.
: CIRCLE_AREA ( r -- )
DUP * 355 * 113 /MOD
." Area = " 5 .R \ ( 8 EMIT ) 1 BACKSPACES
ASCII . EMIT
5 0 DO
10 * 113 /MOD 1 .R \ ( 8 EMIT ) 1 BACKSPACES
LOOP DROP ;
: TEST_CIRC ( -- )
11 1 DO CR I CIRCLE_AREA LOOP ;