Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #135242

kForth-Win32 fix for decimal string to double float conversion

From Krishna Myneni <krishna.myneni@ccreweb.org>
Newsgroups comp.lang.forth
Subject kForth-Win32 fix for decimal string to double float conversion
Date 2026-07-20 09:48 -0500
Organization A noiseless patient Spider
Message-ID <113lcfk$18opt$1@dont-email.me> (permalink)

Show all headers | View raw


I recently fixed a problem with kForth-Win32's conversion of decimal 
strings to IEEE double precision floats e.g., with rounding mode set to 
round nearest (with ties to eve), the string

"1.000 000 000 000 000 111 022 302 462 515 654 042 363 166 809 082 031 
251 e0" (spaces added for clarity)

converted to the IEEE 64-bit binary pattern (shown in hex, high-dword 
low- dword)

3ff00000 00000000

representing the exact number 1.000 ...

instead of converting to the nearest representable IEEE 754 double 
precision number,

3ff00000 00000001

which has the value

1.000 000 000 000 000 222 044 604 925 031 308 084 726 333 618 164 062 500

Thus, the following Forth code should behave as follows:

17 set-precision
  ok
1.000000000000000111022302462515654042363166809082031251e0 fs.
1.0000000000000002e+00  ok

instead of printing 1.000000000000000e+00

Note that this is not an output conversion problem with the word "FS." 
but an interpreter input conversion problem for the entered floating 
point value.

Using double number arithmetic on a 64-bit Forth system, you may easily 
verify that the correct round to nearest representable floating point 
value is the one represented by the binary pattern 3ff00000 00000001 , 
rather than to the exactly representable value of 1.000.... (pattern 
3ff00000 00000000):

#222 044 604 925 031 308 084 726 333 618 164 062 500.
#111 022 302 462 515 654 042 363 166 809 082 031 251.
D- UD.

which prints

111 022 302 462 515 654 042 363 166 809 082 031 249

and is smaller than the distance between

1.000 000 000 000 000 111 022 302 462 515 654 042 363 166 809 082 031 251e0

and the exactly representable 1.000 ...

Be sure to remove spaces between the digits in the code above.

The Problem:
-----------
This is a quality of implementation issue in the decimal string to 
floating point conversion implemented in the C library function, 
strtod(), for the Digital Mars C/C++ compiler. The conversion is not 
simple, and has been studied extensively by a number of people. Of 
particular note is the following paper,

"Correctly Rounded Binary-Decimal and Decimal-Binary Conversions," David 
M. Gay, Numerical Analysis Manuscript 90-10, AT&T Bell Laboratories, 30 
November 1990.

and the corresponding code, dtoa.c, link below, which provides a 
high-quality strtod() replacement function for decimal string to binary 
floating point conversions, as well as the inverse function, dtoa().

https://netlib.org/fp/dtoa.c

The kForth system test code, fpio-test.4th, may be used to identify 
problems with decimal string conversion to IEEE double floats in other 
Forth systems. The test file may be found at the link below.

https://github.com/mynenik/kForth-Win32/blob/master/forth-src/system-test/fpio-test.4th


The Fix:
-------
There are several approaches to doing the conversion correctly (see the 
paper mentioned above, and the references therein). The replacement 
strtod() function in dtoa.c uses its built-in big integer arithmetic to 
do the conversions, and this library is apparently used in languages 
like python and Java.

kForth-Win32, as of version 2.5.5, uses the strtod() function from 
dtoa.c. Previous versions of kForth-Win32 did not pass all tests from 
fpio-test.4th. Version 2.5.5 passes all of the tests. The Linux versions 
of kForth are not affected since GCC fixed their conversions a while back.


Why does it matter?:
-------------------
Without proper rounding to nearest representable double-precision binary 
value, some computational cases can suffer significant reduction in 
accuracy. Examples are range reduction for large value arguments to 
trigonometric functions (sin, cos, tan) and solving systems of nonlinear 
differential equations with sensitivity to initial conditions.


--
Krishna Myneni





Back to comp.lang.forth | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-20 09:48 -0500
  Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-21 11:40 +0200
    Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-21 06:20 -0500
      Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-25 16:08 +0200
        Re: kForth-Win32 fix for decimal string to double float conversion peter <peter.noreply@tin.it> - 2026-07-25 17:07 +0200
          Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-25 13:22 -0500
            Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-25 13:30 -0500
        Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-25 13:17 -0500
          Re: kForth-Win32 fix for decimal string to double float conversion dxf <dxforth@gmail.com> - 2026-07-26 13:15 +1000
            Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-26 07:18 -0500
              Re: kForth-Win32 fix for decimal string to double float conversion dxf <dxforth@gmail.com> - 2026-07-31 14:54 +1000
                Re: kForth-Win32 fix for decimal string to double float conversion anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-07-31 08:35 +0000
                Re: kForth-Win32 fix for decimal string to double float conversion albert@spenarnc.xs4all.nl - 2026-07-31 12:05 +0200
                Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-31 07:54 -0500
                Re: kForth-Win32 fix for decimal string to double float conversion anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-07-31 16:55 +0000
                Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-31 14:40 -0500
                Re: kForth-Win32 fix for decimal string to double float conversion anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2026-08-01 08:23 +0000
                Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-01 06:42 -0500
    Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-21 11:30 -0500
      Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-21 14:47 -0500
  Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-25 23:42 +0200
    Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-25 18:47 -0500
      Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-26 13:11 +0200
  Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-26 13:22 +0200
    Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-26 07:23 -0500
      Re: kForth-Win32 fix for decimal string to double float conversion albert@SPENARNC.XS4ALL.NL - 2026-07-26 20:13 +0200
        Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-07-27 12:20 -0500
  Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-11 07:24 -0500
  Re: kForth-Win32 fix for decimal string to double float conversion Krishna Myneni <krishna.myneni@ccreweb.org> - 2026-08-24 18:55 -0500

csiph-web