• A practical example of a bit accuracy float problem

    From marcel hendrix@mhx@iae.nl to comp.lang.forth on Fri Oct 2 01:40:28 2026
    From Newsgroup: comp.lang.forth

    The xd-fp numerical library is a port of the double-double package
    written by Yozo Hida, Xiaoye S. Li, and David H. Bailey. A C/C++
    version of that code is available on https://github.com/greggroth/im_learning_cpp/tree/master/comp_programing_exam_solution/qd-2.3.12

    What was the main problem that took me so long to approximate this C
    library in iForth?

    The basic idea of qd is to use two IEEE standard 53-bit FP numbers xh
    and xl to synthesize a new floating-point type that has a mantissa of
    106 bits with standard double precision exponent and sign bits.

    It is not mandatory to use IEEE standard double precision floats for xh
    and xl. In xd-fp.frt I have exploited this to use 64-bit extended floats
    for xh and xl, where the usable mantissa width of the new type has 128
    bits, and the exponent and sign are the same as for a standard IEEE
    extended float. No manipulation of the FPU control word is necessary
    to set, and keep, the mantissa width to 53 bits.

    The practical problem with the port was the specification of the 106/128
    bit constants that are used to define the library's mathematical
    functions (exp, sin, sinh, asin, asinh, etc.). A 106 bit double-double constant consists of 2 double-precision floats xh and xl. When xh is off
    in, e.g., its LSB, this translates into an error of bit 53 of the 106
    bit constant. IOW, that constant will be off by 2^-53 and has at most 52
    bits of precision, not 106.

    The above implies that the constants used in qd, specified as duo 53-bit
    FP numbers, must be converted bit-for-bit accurate to the DPFLOATs (!)
    of the target Forth. Example:

    static const dd_real sin_table [] = {
    dd_real(1.950903220161282758e-01, -7.991079068461731263e-18), ...

    This is not a trivial problem, and an example of a practical situation
    where the Forth system is asked to potentially handle >700 digit
    floating point text. (For extended floats, even much more than 700 digits).

    Initially I overlooked this problem. When verifying the xd math
    functions with external programs that have arbitrary precision
    capability, I found that especially the goniometric functions
    had only about 17 accurate digits, not the expected 38. Inspection at
    the bit level showed that this was caused by about 50% (obviously) of
    the inline float constants being off by 1 LSB.

    I did not re-implement iForth's interpreter and >FLOAT word to handle thousands of digits. It isn't clear that this helps, because who says
    that the constants in the qd C library, and others, are always exact
    (the mantissas doubles suspisciously all have 18 decimals).

    Luckily, in the case at hand the necessary constants are not magic
    numbers but result from formulas that can be computed to high precision
    with 106/128 bit floating point, i.e., *with xd-fp itself*.

    The procedure was therefore to define all FP constants as 0e0. After
    xd-fp has loaded, the iForth compiler recomputes all XDCONSTANTs using
    XD from text generated by a high-precision package like MATLAB's
    VPA(), and stores the result with XD! directly into the target
    constant's >BODY areas. I was able to show that all constants turn out
    to have at least 39 decimals of remaining precision.

    The fix fails when the compiler inlines floating point constants. On x86
    this is an unlikely Forth optimization as FP constants can't be
    generated inline and must always be fetched from memory.

    I realize full well that this fix is completely obvious to those that
    have never struggled with the problem. Debugging is about locating the problem, not the fixing process.

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From dxf@dxforth@gmail.com to comp.lang.forth on Fri Oct 2 12:07:39 2026
    From Newsgroup: comp.lang.forth

    On 2/10/2026 9:40 am, marcel hendrix wrote:
    ...
    Luckily, in the case at hand the necessary constants are not magic numbers but result from formulas that can be computed to high precision with 106/128 bit floating point, i.e., *with xd-fp itself*.

    The procedure was therefore to define all FP constants as 0e0. After xd-fp has loaded, the iForth compiler recomputes all XDCONSTANTs using >XD from text generated by a high-precision package like MATLAB's VPA(), and stores the result with XD! directly into the target constant's >BODY areas. I was able to show that all constants turn out to have at least 39 decimals of remaining precision.

    Sounds like the issue I had with DDPI (defining it as two regular floats). Depending on the target system it sometimes worked and sometimes didn't. Relocating the definition to the end of the package and using >DD with a full 32+ digit string was the solution. Same for the XD pack.

    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From albert@albert@spenarnc.xs4all.nl to comp.lang.forth on Fri Oct 2 12:04:56 2026
    From Newsgroup: comp.lang.forth

    In article <119mr1e$c6ka$1@dont-email.me>, marcel hendrix <mhx@iae.nl> wrote: <SNIP>
    This is not a trivial problem, and an example of a practical situation
    where the Forth system is asked to potentially handle >700 digit
    floating point text. (For extended floats, even much more than 700 digits).

    Floats with 700 digits makes no sense. You must do fixed point
    exact calculation with an appropriate error analysis.

    Only the Riemann hypothesis comes to mind. Quantum fluctuations
    prevent future-telling of a galaxy, even if you are given the start
    conditions.


    -marcel
    --
    The Chinese government is satisfied with its military superiority over USA.
    The next 5 year plan has as primary goal to advance life expectancy
    over 80 years, like Western Europe.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From marcel hendrix@mhx@iae.nl to comp.lang.forth on Fri Oct 2 12:28:03 2026
    From Newsgroup: comp.lang.forth

    On 10/2/2026 12:04 PM, albert@spenarnc.xs4all.nl wrote:
    In article <119mr1e$c6ka$1@dont-email.me>, marcel hendrix <mhx@iae.nl> wrote:
    <SNIP>
    This is not a trivial problem, and an example of a practical situation
    where the Forth system is asked to potentially handle >700 digit
    floating point text. (For extended floats, even much more than 700 digits).

    Floats with 700 digits makes no sense. You must do fixed point
    exact calculation with an appropriate error analysis.

    Only the Riemann hypothesis comes to mind. Quantum fluctuations
    prevent future-telling of a galaxy, even if you are given the start conditions.


    -marcel

    I agree. Accepting (and converting) arbitrarily long numeric FP strings
    by a Forth system should not be a top priority.

    As far as I understand it, accepting >700 digit floats does not mean
    that one can make any useful calculations with them. Riemann will have
    to wait.

    -marcel
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From Paul Rubin@no.email@nospam.invalid to comp.lang.forth on Fri Oct 2 12:25:30 2026
    From Newsgroup: comp.lang.forth

    albert@spenarnc.xs4all.nl writes:
    Floats with 700 digits makes no sense.... Only the Riemann hypothesis
    comes to mind.

    You might like to print pictures of very tiny regions of the Mandelbrot
    set. Also, the recent proof of a singularity in the forced
    Navier-Stokes equation built on an earlier proof that used a lot of
    interval arithmetic in a similar situation. IDK if high precision was
    involved though. There's a good video lecture about it,

    https://www.youtube.com/watch?v=TcEefrWrddA

    I remember seeing a similar method used to prove something about the
    Feigenbaum logistic constants. Those have been calculated to 1000s of
    decimal places now. IDK if there are obstacles to computing them to
    much higher precision (like pi), if someone wanted to do that.

    Calculating the quadrillionth digit of pi with the
    Borwein-Bailey-Plouffe formula takes more than 64 bit floats but
    probably not 700 digits. 128 or 256 bit floats might be enough.
    --- Synchronet 3.22a-Linux NewsLink 1.2
  • From albert@albert@spenarnc.xs4all.nl to comp.lang.forth on Sat Oct 3 15:29:36 2026
    From Newsgroup: comp.lang.forth

    In article <119o0vn$20mc2$1@dont-email.me>, marcel hendrix <mhx@iae.nl> wrote: >On 10/2/2026 12:04 PM, albert@spenarnc.xs4all.nl wrote:
    In article <119mr1e$c6ka$1@dont-email.me>, marcel hendrix <mhx@iae.nl> wrote:
    <SNIP>
    This is not a trivial problem, and an example of a practical situation
    where the Forth system is asked to potentially handle >700 digit
    floating point text. (For extended floats, even much more than 700 digits). >>
    Floats with 700 digits makes no sense. You must do fixed point
    exact calculation with an appropriate error analysis.

    Only the Riemann hypothesis comes to mind. Quantum fluctuations
    prevent future-telling of a galaxy, even if you are given the start
    conditions.


    -marcel

    I agree. Accepting (and converting) arbitrarily long numeric FP strings
    by a Forth system should not be a top priority.

    As far as I understand it, accepting >700 digit floats does not mean
    that one can make any useful calculations with them. Riemann will have
    to wait.

    Funny thing is. My humble fp package accepts a 700 digit definition
    of pi. It does it, by starting at the end instead on the beginning of
    the string. Needless precision is no hindrance, it just get lost.
    I normally don't convert the input stream, so there is no copying
    at all if converting from a file that is slurped by the command
    GET-FILE. If reading from standard in, your definition of pi
    should be less that 16K, but you could reassemble ciforth to accommodate
    a larger buffer ...


    -marcel
    --
    The Chinese government is satisfied with its military superiority over USA.
    The next 5 year plan has as primary goal to advance life expectancy
    over 80 years, like Western Europe.
    --- Synchronet 3.22a-Linux NewsLink 1.2