🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #32

Re: F.N floating point format word

#32
From: peter
Date: Thu, 08 Oct 2026 20:07
567 lines
14123 bytes
On Thu, 8 Oct 2026 09:18:57 -0500
Krishna Myneni <krishna.myneni@ccreweb.org> wrote:

> On 10/8/26 8:26 AM, peter wrote:
> > On Thu, 8 Oct 2026 12:13:02 +0200
> > peter <peter.noreply@tin.it> wrote:
> > 
> >> On Thu, 8 Oct 2026 13:34:33 +1100
> >> dxf <dxforth@gmail.com> wrote:
> >>
> >>> On 8/10/2026 7:35 am, peter wrote:
> >>>> ...
> >>>> There were no real reason. The result at fpad is used only for
> >>>> INF and NAN. I also wanted a number larger then 17, 32 looked like
> >>>> a good round number. The importance of the first call to represent
> >>>> is to get a correct exp to use in the following calculations.
> >>>> The second time represent is called it will be with u=exp+n to get
> >>>> the correct rounding.
> >>>
> >>> Are you sure it's necessary?  Your tests appear to work with it set at 5.
> >>>
> >>
> >> You might be right!
> >> My first concern was for number like
> >> 9.9999999995e that will round up all the way and get a exp+1
> >> But it looks like the code handles that also.
> >>
> >> The problem was 99.995e 2 f.n that became 100.0
> >> But that was solved also by pre-filling the buffer with 0 and always
> >> printing the correct number of decimals
> >>
> >> BR
> >> Peter
> >>
> > 
> > Thanks to dxf's question I continued testing and found an error.
> > 
> > 0.999955555e 2 f.n  0.0100
> > 
> > What happened was that in this case exp from the first represent is 0
> > in the test to choose action f.n-case-e<=0 get chosen. In this part
> > exp is recalculated and became 1!
> > The correction is to move the second represent earlier before choosing.
> > 

> 
> Would you please attach the complete code when you get a chance since 
> several revisions have been made?
> 
> Also, I found the following issue with n < 0. It's possible I introduced 
> a bug in my revisions of your code, or that I missed some change.
> 
> 0.1e -1 (f.n) type 0.1 ok  \ should be "0."
> 0.01e -1 (f.n) type 0.01 ok \ should be "0."
> 0.1e -2 (f.n) type 0.1 ok  \ should be "0."
> 
> and similar.
> 
> --
> Krishna
> 
No they were actually designed to behave like that! I made it so that
when an impossible combination of exp and n were found n was set to a
value that showed the number with one significant digit. That is exactly
what happens in the above tests. But I agree with you when both exp and n
are negative n will be set as 0 and the number rounded to 1. or 0.
I have implemented that test now. I also restructured the code better
and combined 2 cases into one case normal.

Below follows code for both F.N and (F.N) tested on kForth64

BR
Peter

F.N
\ F.N  print float to n decimals

create fpad 1024 allot

: .sign ( f -- )
    13 and 32 + emit ;             \ prints bl for positive

: one-or-zero ( F:fp -- )           \ exp<=0 n=0 round to +-1 or 0
    fround>s 48 + emit ;
    
: f.n-case-00  ( F:fp  --  )        \ exp<=0 n=0 round to +-1 or 0
    one-or-zero 46 emit ;
     
: f.n-case-e+n<0 ( F:fp n  -- )
    fdrop
    ." 0." 
    0 ?do 48 emit loop ;

: f.n-case-e+n=0 ( F:fp n e -- )  
    fdup abs s>f falog f* fswap     \ prepare fp for rounding
    1- f.n-case-e+n<0               \ print 1 less zero
    one-or-zero ;

: f.n-case-normal ( F:fp n e -- )
    over +  1 max                   \ n e+n
    fpad 1024 48 fill
    fpad swap represent 2drop       \ n e'
    dup >r dup 0>                   \ n e' f r: e'
    if                              \ exp >0
        fpad swap type 
        dup 0 >= if 46 emit then  \ n
        fpad r> +                   \ n fpad+e'
        swap 0 max type 
    else                            \ exp <=0
        ." 0." abs 0 ?do 48 emit loop  
        fpad swap r> +              \ fpad n+e'
        type 
    then ;

    
: f.n ( F:fp n -- )
    fdup fpad 32 represent          \ find exp and sign
    swap .sign fabs                 \ print the sign
    0= if 2drop fdrop 
          fpad 3 type exit then     \ print nan or inf             
    over 0< if 
        dup 0 <= if nip 0 swap else  \ negative e and n
                swap over 1- negate max swap          \ limit negative n
        else swap over 1024 swap - min swap then then \ limit positive n   
    2dup 0 <= swap 0= and            \ n e f
    if 2drop   f.n-case-00 exit then
    2dup + 0<
    if drop f.n-case-e+n<0 exit then
    2dup + 0=
    if  f.n-case-e+n=0 exit then
    f.n-case-normal ;
     
\ 0 [IF]    
\ Test cases
: testing cr begin refill while source 2dup type space evaluate cr repeat ;
testing
0.9e  0 f.n
0.3e  0 f.n
-0.9e 0 f.n
-0.3e 0 f.n

0.123e   0 f.n
0.123e-1 1 f.n
0.123e-2 2 f.n
0.123e-3 3 f.n

0.823e   0 f.n
0.823e-1 1 f.n
0.823e-2 2 f.n
0.823e-3 3 f.n

 0.1368e    3 f.n
-0.1368e-1  3 f.n
 0.1368e-2  3 f.n
-0.1368e-3  3 f.n
 0.1368e-4  3 f.n

-0.8368e    3 f.n
-0.8368e-1  3 f.n
-0.8368e-2  3 f.n
-0.8368e-3  3 f.n
-0.8368e-4  3 f.n

126.8567e   3 f.n
126.8567e   2 f.n
126.8567e   1 f.n
126.8567e   0 f.n
126.8567e  -1 f.n
126.8567e  -2 f.n
126.8567e  -3 f.n

99.995e  3 f.n
99.995e  2 f.n
99.995e  1 f.n

0.999955555e 0 f.n
0.999955555e 1 f.n
0.999955555e 2 f.n
0.999955555e 3 f.n
0.999955555e 4 f.n
0.999955555e 5 f.n

5.2345678e-1  10 f.n
5.2345678e-2  10 f.n
5.2345678e-3  10 f.n
5.2345678e-4  10 f.n
5.2345678e-5  10 f.n
5.2345678e-6  10 f.n
5.2345678e-7  10 f.n
5.2345678e-8  10 f.n
5.2345678e-9  10 f.n
5.2345678e-10 10 f.n
5.2345678e-11 10 f.n
5.2345678e-12 10 f.n

-1e 0e f/ 5 f.n
 0e 0e f/ 5 f.n

\ Krishna Myneni tests

0.0051e  0 f.n
0.0051e  1 f.n
0.0051e  2 f.n
0.0051e  3 f.n
0.0051e  4 f.n
0.0051e  5 f.n
0.0051e 17 f.n
2.0e-15 17 f.n

0.0e  0 f.n
0.1e  0 f.n
0.2e  0 f.n
0.4e  0 f.n
0.5e  0 f.n
0.51e 0 f.n

0.500000000000000001e 0 f.n
0.50000000000000001e  0 f.n
0.5000000000000001e   0 f.n

0.6e 0 f.n
0.9e 0 f.n
1.0e 0 f.n
1.4e 0 f.n
1.4999999e 0 f.n
1.5e 0 f.n
2.5e 0 f.n
3.5e 0 f.n

-1100.2e 0 f.n
-1100.5e 0 f.n
-1101.5e 0 f.n
-1102.5e 0 f.n

2.01682663070034556e16  0 f.n
2.01682663070034556e16 20 f.n

0.5e   25 f.n
0.625e 25 f.n
0.625e 25 f.n

-0.6e  4 f.n
-0.6e 16 f.n
-0.6e 17 f.n
-0.6e 23 f.n
-0.6e 25 f.n

9.1e   3 f.n
9.51e  1 f.n
9.55e  1 f.n
-9.66e 1 f.n

99.995e 2 f.n
99.625e 2 f.n
96.62500000000001e 2 f.n

1.247777e3 0 f.n
1.247777e3 1 f.n
1.247777e3 2 f.n
1.247777e3 3 f.n

-5.0999913335e0 4 f.n
-5.0999913335e0 5 f.n
-5.0999913335e0 6 f.n
-5.0999913335e0 7 f.n
-5.0999913335e0 8 f.n
-5.0999913335e0 9 f.n

9.599999999e8   0 f.n

0.1e   -1 f.n
0.1e   -2 f.n
0.01e  -1 f.n

0.75e  -1 f.n
0.75e  -2 f.n
0.075e -1 f.n

\ test large arguments
123e300  10  f.n
123e-300 305 f.n
123e-30   40 f.n
\ end of tests
\ [THEN]

Here is the code for (F.N)

\ (F.N) print float with n decimals to string

\ Cell counted string words
-1 [if]
: xcount ( addr -- addr+cell count )
    dup @ swap cell+ swap ; 
    
: xappend ( addr n addr2 -- ) \ append string to string at addr2
    2dup 2>r
    xcount + swap cmove
    r> xcount r> + swap 1 cells - ! ;
    
: xplace ( addr n addr2 -- ) \ place string to string at addr2    
    dup 0 swap !
    xappend ;

: cxappend ( c addr2 -- ) \ append c to end of string at addr2    
    dup >r xcount + c! 
    1 r> +! ;
    
: cxfill ( n c addr2 -- ) \ append n cs to the end of string at addr2
    >r over r@ swap >r 
    xcount + -rot fill
    r> r> +! ;

\ redirect output to memory

VARIABLE (string-buf)

: sbuf  (string-buf) @ ;

: sbuf> sbuf xcount ;

: init-string-buf ( addr -- addr-old)   \ retrieve addr-old from string-buf, place addr there
    (string-buf) @ swap
    dup 0 swap ! (string-buf) ! ;
    
: >sbuf>  ( addr-old -- addr len ) \ retrieve addr len and set string-buf to old value
    >r sbuf xcount
    r> (string-buf) ! ;

: semit  ( c -- )	\ type to buffer
    sbuf cxappend ;
    
: stype ( addr len -- )
	sbuf xappend ;
    
: sfill ( n c -- ) \ fill buffer with n cs
 \   over 0= if 2drop exit then
    sbuf cxfill ;

: nl   10 semit ; 

: s.  s>d swap over dabs <# #s rot sign #> stype ;

: strbuf 1024 allocate throw ;

[then]
\ (F.N) print float to n decimals

create fpad 1024 allot

: .sign ( f -- )
    13 and 32 + semit ;             \ prints bl for positive
\    if 45 semit then ;             \ use this to only print -

: .zeros ( n -- )
    48 sfill ;
    
: s"0."  s" 0." stype ;
    
: one-or-zero ( F:fp -- )           \ exp<=0 n=0 round to +-1 or 0
    fround>s 48 + semit ;
    
: f.n-case-00  ( F:fp  --  )        \ exp<=0 n=0 round to +-1 or 0
    one-or-zero 46 semit ;
     
: f.n-case-e+n<0 ( F:fp n  -- )
    fdrop
    s"0." .zeros ;

: f.n-case-e+n=0 ( F:fp n e -- )  
    fdup abs s>f falog f* fswap     \ prepare fp for rounding
    1- f.n-case-e+n<0               \ print 1 less zero
    one-or-zero ;

: f.n-case-normal ( F:fp n e -- )
    over +  1 max                   \ n e+n
    fpad 1024 48 fill
    fpad swap represent 2drop       \ n e'
    dup >r dup 0>                   \ n e' f r: e'
    if                              \ exp >0
        fpad swap stype 
        dup 0 >= if 46 semit then  \ n
        fpad r> +                   \ n fpad+e'
        swap 0 max stype 
    else                            \ exp <=0
        s"0." abs .zeros  
        fpad swap r> +              \ fpad n+e'
        stype 
    then ;
    
: (f.n) ( F:fp n -- )
    strbuf init-string-buf >r
    fdup fpad 32 represent          \ find exp and sign
    swap .sign fabs                 \ print the sign
    0= if 2drop fdrop 
          fpad 3 stype else         \ print nan or inf             
    over 0< if 
        dup 0 <= if nip 0 swap else  \ negative e and n
                 swap over 1- negate max swap             \ limit negative n
            else swap over 1024 swap - min swap then then \ limit positive n 
    2dup 0 <= swap 0= and           \ n e f
    if 2drop   f.n-case-00 else
    2dup + 0<
    if drop f.n-case-e+n<0 else
    2dup + 0=
    if  f.n-case-e+n=0 else
        f.n-case-normal
    then then then then  r> >sbuf> ;

: f.n  (f.n) type space ;


    
\ 0 [IF]    
\ Test cases
: testing cr begin refill while source 2dup type space evaluate cr repeat ;
testing
0.9e  0 f.n
0.3e  0 f.n
-0.9e 0 f.n
-0.3e 0 f.n

0.123e   0 f.n
0.123e-1 1 f.n
0.123e-2 2 f.n
0.123e-3 3 f.n

0.823e   0 f.n
0.823e-1 1 f.n
0.823e-2 2 f.n
0.823e-3 3 f.n

 0.1368e    3 f.n
-0.1368e-1  3 f.n
 0.1368e-2  3 f.n
-0.1368e-3  3 f.n
 0.1368e-4  3 f.n

-0.8368e    3 f.n
-0.8368e-1  3 f.n
-0.8368e-2  3 f.n
-0.8368e-3  3 f.n
-0.8368e-4  3 f.n

126.8567e   3 f.n
126.8567e   2 f.n
126.8567e   1 f.n
126.8567e   0 f.n
126.8567e  -1 f.n
126.8567e  -2 f.n
126.8567e  -3 f.n

99.995e  3 f.n
99.995e  2 f.n
99.995e  1 f.n

0.999955555e 0 f.n
0.999955555e 1 f.n
0.999955555e 2 f.n
0.999955555e 3 f.n
0.999955555e 4 f.n
0.999955555e 5 f.n

5.2345678e-1  10 f.n
5.2345678e-2  10 f.n
5.2345678e-3  10 f.n
5.2345678e-4  10 f.n
5.2345678e-5  10 f.n
5.2345678e-6  10 f.n
5.2345678e-7  10 f.n
5.2345678e-8  10 f.n
5.2345678e-9  10 f.n
5.2345678e-10 10 f.n
5.2345678e-11 10 f.n
5.2345678e-12 10 f.n

-1e 0e f/ 5 f.n
 0e 0e f/ 5 f.n

\ Krishna Myneni tests

0.0051e  0 f.n
0.0051e  1 f.n
0.0051e  2 f.n
0.0051e  3 f.n
0.0051e  4 f.n
0.0051e  5 f.n
0.0051e 17 f.n
2.0e-15 17 f.n

0.0e  0 f.n
0.1e  0 f.n
0.2e  0 f.n
0.4e  0 f.n
0.5e  0 f.n
0.51e 0 f.n

0.500000000000000001e 0 f.n
0.50000000000000001e  0 f.n
0.5000000000000001e   0 f.n

0.6e 0 f.n
0.9e 0 f.n
1.0e 0 f.n
1.4e 0 f.n
1.4999999e 0 f.n
1.5e 0 f.n
2.5e 0 f.n
3.5e 0 f.n

-1100.2e 0 f.n
-1100.5e 0 f.n
-1101.5e 0 f.n
-1102.5e 0 f.n

2.01682663070034556e16  0 f.n
2.01682663070034556e16 20 f.n

0.5e   25 f.n
0.625e 25 f.n
0.625e 25 f.n

-0.6e  4 f.n
-0.6e 16 f.n
-0.6e 17 f.n
-0.6e 23 f.n
-0.6e 25 f.n

9.1e   3 f.n
9.51e  1 f.n
9.55e  1 f.n
-9.66e 1 f.n

99.995e 2 f.n
99.625e 2 f.n
96.62500000000001e 2 f.n

1.247777e3 0 f.n
1.247777e3 1 f.n
1.247777e3 2 f.n
1.247777e3 3 f.n

-5.0999913335e0 4 f.n
-5.0999913335e0 5 f.n
-5.0999913335e0 6 f.n
-5.0999913335e0 7 f.n
-5.0999913335e0 8 f.n
-5.0999913335e0 9 f.n

9.599999999e8   0 f.n

0.1e   -1 f.n
0.1e   -2 f.n
0.01e  -1 f.n

0.75e  -1 f.n
0.75e  -2 f.n
0.075e -1 f.n

\ test large arguments
123e300  10  f.n
123e-300 305 f.n
\ [THEN]




Technical Details
Message-ID: <20261008220746.0000042f@tin.it>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <119oin2$26n99$1@dont-email.me> <11a01mr$sqrm$1@dont-email.me> <20261005150222.00006cf5@tin.it> <11a16nc$1camn$1@dont-email.me> <20261006080347.00002cdf@tin.it> <11a4tj5$2ku7v$1@dont-email.me> <20261007223526.000065ab@tin.it> <6ac7013a$1@news.ausics.net> <20261008121302.00007412@tin.it> <20261008152645.00001682@tin.it> <11a88oh$3t0ti$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: peter <peter.noreply@tin.it>
Newsgroups: comp.lang.forth
Subject: Re: F.N floating point format word
Date: Thu, 8 Oct 2026 22:07:46 +0200
Organization: A noiseless patient Spider
Lines: 567
Message-ID: <20261008220746.0000042f@tin.it>
References: <119oin2$26n99$1@dont-email.me>
	<11a01mr$sqrm$1@dont-email.me>
	<20261005150222.00006cf5@tin.it>
	<11a16nc$1camn$1@dont-email.me>
	<20261006080347.00002cdf@tin.it>
	<11a4tj5$2ku7v$1@dont-email.me>
	<20261007223526.000065ab@tin.it>
	<6ac7013a$1@news.ausics.net>
	<20261008121302.00007412@tin.it>
	<20261008152645.00001682@tin.it>
	<11a88oh$3t0ti$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 08 Oct 2026 20:07:48 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="4179027"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/t9knE9dJYkz3M33vcdQZ532bjmYNaTjM="; posting-host="dd63b567af524a9d4c3f6459d93aa223"
Cancel-Lock: sha1:lCenLUnLMPE2QxNInjeerzoNHK0= sha256:kffU1lDmiJNxN4vbtxxe9/MLNHmdH3MsavPNc3RcgSQ=
	sha1:a3YkeKoooX6ADQ54HIO67XFijbs= sha256:mjCVmBtBBfrd8XWTWJ0OAUxqUh8kRfHoVec4+cc8EmI=
X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)