On 10/20/26 8:47 AM, Krishna Myneni wrote:
> The double-double (dd) precision floating format consists of a pair of IEEE 754 binary64 (double precision) floats. It effectively provides a significand which has twice the precision of an ordinary binary64 float:
>
> p = 2*53 = 106 bits
>
> An ordinary binary64 number can be converted to a decimal string using REPRESENT. With a high-quality REPRESENT which provides >= 33 significant decimal digits we can generate test cases for binary to decimal conversion of dd precision numbers simply by using binary64 numbers.
>
> Furthermore, if REPRESENT can provide a larger number of digits, it is possible to generate test cases for much larger p e.g. binary128 or binary256 numbers, simply using double-precision floats.
>
> The following test program illustrates the use of double precision numbers to check the binary to decimal conversion of dd precision numbers with the word (DDFS.). In kForth-x, this word is provided in the latest update of dd_io.4th (link below) -- the test program is based on this assumption.
>
> I will emphasize again the important part: The reference string is the one obtained with (FS.) from a double-precision number with a PRECISION set to 32.
>
> The output shows that (DDFS.) as presently implemented in dd_io.4th fails for one of the test cases (test 2), and probably for many more cases.
>
> --
> Krishna Myneni
> ...
It occurred to me to use the MPFR library to determine the correct binary to decimal conversion strings for any pair of binary64 floating point numbers representing a double-double precision number, which effectively provides a 106 bit significand.
This is possible because we can declare two MPFR variables, each with a 128 bit significand, and load these variables from the two binary64 numbers and then add them, to obtain the 128 bit-precision result. The result can then be converted to a string with n=32 significant digits using the MPFR function mpfr_get_str():
=== begin code ===
\ Set precision before initializing mp vars
128 mpfr_set_default_prec
mpfr_t hi
mpfr_t lo
mpfr_t sum
hi mpfr_init
lo mpfr_init
sum mpfr_init
FPSTACK? [IF]
: ddwrite ( F: r_hi r_lo -- )
lo GMP_RNDN mpfr_set_d drop
hi GMP_RNDN mpfr_set_d drop
sum hi lo GMP_RNDN mpfr_add drop
sum 32 mpfr.
;
[ELSE]
: ddwrite ( r_hi r_lo -- )
2>r lo 2r> GMP_RNDN mpfr_set_d drop
2>r hi 2r> GMP_RNDN mpfr_set_d drop
sum hi lo GMP_RNDN mpfr_add drop
sum 32 mpfr.
;
[THEN]
=== end code ===
The above code defines a word DDWRITE which is similar to the function from David Bailey's ddfun package. I've already tested the above definition of DDWRITE and it works, except for one minor issue having to do with the implementation of "MPFR." ; also, I need a string output version.
The word MPFR. is defined in kForth's mpfr-utils.4th ( forth-src/libs/gmp/mpfr-utils.4th ) . The underlying mpfr variable to decimal string conversion word is MP>STR (also defined in mpfr-utils.4th), which calls mpfr_get_str(). However, I am reworking this currently to define a REPRESENT for mpfr variables. The function mpfr_get_str() provides more options than the behavior of REPRESENT but it will be useful to define REPRESENT-MPFR which behaves in the same manner as Forth's REPRESENT.
--
Krishna Myneni