Thread View: comp.lang.forth
39 messages
39 total messages
Started by Krishna Myneni
Wed, 07 Oct 2026 07:49
Re: F.N floating point format word
Author: Krishna Myneni
Date: Wed, 07 Oct 2026 07:49
Date: Wed, 07 Oct 2026 07:49
49 lines
2882 bytes
2882 bytes
On 10/6/26 1:03 AM, peter wrote: > On Mon, 5 Oct 2026 17:01:14 -0500 ... > If you are working on (f.n) that I posted there are 2 changes I have made > later. > > f.n-case-e>0 should be > > : f.n-case-e>0 ( F:fp n e -- ) > over + \ n e+n > fpad 1024 '0' fill > fpad swap represent 2drop \ n e' > dup >r \ n e' r: e' > fpad swap stype > dup 0 >= if 46 semit then \ n > fpad r> + \ n fpad+e' > swap 0 max stype ; > > This will prevent the dot to be printed at the end when using negative n. > > The other is in a comment > > : .sign ( f -- ) > 13 and 32 + semit ; \ prints bl for positive > \ if 45 semit then ; \ use this to only print - > > The code I posted have 32 in the commented out part! > It would print a blank instead of a - > ... In the definition of (F.N) you use an argument of u = 32 significant digits to REPRESENT. I did not understand why you chose this particular number to obtain the decimal exponent (exp). I changed this to 17 and all of the tests in fdotn-test.4th still pass. : (f.n) ... fdup fabs fswap fpad ( 32) 17 represent \ find exp and sign Since binary -> decimal conversion with 17 significant digits is guaranteed to cycle in a round trip for binary64, I think 17 will suffice here for obtaining the correct decimal exponent. -- Krishna -- Krishna
Re: F.N floating point format word
Author: Anton Ertl
Date: Wed, 07 Oct 2026 08:10
Date: Wed, 07 Oct 2026 08:10
18 lines
2024 bytes
2024 bytes
Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>Since binary -> decimal conversion with 17 significant digits is
>guaranteed to cycle in a round trip for binary64
It will give you a binary->decimal->binary round trip, but the decimal
number is not always the shortest one that gives this FP number. E.g.:
0.9e 17 e.p \ .90000000000000002e
while obviously "0.9e" already gives you this FP number. E. tries
15-17 digits, and outputs the first one where the round trip works.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Re: F.N floating point format word
Author: Krishna Myneni
Date: Wed, 07 Oct 2026 13:21
Date: Wed, 07 Oct 2026 13:21
23 lines
2241 bytes
2241 bytes
On 10/7/26 3:10 AM, Anton Ertl wrote: > Krishna Myneni <krishna.myneni@ccreweb.org> writes: >> Since binary -> decimal conversion with 17 significant digits is >> guaranteed to cycle in a round trip for binary64 > > It will give you a binary->decimal->binary round trip, but the decimal > number is not always the shortest one that gives this FP number. E.g.: > > 0.9e 17 e.p \ .90000000000000002e > > while obviously "0.9e" already gives you this FP number. E. tries > 15-17 digits, and outputs the first one where the round trip works. > Ok, but that's not what's needed for implementing F.N where the number of decimal places, to which the output is rounded, is specified. IOW it will not be equivalent to the Fortran F format. -- KM
Re: F.N floating point format word
Author: peter
Date: Wed, 07 Oct 2026 20:35
Date: Wed, 07 Oct 2026 20:35
63 lines
3426 bytes
3426 bytes
On Wed, 7 Oct 2026 02:49:57 -0500 Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > On 10/6/26 1:03 AM, peter wrote: > > On Mon, 5 Oct 2026 17:01:14 -0500 > ... > > If you are working on (f.n) that I posted there are 2 changes I have made > > later. > > > > f.n-case-e>0 should be > > > > : f.n-case-e>0 ( F:fp n e -- ) > > over + \ n e+n > > fpad 1024 '0' fill > > fpad swap represent 2drop \ n e' > > dup >r \ n e' r: e' > > fpad swap stype > > dup 0 >= if 46 semit then \ n > > fpad r> + \ n fpad+e' > > swap 0 max stype ; > > > > This will prevent the dot to be printed at the end when using negative n. > > > > The other is in a comment > > > > : .sign ( f -- ) > > 13 and 32 + semit ; \ prints bl for positive > > \ if 45 semit then ; \ use this to only print - > > > > The code I posted have 32 in the commented out part! > > It would print a blank instead of a - > > ... > > In the definition of (F.N) you use an argument of u = 32 significant > digits to REPRESENT. I did not understand why you chose this particular > number to obtain the decimal exponent (exp). I changed this to 17 and > all of the tests in fdotn-test.4th still pass. > > : (f.n) ... > fdup fabs fswap fpad ( 32) 17 represent \ find exp and sign > > Since binary -> decimal conversion with 17 significant digits is > guaranteed to cycle in a round trip for binary64, I think 17 will > suffice here for obtaining the correct decimal exponent. 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. Peter > -- > Krishna > > > -- > Krishna >
Re: F.N floating point format word
Author: Krishna Myneni
Date: Wed, 07 Oct 2026 21:59
Date: Wed, 07 Oct 2026 21:59
34 lines
2780 bytes
2780 bytes
On 10/7/26 3:35 PM, peter wrote: > On Wed, 7 Oct 2026 02:49:57 -0500 > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > ... >> >> In the definition of (F.N) you use an argument of u = 32 significant >> digits to REPRESENT. I did not understand why you chose this particular >> number to obtain the decimal exponent (exp). I changed this to 17 and >> all of the tests in fdotn-test.4th still pass. >> >> : (f.n) ... >> fdup fabs fswap fpad ( 32) 17 represent \ find exp and sign >> >> Since binary -> decimal conversion with 17 significant digits is >> guaranteed to cycle in a round trip for binary64, I think 17 will >> suffice here for obtaining the correct decimal exponent. > > 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. > There may be an efficiency gain to going with the minimum number of significant digits to get the exponent. I haven't benchmarked it, but big number arithmetic comes into play when dealing with such a large number of significant digits. -- KM
Re: F.N floating point format word
Author: Krishna Myneni
Date: Thu, 08 Oct 2026 02:17
Date: Thu, 08 Oct 2026 02:17
47 lines
3411 bytes
3411 bytes
On 10/7/26 4:59 PM, Krishna Myneni wrote: > On 10/7/26 3:35 PM, peter wrote: >> On Wed, 7 Oct 2026 02:49:57 -0500 >> Krishna Myneni <krishna.myneni@ccreweb.org> wrote: >> > ... >>> >>> In the definition of (F.N) you use an argument of u = 32 significant >>> digits to REPRESENT. I did not understand why you chose this particular >>> number to obtain the decimal exponent (exp). I changed this to 17 and >>> all of the tests in fdotn-test.4th still pass. >>> >>> : (f.n) ... >>> fdup fabs fswap fpad ( 32) 17 represent \ find exp and sign >>> >>> Since binary -> decimal conversion with 17 significant digits is >>> guaranteed to cycle in a round trip for binary64, I think 17 will >>> suffice here for obtaining the correct decimal exponent. >> >> 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. >> > > There may be an efficiency gain to going with the minimum number of > significant digits to get the exponent. I haven't benchmarked it, but > big number arithmetic comes into play when dealing with such a large > number of significant digits. > Initial tests show there is no appreciable difference in execution time between using 32 significant digits and 17 significant digits in the initial call to REPRESENT for benchmarking (F.N). Also, the F>FPSTR method of obtaining the string to n decimal places is approximately twice as fast as the REPRESENT-based (F.N) method, but the latter is more versatile (works over a larger range of n). Incidentally, I replaced raising 10.0e0 to the n^th power via F** with FALOG in the defn of F>FPSTR. -- KM
Re: F.N floating point format word
Author: dxf
Date: Thu, 08 Oct 2026 02:34
Date: Thu, 08 Oct 2026 02:34
11 lines
1458 bytes
1458 bytes
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.
Re: F.N floating point format word
Author: Krishna Myneni
Date: Thu, 08 Oct 2026 02:51
Date: Thu, 08 Oct 2026 02:51
27 lines
2547 bytes
2547 bytes
On 10/7/26 9:17 PM, Krishna Myneni wrote: > On 10/7/26 4:59 PM, Krishna Myneni wrote: ... > Also, the F>FPSTR method of obtaining the string to n decimal places is > approximately twice as fast as the REPRESENT-based (F.N) method, but the > latter is more versatile (works over a larger range of n). ... Also, the ability to use -n to round the integer part of a number to n significant digits is awesome! Example: 1906.0e 0 f.n 1906. ok \ round to 1's place, 4 sig digits 1906.0e -1 f.n 1910 ok \ round to 10's place, 3 sig digits 1906.0e -2 f.n 1900 ok \ round to 100's place, 2 sig digits 1906.0e -3 f.n 2000 ok \ round to 1000's place, 1 sig digit Note there is a trailing decimal point for rounding to the 1's place (n=0): all four digits are significant. There is no trailing decimal point for n = -1 through -3 which is appropriate -- the trailing zeros are not significant. For integers, trailing zeros would only be significant if a decimal point appears at the end. -- KM
Re: F.N floating point format word
Author: peter
Date: Thu, 08 Oct 2026 10:13
Date: Thu, 08 Oct 2026 10:13
27 lines
2359 bytes
2359 bytes
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
Re: F.N floating point format word
Author: 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
Re: F.N floating point format word
Author: Krishna Myneni
Date: Thu, 08 Oct 2026 14:18
Date: Thu, 08 Oct 2026 14:18
108 lines
4959 bytes
4959 bytes
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. > > 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 > > 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
Re: F.N floating point format word
Author: peter
Date: Thu, 08 Oct 2026 20:07
Date: Thu, 08 Oct 2026 20:07
567 lines
14123 bytes
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]
Re: F.N floating point format word
Author: dxf
Date: Fri, 09 Oct 2026 03:29
Date: Fri, 09 Oct 2026 03:29
32 lines
2213 bytes
2213 bytes
On 8/10/2026 9:13 pm, peter 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. There a risk when extracting the exponent on its own. As Anton pointed out in another thread REPRESENT at least keeps mantissa and exponent in sync. > The problem was 99.995e 2 f.n that became 100.0 > ... Every REPRESENT I know gives that: 99.995e pad 4 REPRESENT drop 2drop pad 4 dump 195FE0 | 31 30 30 30 |1000| ok
Re: F.N floating point format word
Author: dxf
Date: Fri, 09 Oct 2026 04:36
Date: Fri, 09 Oct 2026 04:36
14 lines
1429 bytes
1429 bytes
On 9/10/2026 2:29 pm, dxf wrote: > On 8/10/2026 9:13 pm, peter wrote: >> ... >> The problem was 99.995e 2 f.n that became 100.0 >> ... > > Every REPRESENT I know gives that: > > 99.995e pad 4 REPRESENT drop 2drop pad 4 dump > 195FE0 | 31 30 30 30 |1000| ok Never mind. I didn't notice the lack of decimal places.
Re: F.N floating point format word
Author: dxf
Date: Fri, 09 Oct 2026 09:54
Date: Fri, 09 Oct 2026 09:54
36 lines
2757 bytes
2757 bytes
On 8/10/2026 9:13 pm, peter 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.
> ...
I had a closer look using my own routines as a test bed. Using low values
e.g. u=1 resulted in a few tests from your latest list failing to round.
So anything less than u=17 (or whatever digits your REPRESENT can resolve)
might well create rounding issues. It's similar to what I found when #EXP
('Getting the exponent of an x87 float') used an 80-bit FLOG with the last
bits rounded off. Digits that otherwise displayed in the 18th position of
an F. etc for certain numbers (e.g circa 1E-3) were lost!
TL;DR AFAICS if one wants accurate rounding to 17 digits (or 700 digits)
from F.N then that's the value of u that needs to be passed when asking
for the exponent. It's hardly onerous since the buffer has be at least
that. Whether asking REPRESENT for more digits than this gains one
anything, I'd be less confident about. If the arithmetic inside REPRESENT
was only good to 17 digits...
Re: F.N floating point format word
Author: Krishna Myneni
Date: Fri, 09 Oct 2026 20:29
Date: Fri, 09 Oct 2026 20:29
52 lines
3859 bytes
3859 bytes
On 10/8/26 3:07 PM, peter wrote: > On Thu, 8 Oct 2026 09:18:57 -0500 > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > ... >> 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. >> ... > 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 ... Thank you! I will integrate the changes into fdot.4th and fdot-test.4th in the kForth packages, and verify the results. REPRESENT in kForth now returns the three character 'inf' and 'nan' strings when appropriate. It can also return 'ERR' for an unspecified conversion error (when dtoa() returns 0 characters and exp is not equal to 9999). I don't actually know under what condition this might happen, but if it happens, there is a string to indicate it. I factored my implementation of FS. in kForth to use the string output word (FS.) which is a factor of "FS.". (FS.) is now intrinsic to all kForth variants and is part of the Forth wordlist. It enables me to simplify considerably one of my strings library words. My definition of (FS.) will only prepend a sign for the case of 'inf'. The possible outputs of (FS.) on conversion failure are '+inf' '-inf' 'nan' and 'ERR'. I intend to follow this convention with (F.N) as well. Presently, I'm making other corrections and enhancements to my strings library, most of which came from discussion in this thread. -- Krishna
Re: F.N floating point format word
Author: Krishna Myneni
Date: Fri, 09 Oct 2026 21:54
Date: Fri, 09 Oct 2026 21:54
81 lines
4145 bytes
4145 bytes
On 10/9/26 4:54 AM, dxf wrote: ... > TL;DR AFAICS if one wants accurate rounding to 17 digits (or 700 digits) > from F.N then that's the value of u that needs to be passed when asking > for the exponent. Because of the round trip requirement for IEEE binary64 you should not need to ask for more than 17 digits to the initial call to REPRESENT in order to get the exponent. In my F.N code from Peter, I have the value u set at 17. Of course when you actually get down to using REPRESENT to obtain the number of digits you want, u must be set accordingly. Consider the following example: pad 8 erase 1 pad ! pad df@ fs. 4.94065645841247e-324 ok pad df@ 324 f.n 0.000 ... 005 \ last digit is 324 digits to right of decimal point pad df@ 328 f.n 0.000 ... 004 940 7 pad df@ 424 f.n 0.000 ... 004 940 656 458 412 465 441 765 687 928 682 213 723 650 598 026 143 247 644 255 856 825 006 755 072 702 087 518 652 998 363 616 359 923 8 ok It is rounding correctly. For reference see the output below. Do you have an example which shows that u > 17 is needed for the initial call to REPRESENT? -- Krishna 800 set-precision ok pad f@ fs. Digits 1 4. 2--031 940 656 458 412 465 441 765 687 928 682 32--061 213 723 650 598 026 143 247 644 255 856 62--091 825 006 755 072 702 087 518 652 998 363 92--121 616 359 923 797 965 646 954 457 177 309 122--151 266 567 103 559 397 963 987 747 960 107 152--181 818 781 263 007 131 903 114 045 278 458 182--211 171 678 489 821 036 887 186 360 569 987 212--241 307 230 500 063 874 091 535 649 843 873 242--271 124 733 972 731 696 151 400 317 153 853 272--301 980 741 262 385 655 911 710 266 585 566 302--331 867 681 870 395 603 106 249 319 452 715 332--361 914 924 553 293 054 565 444 011 274 801 362--391 297 099 995 419 319 894 090 804 165 633 392--421 245 247 571 478 690 147 267 801 593 552 422--451 386 115 501 348 035 264 934 720 193 790 452--481 268 107 107 491 703 332 226 844 753 335 482--511 720 832 431 936 092 382 893 458 368 060 512--541 106 011 506 169 809 753 078 342 277 318 542--571 329 247 904 982 524 730 776 375 927 247 572--601 874 656 084 778 203 734 469 699 533 647 602--631 017 972 677 717 585 125 660 551 199 131 632--661 504 891 101 451 037 862 738 167 250 955 662--691 837 389 733 598 993 664 809 941 164 205 692--721 702 637 090 279 242 767 544 565 229 087 722--751 538 682 506 419 718 265 533 447 265 625 752--781 000 000 000 000 000 000 000 000 000 000 782--800 000 000 000 000 000 000 0 e-324 ok
Re: F.N floating point format word
Author: dxf
Date: Fri, 09 Oct 2026 23:54
Date: Fri, 09 Oct 2026 23:54
12 lines
1540 bytes
1540 bytes
On 10/10/2026 8:54 am, Krishna Myneni wrote: > ... > It is rounding correctly. > > For reference see the output below. > > Do you have an example which shows that u > 17 is needed for the initial call to REPRESENT? Using SwifthForth4 and an initial call of u=1, it took 119 tests to show a failure of 8. A call of u=4 resulted in a failure of 4 out 119. I've certainly no reason to trust the test of a single number as evidence.
Re: F.N floating point format word
Author: dxf
Date: Sat, 10 Oct 2026 02:14
Date: Sat, 10 Oct 2026 02:14
14 lines
1929 bytes
1929 bytes
On 10/10/2026 8:54 am, Krishna Myneni wrote: > On 10/9/26 4:54 AM, dxf wrote: > ... >> TL;DR AFAICS if one wants accurate rounding to 17 digits (or 700 digits) >> from F.N then that's the value of u that needs to be passed when asking >> for the exponent. > > Because of the round trip requirement for IEEE binary64 you should not need to ask for more than 17 digits to the initial call to REPRESENT in order to get the exponent. In my F.N code from Peter, I have the value u set at 17. Of course when you actually get down to using REPRESENT to obtain the number of digits you want, u must be set accordingly. All I'm saying is the math, the numbers produced and subsequently displayed, have to correspond. A REPRESENT capable of resolving 700 digits is overkill for a system whose math is only good to 17 digits.
Re: F.N floating point format word
Author: Krishna Myneni
Date: Sat, 10 Oct 2026 02:37
Date: Sat, 10 Oct 2026 02:37
19 lines
2154 bytes
2154 bytes
On 10/9/26 6:54 PM, dxf wrote: > On 10/10/2026 8:54 am, Krishna Myneni wrote: >> ... >> It is rounding correctly. >> >> For reference see the output below. >> >> Do you have an example which shows that u > 17 is needed for the initial call to REPRESENT? > > Using SwifthForth4 and an initial call of u=1, it took 119 tests to show > a failure of 8. A call of u=4 resulted in a failure of 4 out 119. I've > certainly no reason to trust the test of a single number as evidence. > You could simply say you don't have an example. -- KM
Re: F.N floating point format word
Author: Krishna Myneni
Date: Sat, 17 Oct 2026 04:31
Date: Sat, 17 Oct 2026 04:31
32 lines
2445 bytes
2445 bytes
On 10/8/26 3:07 PM, peter wrote: ... > : f.n ( F:fp n -- ) ... > 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 ... > Here is the code for (F.N) > > \ (F.N) print float with n decimals to string > : (f.n) ( F:fp n -- ) ... > 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 ... The code snippets above use a control structure of the form IF ... IF ... ELSE ... ELSE ... THEN THEN Is this consistent with the standard? -- Krishna
Re: F.N floating point format word
Author: Anton Ertl
Date: Sat, 17 Oct 2026 05:27
Date: Sat, 17 Oct 2026 05:27
67 lines
3131 bytes
3131 bytes
Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>IF ... IF ... ELSE ... ELSE ... THEN THEN
>
>Is this consistent with the standard?
Yes, but
if A if B else C else D THEN THEN
is equivalent to
if A if B D else C then then
and in particular
if A if B swap else C else swap D then then
is also equivalent to
if A if B D else C then then
So one normally would not write code like this. It may be that this
is not the intended code.
OTOH, I have seen code like
swap +
or
does> ;
in production code, so sometimes programmers miss obvious
simplifications; the formatting of the original code may have
contributed to that.
For the code you posted, the simplification would be
over 0< if
dup 0 <= if
nip 0 over 1024 swap - min swap
else \ negative e and n
swap over 1- negate max swap \ limit negative n
then
then \ limit positive n
which can be further simplified (first branch) and unified (second
branch):
over 0< if
dup 0 <= if ( a b
nip 1024 over - 0 min
else \ negative e and n
swap 1 over - max \ limit negative n
then
swap
then \ limit positive n
The similarities between the branches make it plausible that this code
does what is intended.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Re: F.N floating point format word
Author: peter
Date: Sat, 17 Oct 2026 09:52
Date: Sat, 17 Oct 2026 09:52
48 lines
3059 bytes
3059 bytes
On Fri, 16 Oct 2026 23:31:07 -0500 Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > On 10/8/26 3:07 PM, peter wrote: > ... > > : f.n ( F:fp n -- ) > ... > > 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 > ... > > > Here is the code for (F.N) > > > > \ (F.N) print float with n decimals to string > > > : (f.n) ( F:fp n -- ) > ... > > 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 > ... > The code snippets above use a control structure of the form > > IF ... IF ... ELSE ... ELSE ... THEN THEN > > Is this consistent with the standard? > > -- > Krishna Thanks for noticing this! It might be standard but it does not do what it should! The limit positive n does not execute when it should. Here is a version that corrects this. over 0 >= if swap over 1024 swap - min swap else \ limit positive n dup 0<= if nip 0 swap else \ limit negative n when e negative 1 over - rot max swap then then \ limit negative n It worked correctly until I added the test for negative n and e! BR Peter
Re: F.N floating point format word
Author: Anton Ertl
Date: Sat, 17 Oct 2026 10:06
Date: Sat, 17 Oct 2026 10:06
37 lines
2434 bytes
2434 bytes
peter <peter.noreply@tin.it> writes:
>Here is a version that corrects this.
>
>over 0 >= if
> swap over 1024 swap - min swap else \ limit positive n
> dup 0<= if nip 0 swap else \ limit negative n when e negative
> 1 over - rot max swap then then \ limit negative n
Can this be simplified? Here's what I came up with:
over 0 >= if
1024 2 pick - rot min
else
dup 0<= if
nip 0
else
1 over - rot max
then
then
swap
Or, using CASE with ?OF:
case
dup 0 >= ?of 1024 2 pick - rot min endof
dup 0<= ?of nip 0 endof
1 over - rot max
0 endcase
swap
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Re: F.N floating point format word
Author: peter
Date: Sat, 17 Oct 2026 13:41
Date: Sat, 17 Oct 2026 13:41
137 lines
6678 bytes
6678 bytes
On Sat, 17 Oct 2026 10:06:20 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> peter <peter.noreply@tin.it> writes:
> >Here is a version that corrects this.
> >
> >over 0 >= if
> > swap over 1024 swap - min swap else \ limit positive n
> > dup 0<= if nip 0 swap else \ limit negative n when e negative
> > 1 over - rot max swap then then \ limit negative n
>
>
> Can this be simplified? Here's what I came up with:
>
> over 0 >= if
> 1024 2 pick - rot min
> else
> dup 0<= if
> nip 0
> else
> 1 over - rot max
> then
> then
> swap
So you are basically taken out the swap and putting it last.
The 2 pick needs to be over to work properly.
we have n, exp on the stack. The 3 cases we have can be summarized as
sign of
n exp limit of n
+ +- n=min(n, 1024-exp)
- - n=0
- + n=max(n, 1-e)
Lets now compile our different implementations
: A1
over 0 >= if
1024 over - rot min
else
dup 0<= if
nip 0
else
1 over - rot max
then
then
swap ;
: P1
over 0 >= if
swap over 1024 swap - min swap else \ limit positive n
dup 0<= if nip 0 swap else \ limit n when e negative
1 over - rot max swap then then ; \ limit negative n
seea A1
0x4284E0 48837D0000 cmp qword ptr [rbp], 0x0
0x4284E5 7C1E jl 0x428505
0x4284E7 48C7C000040000 mov rax, 0x400
0x4284EE 4829D8 sub rax, rbx
0x4284F1 488B4D00 mov rcx, qword ptr [rbp]
0x4284F5 4839C8 cmp rax, rcx
0x4284F8 7D02 jge 0x4284FC
0x4284FA 4891 xchg rcx, rax
0x4284FC 48895D00 mov qword ptr [rbp], rbx
0x428500 4889CB mov rbx, rcx
0x428503 EB2F jmp 0x428534
0x428505 4883FB00 cmp rbx, 0x0
0x428509 7F0D jg 0x428518
0x42850B 48895D00 mov qword ptr [rbp], rbx
0x42850F 48C7C300000000 mov rbx, 0x0
0x428516 EB1C jmp 0x428534
0x428518 48C7C001000000 mov rax, 0x1
0x42851F 4829D8 sub rax, rbx
0x428522 488B4D00 mov rcx, qword ptr [rbp]
0x428526 4839C8 cmp rax, rcx
0x428529 7D02 jge 0x42852D
0x42852B 4891 xchg rcx, rax
0x42852D 48895D00 mov qword ptr [rbp], rbx
0x428531 4889C3 mov rbx, rax
0x428534 488B4500 mov rax, qword ptr [rbp]
0x428538 48895D00 mov qword ptr [rbp], rbx
0x42853C 4889C3 mov rbx, rax
0x42853F C3 ret
96 bytes, 28 instructions
seea p1
0x428440 48837D0000 cmp qword ptr [rbp], 0x0
0x428445 7C1B jl 0x428462
0x428447 48C7C000040000 mov rax, 0x400
0x42844E 4829D8 sub rax, rbx
0x428451 488B4D00 mov rcx, qword ptr [rbp]
0x428455 4839C1 cmp rcx, rax
0x428458 7D02 jge 0x42845C
0x42845A 4891 xchg rcx, rax
0x42845C 48894500 mov qword ptr [rbp], rax
0x428460 EB29 jmp 0x42848B
0x428462 4883FB00 cmp rbx, 0x0
0x428466 7F0A jg 0x428472
0x428468 48C7450000000000 mov qword ptr [rbp], 0x0
0x428470 EB19 jmp 0x42848B
0x428472 48C7C001000000 mov rax, 0x1
0x428479 4829D8 sub rax, rbx
0x42847C 488B4D00 mov rcx, qword ptr [rbp]
0x428480 4839C8 cmp rax, rcx
0x428483 7D02 jge 0x428487
0x428485 4891 xchg rcx, rax
0x428487 48894500 mov qword ptr [rbp], rax
0x42848B C3 ret
76 bytes, 22 instructions
by moving out the swap and reducing from 3 to 1 occurrences we get
20 bytes more code! Now this example when we look at the function
standalone instead of inlined we are penalized by moving out the swap
as it need to generate code, it gets a block by itself between
then and ; In P1 we have the exp always as top of stack at all block
boundaries, it is unmodified and resides always in rbx.
In A1 the exp is second on stack and needs to be moved from rbx to memory
after each block of the code.
Note also that max and min are implemented to not create a block
boundary. Generally stack movements only create code at block boundaries
BR
Peter
> Or, using CASE with ?OF:
>
> case
> dup 0 >= ?of 1024 2 pick - rot min endof
> dup 0<= ?of nip 0 endof
> 1 over - rot max
> 0 endcase
> swap
>
> - anton
Re: F.N floating point format word
Author: dxf
Date: Sat, 17 Oct 2026 13:57
Date: Sat, 17 Oct 2026 13:57
33 lines
2110 bytes
2110 bytes
While I never considered or designed for 'negative decimal places' it appears my version of F.N does do them - albeit with results different from tests I've seen posted here. Wondering if there was any convention for them, google turned up the ROUND() function in Oracle and SQL. Online examples didn't show what happens at the extremes (what I was interested in) but I did find an online SQL server where I could test that. For SQL at least, the results were as follows. SQL: ROUND(876.543, 0) 877.000 ROUND(876.543,-1) 880.000 ROUND(876.543,-2) 900.000 ROUND(876.543,-3) <overflow error> ROUND(456.789, 0) 457.000 ROUND(456.789,-1) 460.000 ROUND(456.789,-2) 500.000 ROUND(456.789,-3) .000 F.N (dxf): 876.543e 0 f.n 877. 876.543e -1 f.n 880. 876.543e -2 f.n 900. 876.543e -3 f.n 1000. 456.789e 0 f.n 457. 456.789e -1 f.n 460. 456.789e -2 f.n 500. 456.789e -3 f.n 0.
Re: F.N floating point format word
Author: Anton Ertl
Date: Sat, 17 Oct 2026 13:59
Date: Sat, 17 Oct 2026 13:59
111 lines
5283 bytes
5283 bytes
peter <peter.noreply@tin.it> writes:
>On Sat, 17 Oct 2026 10:06:20 GMT
>anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
>
>> peter <peter.noreply@tin.it> writes:
>> >Here is a version that corrects this.
>> >
>> >over 0 >= if
>> > swap over 1024 swap - min swap else \ limit positive n
>> > dup 0<= if nip 0 swap else \ limit negative n when e negative
>> > 1 over - rot max swap then then \ limit negative n
>>
>>
>> Can this be simplified? Here's what I came up with:
>>
>> over 0 >= if
>> 1024 2 pick - rot min
>> else
>> dup 0<= if
>> nip 0
>> else
>> 1 over - rot max
>> then
>> then
>> swap
>
>So you are basically taken out the swap and putting it last.
Yes, and the change to the stuff after the first IF that I got wrong.
>The 2 pick needs to be over to work properly.
Even nicer.
>Lets now compile our different implementations
>by moving out the swap and reducing from 3 to 1 occurrences we get
>20 bytes more code!
Yes, with your code generator that usually produces a perfect register
allocation for basic blocks, the change to the code after the first if
provides no benefit to the generated code.
As for the SWAP, your code generator produces the canonical stack
representation at every basic block end, so putting the SWAP in a
separate basic block costs extra code in that block, and the other blocks don't necessarily get smaller, sometimes even larger. E.g.
nip 0 nip 0 swap
mov qword ptr [rbp], rbx mov qword ptr [rbp], 0x0
mov rbx, 0x0
>Now this example when we look at the function
>standalone instead of inlined we are penalized by moving out the swap
>as it need to generate code, it gets a block by itself between
>then and ; In P1 we have the exp always as top of stack at all block
>boundaries, it is unmodified and resides always in rbx.
Given that you don't change exp, another alternative is to keep it on
the return stack:
: A2
>r
dup 0 >= if
1024 r@ - min
else
r@ 0<= if
drop 0
else
1 r@ - max
then
then
r> ;
I leave it to you to determine if any of those results in shorter code
(my copy of lxf64 does not generate native code yet). And who knows
if I got that right.
Apart from inlining, there are two other ways in which the code
generation can be improved:
1) Extend the basic blocks by duplicating the code after control-flow
joins (in particular for THEN) until some other limit is reached
(e.g., the end of the definition); this would undo the move of the
SWAP to the end, but also duplicate the EXIT, and make the ELSE
jumps dead code, resulting in code as if the source looked like
(for A1):
: A1B
over 0 >= if
1024 over - rot min swap exit then
dup 0<= if
nip 0 swap exit then
1 over - rot max swap ;
That's closer to P1 than A1:-).
2) Register allocation that works across basic block boundaries and
does not need to materialize a canonical state there.
If the code generator is good enough that it does not make a
significant difference in the generated code which of the variants we
have written, we can ask ourselves which version is best for
understanding by a reader. Looking at the variants again, the
return-stack variant looks best to me, but again, who knows if I got
it right.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Re: F.N floating point format word
Author: Krishna Myneni
Date: Sat, 17 Oct 2026 14:31
Date: Sat, 17 Oct 2026 14:31
84 lines
2942 bytes
2942 bytes
On 10/17/26 12:27 AM, Anton Ertl wrote:
> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>> IF ... IF ... ELSE ... ELSE ... THEN THEN
>>
>> Is this consistent with the standard?
>
> Yes, but
>
> if A if B else C else D THEN THEN
>
> is equivalent to
>
> if A if B D else C then then
> ...
This does not seem to be what it does in either Gforth or kForth with
the following example, considering all possible boolean inputs.
: test ( n1 n2 -- )
IF
." n2 is true" cr
IF
." n1 is true" cr
ELSE
." n1 is false" cr
ELSE
." ??" cr
THEN
THEN
;
=== Gforth output ===
\ 1 false false ok
false false test ok 1
.s <1> 0 ok 1
drop
\ 2 false true ok
\ 3 true false ok
\ 4 true true ok
.s <0> ok
=== end output ===
With Gforth, nothing is ever printed.
=== kForth output ===
\ 1 false false
ok
false false test .s
0
ok
drop
ok
\ 2 false true
ok
false true test .s
n2 is true
n1 is false
<empty>
ok
\ 3 true false
ok
true false test .s
-1
ok
drop
ok
\ 4 true true
ok
true true test .s
n2 is true
n1 is true
??
<empty>
ok
=== end output ===
Re: F.N floating point format word
Author: Anton Ertl
Date: Sat, 17 Oct 2026 15:27
Date: Sat, 17 Oct 2026 15:27
116 lines
3901 bytes
3901 bytes
Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>On 10/17/26 12:27 AM, Anton Ertl wrote:
>> Krishna Myneni <krishna.myneni@ccreweb.org> writes:
>>> IF ... IF ... ELSE ... ELSE ... THEN THEN
>>>
>>> Is this consistent with the standard?
>>
>> Yes, but
>>
>> if A if B else C else D THEN THEN
>>
>> is equivalent to
>>
>> if A if B D else C then then
>> ...
>This does not seem to be what it does in either Gforth or kForth with
>the following example, considering all possible boolean inputs.
>
>: test ( n1 n2 -- )
> IF
> ." n2 is true" cr
> IF
> ." n1 is true" cr
> ELSE
> ." n1 is false" cr
> ELSE
> ." ??" cr
> THEN
> THEN
>;
>
>
>=== Gforth output ===
>\ 1 false false ok
>false false test ok 1
>.s <1> 0 ok 1
>drop
>
>\ 2 false true ok
>
>\ 3 true false ok
>
>\ 4 true true ok
>.s <0> ok
>=== end output ===
>
>With Gforth, nothing is ever printed.
If n2 is false nothing should be printed. And that's the only test
you did. The outer IF...THEN is just a distraction and is not
involved in the IF ... ELSE ... ELSE ... THEN. Here are more
interesting cases:
input:
cr true true test
output:
n2 is true
n1 is true
??
input:
cr false true test
output:
n2 is true
n1 is false
That's exactly what the standard specifies: ELSE ( orig1 -- orig2 )
continues the current execution at the place where orig2 is resolved,
and then resolves orig1, continuing the execution from there. In a
way it swaps between the current execution and the one in the orig.
>=== kForth output ===
>\ 1 false false
> ok
>false false test .s
>
> 0
> ok
>drop
> ok
>
>\ 2 false true
> ok
>false true test .s
>n2 is true
>n1 is false
><empty>
> ok
>
>\ 3 true false
> ok
>true false test .s
>
> -1
> ok
>drop
> ok
>
>\ 4 true true
> ok
>true true test .s
>n2 is true
>n1 is true
>??
><empty>
> ok
>=== end output ===
Judging by these tests, kforth also implements ELSE correctly.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Re: F.N floating point format word
Author: Krishna Myneni
Date: Sat, 17 Oct 2026 15:55
Date: Sat, 17 Oct 2026 15:55
61 lines
3257 bytes
3257 bytes
On 10/17/26 10:27 AM, Anton Ertl wrote: > Krishna Myneni <krishna.myneni@ccreweb.org> writes: >> On 10/17/26 12:27 AM, Anton Ertl wrote: >>> Krishna Myneni <krishna.myneni@ccreweb.org> writes: >>>> IF ... IF ... ELSE ... ELSE ... THEN THEN >>>> >>>> Is this consistent with the standard? >>> >>> Yes, but >>> >>> if A if B else C else D THEN THEN >>> >>> is equivalent to >>> >>> if A if B D else C then then >>> ... >> This does not seem to be what it does in either Gforth or kForth with >> the following example, considering all possible boolean inputs. >> >> : test ( n1 n2 -- ) >> IF >> ." n2 is true" cr >> IF >> ." n1 is true" cr >> ELSE >> ." n1 is false" cr >> ELSE >> ." ??" cr >> THEN >> THEN >> ; >> >> >> === Gforth output === >> \ 1 false false ok >> false false test ok 1 >> .s <1> 0 ok 1 >> drop >> >> \ 2 false true ok >> >> \ 3 true false ok >> >> \ 4 true true ok >> .s <0> ok >> === end output === >> >> With Gforth, nothing is ever printed. > > If n2 is false nothing should be printed. And that's the only test > you did. The outer IF...THEN is just a distraction and is not > involved in the IF ... ELSE ... ELSE ... THEN. Here are more > interesting cases: > I only had the comments, and looking back I didn't actually do the other tests with Gforth. Good to know there is consistency here. -- KM
Re: F.N floating point format word
Author: peter
Date: Sat, 17 Oct 2026 16:20
Date: Sat, 17 Oct 2026 16:20
190 lines
9334 bytes
9334 bytes
On Sat, 17 Oct 2026 13:59:02 GMT
anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> peter <peter.noreply@tin.it> writes:
> >On Sat, 17 Oct 2026 10:06:20 GMT
> >anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote:
> >
> >> peter <peter.noreply@tin.it> writes:
> >> >Here is a version that corrects this.
> >> >
> >> >over 0 >= if
> >> > swap over 1024 swap - min swap else \ limit positive n
> >> > dup 0<= if nip 0 swap else \ limit negative n when e negative
> >> > 1 over - rot max swap then then \ limit negative n
> >>
> >>
> >> Can this be simplified? Here's what I came up with:
> >>
> >> over 0 >= if
> >> 1024 2 pick - rot min
> >> else
> >> dup 0<= if
> >> nip 0
> >> else
> >> 1 over - rot max
> >> then
> >> then
> >> swap
> >
> >So you are basically taken out the swap and putting it last.
>
> Yes, and the change to the stuff after the first IF that I got wrong.
>
> >The 2 pick needs to be over to work properly.
>
> Even nicer.
>
> >Lets now compile our different implementations
> >by moving out the swap and reducing from 3 to 1 occurrences we get
> >20 bytes more code!
>
> Yes, with your code generator that usually produces a perfect register
> allocation for basic blocks, the change to the code after the first if
> provides no benefit to the generated code.
>
> As for the SWAP, your code generator produces the canonical stack
> representation at every basic block end, so putting the SWAP in a
> separate basic block costs extra code in that block, and the other blocks don't necessarily get smaller, sometimes even larger. E.g.
>
> nip 0 nip 0 swap
> mov qword ptr [rbp], rbx mov qword ptr [rbp], 0x0
> mov rbx, 0x0
>
> >Now this example when we look at the function
> >standalone instead of inlined we are penalized by moving out the swap
> >as it need to generate code, it gets a block by itself between
> >then and ; In P1 we have the exp always as top of stack at all block
> >boundaries, it is unmodified and resides always in rbx.
>
> Given that you don't change exp, another alternative is to keep it on
> the return stack:
>
> : A2
> >r
> dup 0 >= if
> 1024 r@ - min
> else
> r@ 0<= if
> drop 0
> else
> 1 r@ - max
> then
> then
> r> ;
This is nice and easy to read.
seea a2
0x4285A0 48837D0000 cmp qword ptr [rbp], 0x0
0x4285A5 48895C24F8 mov qword ptr [rsp-0x8], rbx
0x4285AA 488B5D00 mov rbx, qword ptr [rbp]
0x4285AE 488D6D08 lea rbp, [rbp+0x8]
0x4285B2 488D6424F8 lea rsp, [rsp-0x8]
0x4285B7 7C17 jl 0x4285D0
0x4285B9 48C7C000040000 mov rax, 0x400
0x4285C0 482B0424 sub rax, qword ptr [rsp]
0x4285C4 4839C3 cmp rbx, rax
0x4285C7 7D02 jge 0x4285CB
0x4285C9 4893 xchg rbx, rax
0x4285CB 4889C3 mov rbx, rax
0x4285CE EB22 jmp 0x4285F2
0x4285D0 48833C2400 cmp qword ptr [rsp], 0x0
0x4285D5 7F09 jg 0x4285E0
0x4285D7 48C7C300000000 mov rbx, 0x0
0x4285DE EB12 jmp 0x4285F2
0x4285E0 48C7C001000000 mov rax, 0x1
0x4285E7 482B0424 sub rax, qword ptr [rsp]
0x4285EB 4839C3 cmp rbx, rax
0x4285EE 7D02 jge 0x4285F2
0x4285F0 4893 xchg rbx, rax
0x4285F2 48895DF8 mov qword ptr [rbp-0x8], rbx
0x4285F6 488B1C24 mov rbx, qword ptr [rsp]
0x4285FA 488D6DF8 lea rbp, [rbp-0x8]
0x4285FE 488D642408 lea rsp, [rsp+0x8]
0x428603 C3 ret
100 bytes, 27 instructions
> I leave it to you to determine if any of those results in shorter code
> (my copy of lxf64 does not generate native code yet). And who knows
> if I got that right.
>
> Apart from inlining, there are two other ways in which the code
> generation can be improved:
>
> 1) Extend the basic blocks by duplicating the code after control-flow
> joins (in particular for THEN) until some other limit is reached
> (e.g., the end of the definition); this would undo the move of the
> SWAP to the end, but also duplicate the EXIT, and make the ELSE
> jumps dead code, resulting in code as if the source looked like
> (for A1):
>
> : A1B
> over 0 >= if
> 1024 over - rot min swap exit then
> dup 0<= if
> nip 0 swap exit then
> 1 over - rot max swap ;
This is how I would have written it if it was a standalone word
seea a1b
0x428550 48837D0000 cmp qword ptr [rbp], 0x0
0x428555 7C1A jl 0x428571
0x428557 48C7C000040000 mov rax, 0x400
0x42855E 4829D8 sub rax, rbx
0x428561 488B4D00 mov rcx, qword ptr [rbp]
0x428565 4839C8 cmp rax, rcx
0x428568 7D02 jge 0x42856C
0x42856A 4891 xchg rcx, rax
0x42856C 48894D00 mov qword ptr [rbp], rcx
0x428570 C3 ret
0x428571 4883FB00 cmp rbx, 0x0
0x428575 7F09 jg 0x428580
0x428577 48C7450000000000 mov qword ptr [rbp], 0x0
0x42857F C3 ret
0x428580 48C7C001000000 mov rax, 0x1
0x428587 4829D8 sub rax, rbx
0x42858A 488B4D00 mov rcx, qword ptr [rbp]
0x42858E 4839C8 cmp rax, rcx
0x428591 7D02 jge 0x428595
0x428593 4891 xchg rcx, rax
0x428595 48894500 mov qword ptr [rbp], rax
0x428599 C3 ret
74 bytes, 22 instructions
> That's closer to P1 than A1:-).
>
> 2) Register allocation that works across basic block boundaries and
> does not need to materialize a canonical state there.
>
> If the code generator is good enough that it does not make a
> significant difference in the generated code which of the variants we
> have written, we can ask ourselves which version is best for
> understanding by a reader. Looking at the variants again, the
> return-stack variant looks best to me, but again, who knows if I got
> it right.
It tests out right!
I agree that this is the easiest to understand.
When I started the code-generator for lxf, many years ago my first approach
was to push the state to a state stack at a block entry and to pop it of
at the end and there do a fixup to get both branches of an IF to the same
state . It tuned out difficult to implement so I started with the easy
way of returning to a known state at every block. This was good also
for another reason. The longer blocks you have the more registers can be
tied up. In lxf I have 5 registers available to the code generator. It is
very seldom that they get over used and need to spill to memory. In lxf64
I have 9 registers and have not yet written a spill routine. The few times
it has been signaled it has been due to another error, like not releasing
a register. On ARM64 there will be even more registers to play with.
I need to think if I can use them better
BR
Peter
> - anton
Re: F.N floating point format word
Author: peter
Date: Sat, 17 Oct 2026 16:23
Date: Sat, 17 Oct 2026 16:23
50 lines
2952 bytes
2952 bytes
On Sun, 18 Oct 2026 00:57:50 +1100 dxf <dxforth@gmail.com> wrote: > While I never considered or designed for 'negative decimal places' it appears > my version of F.N does do them - albeit with results different from tests I've > seen posted here. > > Wondering if there was any convention for them, google turned up the ROUND() > function in Oracle and SQL. Online examples didn't show what happens at the > extremes (what I was interested in) but I did find an online SQL server where > I could test that. For SQL at least, the results were as follows. > > SQL: > > ROUND(876.543, 0) 877.000 > ROUND(876.543,-1) 880.000 > ROUND(876.543,-2) 900.000 > ROUND(876.543,-3) <overflow error> > > ROUND(456.789, 0) 457.000 > ROUND(456.789,-1) 460.000 > ROUND(456.789,-2) 500.000 > ROUND(456.789,-3) .000 > > F.N (dxf): > > 876.543e 0 f.n 877. > 876.543e -1 f.n 880. > 876.543e -2 f.n 900. > 876.543e -3 f.n 1000. > > 456.789e 0 f.n 457. > 456.789e -1 f.n 460. > 456.789e -2 f.n 500. > 456.789e -3 f.n 0. > F.N (pf) 876.543e 0 f.n 877. ok 876.543e -1 f.n 880 ok 876.543e -2 f.n 900 ok 876.543e -3 f.n 900 ok 456.789e 0 f.n 457. ok 456.789e -1 f.n 460 ok 456.789e -2 f.n 500 ok 456.789e -3 f.n 500 ok
Re: F.N floating point format word
Author: Krishna Myneni
Date: Sun, 18 Oct 2026 22:10
Date: Sun, 18 Oct 2026 22:10
74 lines
4141 bytes
4141 bytes
On 10/8/26 3:07 PM, peter wrote: > On Thu, 8 Oct 2026 09:18:57 -0500 > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: ... >>> 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. >> ... > 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 > ... > > \ 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 > ... I have patched the code with all recent fixes for the logic of (F.N) and revised it to work on both separate stack and unified data/fp stack in fdotn.4th. I have also revised the automated tests in fdotn-test.4th to include your tests. The two files, fdotn.4th fdotn-test.4th may be found in the lastest commits for the git repos for kForth-64/32/Win32 on github or in the repos on ccreweb.org. Direct links to the revised files: https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn.4th https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn-test.4th An additional change to the definition of (F.N) prevents the sign on conversion failure from being output except for "inf". The last two tests in fdotn-test.4th have been revised accordingly. -- KM
Re: F.N floating point format word
Author: peter
Date: Mon, 19 Oct 2026 13:41
Date: Mon, 19 Oct 2026 13:41
87 lines
4673 bytes
4673 bytes
On Sun, 18 Oct 2026 17:10:27 -0500 Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > On 10/8/26 3:07 PM, peter wrote: > > On Thu, 8 Oct 2026 09:18:57 -0500 > > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > ... > >>> 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. > >> > ... > > 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 > > > ... > > > > \ 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 > > ... > > I have patched the code with all recent fixes for the logic of (F.N) and > revised it to work on both separate stack and unified data/fp stack in > fdotn.4th. I have also revised the automated tests in fdotn-test.4th to > include your tests. The two files, > > fdotn.4th > fdotn-test.4th > > may be found in the lastest commits for the git repos for > kForth-64/32/Win32 on github or in the repos on ccreweb.org. > > Direct links to the revised files: > > https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn.4th > https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn-test.4th > > An additional change to the definition of (F.N) prevents the sign on > conversion failure from being output except for "inf". The last two > tests in fdotn-test.4th have been revised accordingly. > > -- > KM > Nice to see that you could integrate it in kForth! I have also introduced it in ntf64/lxf64. I liked the possibility to directly specify how many digits to print instead of setting precision (that I half of the time misspell!) I might do a FS.N where n would be significant digits BR Peter
Re: F.N floating point format word
Author: Krishna Myneni
Date: Mon, 19 Oct 2026 14:44
Date: Mon, 19 Oct 2026 14:44
96 lines
5107 bytes
5107 bytes
On 10/19/26 8:41 AM, peter wrote: > On Sun, 18 Oct 2026 17:10:27 -0500 > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > >> On 10/8/26 3:07 PM, peter wrote: >>> On Thu, 8 Oct 2026 09:18:57 -0500 >>> Krishna Myneni <krishna.myneni@ccreweb.org> wrote: >> ... >>>>> 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. >>>> >> ... >>> 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 >>> >> ... >>> >>> \ 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 >>> ... >> >> I have patched the code with all recent fixes for the logic of (F.N) and >> revised it to work on both separate stack and unified data/fp stack in >> fdotn.4th. I have also revised the automated tests in fdotn-test.4th to >> include your tests. The two files, >> >> fdotn.4th >> fdotn-test.4th >> >> may be found in the lastest commits for the git repos for >> kForth-64/32/Win32 on github or in the repos on ccreweb.org. >> >> Direct links to the revised files: >> >> https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn.4th >> https://github.com/mynenik/kForth-64/blob/master/forth-src/fdotn-test.4th >> >> An additional change to the definition of (F.N) prevents the sign on >> conversion failure from being output except for "inf". The last two >> tests in fdotn-test.4th have been revised accordingly. >> >> -- >> KM >> > Nice to see that you could integrate it in kForth! > > I have also introduced it in ntf64/lxf64. > I liked the possibility to directly specify how many digits to print > instead of setting precision (that I half of the time misspell!) > > I might do a FS.N where n would be significant digits > Do you mean decimal places? The latest strings library in kForth-x has (FS.N) where n is decimal places. I think this corresponds to Fortran 90's ES<w>.<n> format, but haven't verified it yet. Normal FS. and (FS.) converts to output in significant digits specified via PRECISION. -- KM
Re: F.N floating point format word
Author: Krishna Myneni
Date: Mon, 19 Oct 2026 14:51
Date: Mon, 19 Oct 2026 14:51
40 lines
2866 bytes
2866 bytes
On 10/19/26 9:44 AM, Krishna Myneni wrote:
> On 10/19/26 8:41 AM, peter wrote:
...
>> Nice to see that you could integrate it in kForth!
>>
>> I have also introduced it in ntf64/lxf64.
>> I liked the possibility to directly specify how many digits to print
>> instead of setting precision (that I half of the time misspell!)
>>
>> I might do a FS.N where n would be significant digits
>>
>
> Do you mean decimal places? The latest strings library in kForth-x has
> (FS.N) where n is decimal places. I think this corresponds to Fortran
> 90's ES<w>.<n> format, but haven't verified it yet.
>
> Normal FS. and (FS.) converts to output in significant digits specified
> via PRECISION.
>
...
From strings.4th:
\ Convert r to a scientific notation string, caddr u pair,
\ with n decimal places
: (fs.n) ( n -- a u ) ( F: r -- ) \ ( r n -- a u )
precision >r 1+ set-precision (fs.) strbufmove
r> set-precision ;
(FS.) is an intrinsic word in kForth, but it is easy to write using
REPRESENT. I think I posted it in a previous thread.
FS.N where n is decimal places is often what users want.
--
KM
Re: F.N floating point format word
Author: peter
Date: Mon, 19 Oct 2026 16:03
Date: Mon, 19 Oct 2026 16:03
49 lines
3299 bytes
3299 bytes
On Mon, 19 Oct 2026 09:51:12 -0500 Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > On 10/19/26 9:44 AM, Krishna Myneni wrote: > > On 10/19/26 8:41 AM, peter wrote: > ... > >> Nice to see that you could integrate it in kForth! > >> > >> I have also introduced it in ntf64/lxf64. > >> I liked the possibility to directly specify how many digits to print > >> instead of setting precision (that I half of the time misspell!) > >> > >> I might do a FS.N where n would be significant digits > >> > > > > Do you mean decimal places? The latest strings library in kForth-x has > > (FS.N) where n is decimal places. I think this corresponds to Fortran > > 90's ES<w>.<n> format, but haven't verified it yet. > > > > Normal FS. and (FS.) converts to output in significant digits specified > > via PRECISION. > > > ... > > From strings.4th: > > \ Convert r to a scientific notation string, caddr u pair, > \ with n decimal places > : (fs.n) ( n -- a u ) ( F: r -- ) \ ( r n -- a u ) > precision >r 1+ set-precision (fs.) strbufmove > r> set-precision ; > > (FS.) is an intrinsic word in kForth, but it is easy to write using > REPRESENT. I think I posted it in a previous thread. > > FS.N where n is decimal places is often what users want. > > -- > KM I did it even simpler as I already had a factor for fs. and fe. : (FS.) fpad precision 2dup represent 1 (fx.) ; : (FS.N) fpad swap 1+ 1 max 2dup represent 1 (fx.) ; : (FE.) fpad precision 3 max 2dup represent 3 (fx.) ; no negative n allowed here! Peter
Re: F.N floating point format word
Author: Krishna Myneni
Date: Mon, 19 Oct 2026 16:41
Date: Mon, 19 Oct 2026 16:41
29 lines
2549 bytes
2549 bytes
On 10/19/26 11:03 AM, peter wrote: > On Mon, 19 Oct 2026 09:51:12 -0500 > Krishna Myneni <krishna.myneni@ccreweb.org> wrote: > ... >> \ Convert r to a scientific notation string, caddr u pair, >> \ with n decimal places >> : (fs.n) ( n -- a u ) ( F: r -- ) \ ( r n -- a u ) >> precision >r 1+ set-precision (fs.) strbufmove >> r> set-precision ; >> ... >> FS.N where n is decimal places is often what users want. >> ... > I did it even simpler as I already had a factor for fs. and fe. > > : (FS.) fpad precision 2dup represent 1 (fx.) ; > : (FS.N) fpad swap 1+ 1 max 2dup represent 1 (fx.) ; > : (FE.) fpad precision 3 max 2dup represent 3 (fx.) ; > > no negative n allowed here! > That's nice and compact. -- KM
Re: F.N floating point format word
Author: Anton Ertl
Date: Mon, 19 Oct 2026 18:18
Date: Mon, 19 Oct 2026 18:18
18 lines
2386 bytes
2386 bytes
peter <peter.noreply@tin.it> writes:
>I might do a FS.N where n would be significant digits
Or you implement f.rdp
<https://net2o.de/gforth/Floating_002dpoint-output.html#index-f_002erdp>
and get something that is useful for a number of other use cases as
well. F.RDP uses scientific notation if d (decimal places) is too big
for r (field width), among other conditions. So if you want
scientific notation, use np>nr. It does not limit itself to p digits,
if it has more room; p is just the minimum shown precision; it's used
for deciding between fixed-point and scientific notation.
- anton
--
M. Anton Ertl http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
New standard: https://forth-standard.org/
EuroForth 2026 CFP: http://www.euroforth.org/ef26/cfp.html
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads