F.R and FS.R words? 
Author Message
 F.R and FS.R words?

My floating point wordset has F. and FS.  Can anyone provide some sample code
for corresponding functions that uses REPRESENT and will print fp numbers
suitable for listing in columns (i.e. right-justified in a given field width
and with the decimal point aligned).

(I tried...and gave up.  I suspect it would take at least half a dozen screens
to get working properly.  Best of luck!)



Thu, 02 Mar 2000 03:00:00 GMT  
 F.R and FS.R words?


Quote:
> My floating point wordset has F. and FS.  Can anyone provide some sample code
> for corresponding functions that uses REPRESENT and will print fp numbers
> suitable for listing in columns (i.e. right-justified in a given field width
> and with the decimal point aligned).

I remember this was a very {*filter*}e to write, plus that it turned up one of the
latest (known) bugs in iForth :-)

Quote:
> (I tried...and gave up.  I suspect it would take at least half a dozen screens
> to get working properly.  Best of luck!)

This one does both too much and too less, but it's a start. Written so that
output to disk is easily accomplished by revectoring EMIT etc. (PUTS , PUTCH).
You may want to pull the hard fieldsize constants ( 4, 3, 2) out of it.

I hope you have (UD.R) . (Mine has an undocumented feature when the field is
specified as being negative, it then fills the field with 0's). $+ can be done
with Wil Baden's S+ .

-marcel
---

: NUM>       ( F: r -- )
        PAD 4 REPRESENT
        0= IF 2DROP PAD 4
         ELSE IF S" -" ELSE S"  " ENDIF
              PAD 1 $+ S" ." $+ PAD 1+ 3 $+
              ROT 1- DUP >S 0< IF S" e-" ELSE S" e+" ENDIF $+
              S> ABS U>D -2 (UD.R) $+
        ENDIF PUTS BL PUTCH ;p



Fri, 03 Mar 2000 03:00:00 GMT  
 F.R and FS.R words?

MH> This one does both too much and too less, but it's a start. Written so
MH> that output to disk is easily accomplished by revectoring EMIT etc.

Your code is quite short thanks to $+ .  (I've managed to avoid $+ or S+
in my forth kernel thus far andso prefer not to use them - CP/M systems
don't have much memory to spare).  Btw, NUM> has the usual 'quirk' of
displaying 0.0E as 0.000e-01 which is mathematically correct but looks
odd.

The following is my existing code for FS. and F.  It's mostly Forth-83
and will need some adjustment for ANS.  While it's compact, it is not
easy to add 'right-justified' output functions to it - something which I
consider essential to any FP package (I can't believe ANS-FORTH left
them out!).

 \ FP Output - FS. F.  (warning: not ANS compatible)

 CREATE FBUF  7 ALLOT  ( max precision)

 : (F.)  ( r -- exp flag )
   FDUP F0= >R  FBUF PRECISION REPRESENT  DROP
   IF ." - " THEN R> ;

 0 \ fool fig-forth style compiler security
 : FS.  ( r -- )
   (F.) [ DROP HERE ]
   FBUF COUNT EMIT ." ." PRECISION 1- TYPE ." E" 0= + . ;

 : F.  ( r -- )
   (F.)  OVER 0 PRECISION 1+ WITHIN  [COMPILE] ?BRANCH [ , 0 ]
   - DUP 0< NOT IF FBUF OVER TYPE THEN
   ." ." FBUF PRECISION  ROT /STRING TYPE SPACE ;
 DROP

___--

The following is a replacement I've been working on to give FS.R and F.R
functions in addition.

F.R is very lame, however - a proper version should have another
parameter i.e. F.R ( r places fieldwidth -- ) where 'places' is the
number of places after the decimal point ... but I'm finding it very
hard to write a decent F.R, hence my request for help.

Perhaps having a wordset similar to <# # #> etc to build FP output
strings is the way to go?

 \ FP Output -  FS.R FS. F.R F.  (warning: not ANS compatible)

 CREATE FBUF  13 ALLOT  \  string buffer, max precision + 6

 \ Insert decimal point into string
 : !PT  ( n -- )

 \ Convert exponent into string and insert
 : !EXP  ( exp -- )
   S>D SWAP OVER DABS <# # # ROT 0< IF [CHAR] - ELSE [CHAR] +
   THEN HOLD  [CHAR] E HOLD #> FBUF 2+ PRECISION + SWAP CMOVE ;

 : (F1)  ( r width -- width exp )
   >R  FDUP F0= >R  FBUF  DUP 13 BLANK
   2+ PRECISION REPRESENT DROP  IF [CHAR] - FBUF C! THEN
   2R> 0= ROT + ( adj exp if not zero) ;

 \ Display string, right aligned
 : (F2)  ( width -- )
   FBUF 13  BL SKIP  -TRAILING  ROT OVER - SPACES TYPE ;

 : FS.R  ( r width -- )
   (F1)  !EXP  1  !PT  (F2) ;

 : FS.  ( r -- )  0 FS.R SPACE ;

 : F.R  ( r width -- )
   (F1)  DUP -1 PRECISION WITHIN
   IF  1+  ELSE  !EXP  1  THEN  !PT (F2) ;

 : F.  ( r -- )  0 F.R SPACE ;

___--
___
 X SLMR 2.1 X



Thu, 09 Mar 2000 03:00:00 GMT  
 F.R and FS.R words?


Quote:
> MH> This one does both too much and too less, but it's a start. Written so
> MH> that output to disk is easily accomplished by revectoring EMIT etc.

> Your code is quite short thanks to $+ .  (I've managed to avoid $+ or S+
> in my forth kernel thus far andso prefer not to use them - CP/M systems
> don't have much memory to spare).

Well, I'm quite pleased with $+ . It is not an iForth kernel word (neither
are most fp formatting words), but it makes a lot of tasks surprisingly easy.

Quote:
>                                    Btw, NUM> has the usual 'quirk' of
> displaying 0.0E as 0.000e-01 which is mathematically correct but looks
> odd.

This depends on context. That is, in a table, or a disk file, it won't be
noticed (0.000e-03 is less than 1e-7 and has been computed with a 32-bit
fp package, 0e is ??). When labeling a graph this notation is an eye sore.
I have not found a good solution for labeling and (auto-)scaling yet. Each
use seems to need its own approach.

Quote:
> The following is my existing code for FS. and F.  It's mostly Forth-83
> and will need some adjustment for ANS.  While it's compact, it is not
> easy to add 'right-justified' output functions to it - something which I
> consider essential to any FP package (I can't believe ANS-FORTH left
> them out!).

ANS Forth does not provide the kitchen sink. It delivers a minimum functionality
( FS. ) plus the possibility to write something extremely fancy ( REPRESENT ).
Anything inbetween is trimmed out.

[ .. ]

Quote:
> 0 \ fool fig-forth style compiler security

Aha, I see Chuck Moore's good examples spreading out :-)

[ .. ]

-marcel



Fri, 10 Mar 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Testing CORE Words (coretest.fr)

2. Testing CORE Words (coretest.fr)

3. Testing Core Words (tester.fr)

4. How to replace one or two words with one word with one line of awk code

5. unsigned word to signed word in RB

6. Word 97 and word 2000 - calling from clarion - HELP

7. Word by word cross reference

8. More MF.Fun WORD-RIGHT and WORD-LEFT

9. Class(y) 2.4b, with documentation for MS-Word 97, MS-Word 6.0 and WordPerfect 7.0

10. My final word on word size

11. Adding a signed word to unsigned word...

12. copy replacing part of words, instead of complete word

 

 
Powered by phpBB® Forum Software