🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.forth
Article #21

Re: F.N floating point format word

#21
From: Krishna Myneni
Date: Wed, 07 Oct 2026 19:58
115 lines
5221 bytes
On 10/6/26 4:44 PM, Krishna Myneni wrote:
> On 10/2/26 10:30 AM, Krishna Myneni wrote:
> ...
>> I've also revised your F.N code to work with both separate fp stack 
>> and unified data/fp stack systems (since kForth-32 and kForth-Win32 
>> still use a unified stack). F.N passes all of my tests on both 
>> kForth-64/32. 
> ...
> F.N is already a significant improvement over my F.RD word which uses 
> the conventional method for fixed point output using f>fpstr:
> 
> \ Convert r to a formatted fixed point string with
> \ n decimal places, 0 <= n <= 17.
> : f>fpstr ( n -- a u ) ( F: r -- ) \ ( r n -- a u )
>      0 max 17 min >r 10e r@ s>f f**
>      f* fround f>d dup -rot dabs
>      <# r> 0 ?DO # LOOP [char] . hold #s rot sign #> ;
> ...

It dawned on me that F>FPSTR is just an implementation of (F.N) -- the 
string output version of F.N . Beyond the example I gave above, what's 
wrong with this implementation? I can rename the word to (F.N) and use 
my automated tests in fdotn-test.4th to test F>FPSTR.

: (f.n) f>fpstr ;

With the definition above, I obtain 7 errors for the following tests: 
29, 30, 31, 34, 35, 36, and 56.

The implementation of F>FPSTR was designed to handle no more than 17 
decimal places, which explains why it fails for tests requesting more 
than 17 decimal places: 29, 30, 31, 35, and 36.

Eliminating these tests leaves two remaining tests of F>FPSTR which 
failed for some reason other than asking for too many decimal places:

34: -0.6e0 17 (f.n) type -0.60000000000000000 ok
56: -0.0e 0 (f.n) type 0. ok

The failure of test 56 occurred because we lost the sign of -0.0e when 
performing F>D so this is understood.

The failure of converting the binary64 representation of -0.6e0 to 17 
decimal places, even though it looks like it is correct, is not, because 
the only double precision numbers which can be represented near the 
decimal input, -0.6e0 are

    before: -0.600000000000000088817841970012523233890533447265625
         x: -0.59999999999999997779553950749686919152736663818359375
     after: -0.5999999999999998667732370449812151491641998291015625

The closest number is the middle number, which, rounded to 17 decimal 
places, is

-0.59999999999999998

Why does F>FPSTR shows the output as "-0.60000000000000000"? This is due 
to a fortuitous coincidence for the numbers which are represented in 
double precision at -0.6e17, which is the intermediate computation of 
F>FPSTR before converting to a double length integer. The actual numbers 
which can be represented around -0.6e-17 are spaced like this:

    before: -60000000000000008
         x: -60000000000000000
     after: -59999999999999992

The middle number happens to be a match for the expected output,

-0.60000000000000000

The algorithm for F>FPSTR is falling on the double precision numbers in 
the region of the scaled number, which can turn out badly if we did not 
have the coincidence of landing on an exact representation after the 
scaling e.g. 0.61100000000009990e

The double precision numbers in the unscaled region are
    before: 0.61100000000009979661541592577123083174228668212890625
         x: 0.6110000000000999076377183882868848741054534912109375
     after: 0.61100000000010001866002085080253891646862030029296875

The center value x, rounded to 17 decimal places is

0.61100000000009991e

which is what is printed by our more accurate REPRESENT-based (f.n) 
defined in fdotn.4th:

0.61100000000009990e 17 (f.n) type
0.61100000000009991 ok

In contrast the scaled region used by F>FPSTR has the possible double 
precision numbers

    before: 61100000000009984
         x: 61100000000009992
     after: 61100000000010000

with the closest being 61100000000009992 to the scaled input number. Hence,

0.61100000000009990e 17 f>fpstr type
0.61100000000009992 ok


--
KM









Technical Details
Message-ID: <11a689m$36odc$1@dont-email.me>
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> <11a3q49$2b8g6$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: Krishna Myneni <krishna.myneni@ccreweb.org>
Newsgroups: comp.lang.forth
Subject: Re: F.N floating point format word
Date: Wed, 7 Oct 2026 14:58:44 -0500
Organization: A noiseless patient Spider
Lines: 115
Message-ID: <11a689m$36odc$1@dont-email.me>
References: <119oin2$26n99$1@dont-email.me> <11a3q49$2b8g6$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 07 Oct 2026 19:58:47 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3367340"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18gde8N3yT+S65BAzYU5+6N"; posting-host="d05ed1c28a75393cf2b11653aaa7e6f7"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:nyTz7NVaS/HKEZDHEtFcPDTkZ24= sha256:0U4bUVOJROlbUBA36iCxkh/be83VZ/yF9wqcqvraQy8=
	sha1:Gzuhf+lWX6WV8PhDVqOusuJm224= sha256:Rj0yVjU6zWPTqtTHwczfQ6yEQ2bd7IZJIcqZNABCSxw=
Content-Language: en-US
In-Reply-To: <11a3q49$2b8g6$1@dont-email.me>