Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!willett!ForthNet From: ForthNet@willett.UUCP (ForthNet articles from GEnie) Newsgroups: comp.lang.forth Subject: F-PC Forth Tutorial Message-ID: <760.UUL1.3#5129@willett.UUCP> Date: 11 Apr 90 01:57:10 GMT Organization: Latest link in the ForthNet chain. (Pgh, PA) Lines: 64 Date: 04-08-90 (10:23) Number: 511 (Echo) To: PAUL BRANNON Refer#: 509 From: JACK BROWN Read: NO Subj: RELEARNING FORTH Status: PUBLIC MESSAGE >end of lesson 2. The word below is my attempt to calculate the area of >a circle. It works except for printing the decimal point. Also I could >not round the answer when appropriate. Any suggestions or comments? > \ word for calculating circle area to 3 decimals > : circle_area ( r -- ) > dup * 355 * 113 /mod > ." area = " . ascii . emit > 5 0 do > 10 * 113 /mod 8 emit . > loop drop ; Hi Paul, I was thinking about your solution and really like your application of /MOD to compute each decimal digit. I don't really like the 1 BACKSPACES required by F-PC in place of 8 EMIT because although clear it is not Forth 83 Standard. Also the use of */MOD in the first line of CIRC_AREA will allow a greater range of radii. To get around the 1 BACKSPACES we can use the right justified display operator which we present in lesson 3. \ Alternate to Problem 2.17 (d) \ In this solution we use the right justified display operator .R \ to avoid the use of 1 BACKSPACES which was used to back up over \ the trailing blank that . provides. \ We have also replaced the phrase 355 * 113 /MOD \ with the phrase 355 113 */MOD for a greater range of radii. \ */MOD will first multiply by 355 leaving a 32 bit intermediate \ product and then divide this 32 bit product by 113. \ 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 ; I just thought of an even better solution than using 1 .R How about just replacing 1. R with 48 + EMIT Can you explain why this will work?? NET/Mail : British Columbia Forth Board - Burnaby BC - (604)434-5886 ----- This message came from GEnie via willett through a semi-automated process. Report problems to: 'uunet!willett!dwp' or 'willett!dwp@gateway.sei.cmu.edu'