Article View: comp.lang.forth
Article #29Re: F.N floating point format word
From: peter
Date: Thu, 08 Oct 2026 13:26
Date: Thu, 08 Oct 2026 13:26
90 lines
4286 bytes
4286 bytes
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.
the relevant code will become
: f.n-case-e>0 ( F:fp n e -- )
\ 3 lines deleted
dup >r \ n e' r: e'
fpad swap type
dup 0 >= if '.' emit then \ n
fpad r> + \ n fpad+e'
swap 0 max type ;
: f.n-case-e<=0 ( F:fp n e -- )
\ 3 lines deleted
dup >r \ n e' r: e'
." 0." abs 0 ?do '0' emit loop
fpad swap r> + \ fpad n+e'
type ;
: f.n ( F:fp n -- )
fdup fpad 32 represent \ find exp and sign
swap .sign fabs \ if '-' emit fabs then \ print the sign
0= if 2drop fdrop
fpad 3 type exit then \ print nan or inf
over 0< if
swap over 1- negate max swap else \ limit negative n
swap over 1024 swap - min swap 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
\ the 3 lines added here
over + 1 max \ n e+n
fpad 1024 '0' fill
fpad swap represent 2drop \ n e'
dup 0>
if f.n-case-e>0 exit then
f.n-case-e<=0 ;
The same changes need to be done on the (f.n) words
Now also this case will round correctly
0.999955555e 2 f.n 1.00
BR
Peter
Technical Details
Message-ID:
<20261008152645.00001682@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-mailReferences:
<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>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 15:26:45 +0200 Organization: A noiseless patient Spider Lines: 90 Message-ID: <20261008152645.00001682@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> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Injection-Date: Thu, 08 Oct 2026 13:26:46 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3990949"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+yEyHelNv1MOduhaLWWmiu26MJvcfATQs="; posting-host="dd63b567af524a9d4c3f6459d93aa223" Cancel-Lock: sha1:SU2ZSP3cYvFr+pwku3BXov0lZyM= sha256:KS+raqg4Oxh85bGEfR2AhnHI4eg/KtQmbqV4B9blqdw= sha1:VEE4shAQYZgBm6GIGjln5Vl2BMw= sha256:bpHZaq+sdsx58F1uTwGxEXUKjNqeDDX58zPM683BOmk= X-Newsreader: Claws Mail 4.4.0 (GTK 3.24.51; x86_64-w64-mingw32)