Thread View: comp.lang.forth
11 messages
11 total messages
Started by marcel hendrix
Sun, 18 Oct 2026 16:42
bug in mpfr-3.1.0
Author: marcel hendrix
Date: Sun, 18 Oct 2026 16:42
Date: Sun, 18 Oct 2026 16:42
86 lines
3858 bytes
3858 bytes
iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. I
have found that the mpfr_pow(rop, op1,op2,roundingmode) function does
not work for at least *some* values of op1 (0 and 1 works) that are a
power of 2 (0 and 1 work). The source hints at this, the documentation
does not.
FORTH> LET (3,2): F#** .print
9.0000000000000000000000000000000000000000e+0000 ok
FORTH> LET (1,2): F#** .print
1.0000000000000000000000000000000000000000e+0000 ok
FORTH> LET (2,3): F#** .print @.Inf@e-0003 ok
FORTH> LET (2,2): F#** .print @.Inf@e-0001 ok
FORTH> LET (3,2): F#** .print
9.0000000000000000000000000000000000000000e+0000 ok
FORTH> LET (4,2): F#** .print @.Inf@e-0001 ok
This was found when testing the archimedes.frt program in the current
iForth distribution.
-marcel
(*
* LANGUAGE : ANS Forth with extensions
* PROJECT : Forth Environments
* DESCRIPTION : Hairy floating-point issues
* CATEGORY : Demonstration, using of OPG (see also archimedes.frt)
* AUTHOR : Marcel Hendrix
* LAST CHANGE : August 4, 2012, Marcel Hendrix
* LAST CHANGE : Saturday, December 22, 2012, 22:46, Marcel Hendrix
*)
NEEDS -xopg
REVISION -archimedes "--- Archimedes' PI Version 2.01 ---"
PRIVATES
DOC
(*
Round-off error can affect the convergence and accuracy of iterative
numerical procedures. As an example, Archimedes approximated PI by
calculating the perimeters of polygons inscribing and circumscribing
a circle, starting with hexagons, and successively doubling the number
of sides. As noted above, computations may be rearranged in a way that
is mathematically equivalent but less prone to error (numerical analysis).
Two forms of the recurrence formula for the circumscribed polygon are:
t0 = 1 / sqrt(3)
sqrt(1+ti^2) - 1
first form: ti+1 = ----------------
ti
ti
second form: ti+1 = ----------------
sqrt(1+ti^2) + 1
*)
ENDDOC
.arbitrary.p
: #SHOW ( -- )
0-VALUE GLOCAL t0
LET SQRT(1/3): GLOCAL ti1
LET SQRT(1/3): GLOCAL ti2
CR ." flawed
correct"
#32 0 DO
CR I 3 .R SPACE LET t0=6*ti1*2^double(I()): LET (t0-PI,t0):
.PRINT ." (err=" .PRINT ." )"
4 SPACES LET t0=6*ti2*2^double(I()): LET (t0-PI,t0): .PRINT
." (err=" .PRINT ." )"
LET ti1 = (SQRT(ti1*ti1+1)-1)/ti1:
LET ti2 = ti2/(SQRT(ti2*ti2+1)+1):
LOOP ;
:ABOUT CR ." Try: #SHOW -- approximates PI using two different algorithms."
CR ." .( PI = ) " PI-VALUE .PRINT ;
.ABOUT -archimedes CR
DEPRIVE
(* End of Source *)
Re: bug in mpfr-3.1.0
Author: Krishna Myneni
Date: Mon, 19 Oct 2026 11:01
Date: Mon, 19 Oct 2026 11:01
91 lines
3510 bytes
3510 bytes
On 10/18/26 11:42 AM, marcel hendrix wrote: > iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. I > have found that the mpfr_pow(rop, op1,op2,roundingmode) function does > not work for at least *some* values of op1 (0 and 1 works) that are a > power of 2 (0 and 1 work). The source hints at this, the documentation > does not. > ... > > FORTH> LET (2,3): F#** .print @.Inf@e-0003 ok > FORTH> LET (2,2): F#** .print @.Inf@e-0001 ok > FORTH> LET (3,2): F#** .print I'm currently using MPFR ver 4.2.1. Both mpfr_pow and mpfr_pow_ui appear to be working: 2^3 r1 = 0.200000000000000000000000000000000000000000000000000000000000E1 r2 = 0.300000000000000000000000000000000000000000000000000000000000E1 Using mpfr_pow r1^r2 = 0.800000000000000000000000000000000000000000000000000000000000E1 Using mpfr_pow_ui r1^3 = 0.800000000000000000000000000000000000000000000000000000000000E1 2^2 r1 = 0.200000000000000000000000000000000000000000000000000000000000E1 r2 = 0.200000000000000000000000000000000000000000000000000000000000E1 Using mpfr_pow r1^r2 = 0.400000000000000000000000000000000000000000000000000000000000E1 Using mpfr_pow_ui r1^2 = 0.400000000000000000000000000000000000000000000000000000000000E1 ok -- KM === begin code === include ans-words include modules include syscalls include mc include asm include strings include lib-interface include libs/gmp/libmpfr include libs/gmp/mpfr-utils DECIMAL 256 mpfr_set_default_prec \ provides 77 sig. decimal digits mpfr_t r1 mpfr_t r2 mpfr_t dst r1 mpfr_init r2 mpfr_init dst mpfr_init cr cr .( 2^3) cr r1 2 GMP_RNDN mpfr_set_ui drop r2 3 GMP_RNDN mpfr_set_ui drop .( r1 = ) r1 60 mpfr. cr .( r2 = ) r2 60 mpfr. cr cr .( Using mpfr_pow) cr dst r1 r2 GMP_RNDN mpfr_pow drop .( r1^r2 = ) dst 60 mpfr. cr cr .( Using mpfr_pow_ui) cr dst r1 3 GMP_RNDN mpfr_pow_ui drop .( r1^3 = ) dst 60 mpfr. cr cr .( 2^2) cr r2 2 GMP_RNDN mpfr_set_ui drop .( r1 = ) r1 60 mpfr. cr .( r2 = ) r2 60 mpfr. cr cr .( Using mpfr_pow) cr dst r1 r2 GMP_RNDN mpfr_pow drop .( r1^r2 = ) dst 60 mpfr. cr cr .( Using mpfr_pow_ui) cr dst r1 2 GMP_RNDN mpfr_pow_ui drop .( r1^2 = ) dst 60 mpfr. cr === end code ===
Re: bug in mpfr-3.1.0
Author: marcel hendrix
Date: Mon, 19 Oct 2026 15:37
Date: Mon, 19 Oct 2026 15:37
24 lines
2137 bytes
2137 bytes
On 10/19/2026 1:01 PM, Krishna Myneni wrote: > On 10/18/26 11:42 AM, marcel hendrix wrote: >> iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. >> I have found that the mpfr_pow(rop, op1,op2,roundingmode) function >> does not work for at least *some* values of op1 (0 and 1 works) that >> are a power of 2 (0 and 1 work). The source hints at this, the >> documentation does not. >> > ... >> >> FORTH> LET (2,3): F#** .print @.Inf@e-0003 ok >> FORTH> LET (2,2): F#** .print @.Inf@e-0001 ok >> FORTH> LET (3,2): F#** .print > I'm currently using MPFR ver 4.2.1. > > Both mpfr_pow and mpfr_pow_ui appear to be working: [..] Thanks! I'll upgrade to gmp 5.0 and mpfr 4.2.2 shortly. Meanwhile, I found that the present mpfr_pow_ui works correctly. As I don't need to compute 2.5362^2.1 that often, I'll get by :--) -marcel
Re: bug in mpfr-3.1.0
Author: Krishna Myneni
Date: Mon, 19 Oct 2026 16:45
Date: Mon, 19 Oct 2026 16:45
33 lines
2441 bytes
2441 bytes
On 10/19/26 10:37 AM, marcel hendrix wrote: > On 10/19/2026 1:01 PM, Krishna Myneni wrote: >> On 10/18/26 11:42 AM, marcel hendrix wrote: >>> iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. >>> I have found that the mpfr_pow(rop, op1,op2,roundingmode) function >>> does not work for at least *some* values of op1 (0 and 1 works) that >>> are a power of 2 (0 and 1 work). The source hints at this, the >>> documentation does not. >>> >> ... >>> >>> FORTH> LET (2,3): F#** .print @.Inf@e-0003 ok >>> FORTH> LET (2,2): F#** .print @.Inf@e-0001 ok >>> FORTH> LET (3,2): F#** .print >> I'm currently using MPFR ver 4.2.1. >> >> Both mpfr_pow and mpfr_pow_ui appear to be working: > [..] > > Thanks! I'll upgrade to gmp 5.0 and mpfr 4.2.2 shortly. > > Meanwhile, I found that the present mpfr_pow_ui works correctly. As I > don't need to compute 2.5362^2.1 that often, I'll get by :--) > It would be truly bad if mpfr_pow_ui didn't work! If you know where I can get the Win32 DLL for MPFR 4.2, please let me know. I would like to make it available with kForth-Win32. -- KM
Re: bug in mpfr-3.1.0
Author: marcel hendrix
Date: Mon, 19 Oct 2026 17:09
Date: Mon, 19 Oct 2026 17:09
17 lines
1954 bytes
1954 bytes
On 10/19/2026 6:45 PM, Krishna Myneni wrote: >> >> Meanwhile, I found that the present mpfr_pow_ui works correctly. As I >> don't need to compute 2.5362^2.1 that often, I'll get by :--) >> > > It would be truly bad if mpfr_pow_ui didn't work! If you know where I > can get the Win32 DLL for MPFR 4.2, please let me know. I would like to > make it available with kForth-Win32. > Last time I had to compile it myself, as there where lots of macros that I wanted as functions, and because the int and return types were inconvenient. I am sure that has improved. I'll let you know once I get to it. -marcel
Re: bug in mpfr-3.1.0
Author: marcel hendrix
Date: Sat, 24 Oct 2026 16:13
Date: Sat, 24 Oct 2026 16:13
22 lines
2218 bytes
2218 bytes
On 10/19/2026 1:01 PM, Krishna Myneni wrote: > On 10/18/26 11:42 AM, marcel hendrix wrote: >> iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. >> I have found that the mpfr_pow(rop, op1,op2,roundingmode) function >> does not work for at least *some* values of op1 (0 and 1 works) that >> are a power of 2 (0 and 1 work). The source hints at this, the >> documentation does not. [..] I finished debugging the mpc (arbitrary complex) libraries (same timestamps as mpfr). The mpc_pow function had the same problem as mpfr_pow. It is a bit worse, as mpc_pow_si now also has this problem (mpfr_pow_si is ok). I 'fixed' the problem by adding 1e-250 to the function argument in case it is exactly 2. There were other problems too, but they didn't affect the iForth ferrari, zgamma and zeta examples. I assume the pow and pow_... functions in the mpfr and mpc have this particular problem fixed by now. -marcel
Re: bug in mpfr-3.1.0
Author: Krishna Myneni
Date: Sat, 24 Oct 2026 18:29
Date: Sat, 24 Oct 2026 18:29
28 lines
2437 bytes
2437 bytes
On 10/24/26 11:13 AM, marcel hendrix wrote: > On 10/19/2026 1:01 PM, Krishna Myneni wrote: >> On 10/18/26 11:42 AM, marcel hendrix wrote: >>> iForth uses the mpfr 3.1.0 release, and possibly other Forths do too. >>> I have found that the mpfr_pow(rop, op1,op2,roundingmode) function >>> does not work for at least *some* values of op1 (0 and 1 works) that >>> are a power of 2 (0 and 1 work). The source hints at this, the >>> documentation does not. > [..] > > I finished debugging the mpc (arbitrary complex) libraries (same > timestamps as mpfr). The mpc_pow function had the same problem as > mpfr_pow. It is a bit worse, as mpc_pow_si now also has this problem > (mpfr_pow_si is ok). I 'fixed' the problem by adding 1e-250 to the > function argument in case it is exactly 2. > There were other problems too, but they didn't affect the iForth > ferrari, zgamma and zeta examples. > > I assume the pow and pow_... functions in the mpfr and mpc have this > particular problem fixed by now. > If you post these new calculations which have problems in ver 3.1, I can test them with ver 4.1. -- KM
Re: bug in mpfr-3.1.0
Author: marcel hendrix
Date: Sat, 24 Oct 2026 22:44
Date: Sat, 24 Oct 2026 22:44
72 lines
4193 bytes
4193 bytes
On 10/24/2026 8:29 PM, Krishna Myneni wrote:
> On 10/24/26 11:13 AM, marcel hendrix wrote:
>> On 10/19/2026 1:01 PM, Krishna Myneni wrote:
[..]
>> I assume the pow and pow_... functions in the mpfr and mpc have this
>> particular problem fixed by now.
>>
>
> If you post these new calculations which have problems in ver 3.1, I can
> test them with ver 4.1.
It is unlikely that my code will work for you, but here is the straight
library call to push the mpfr function results on the F#stack ( iForth's
arbitrary float stack). For mpfr_pow it was
\ : F#** ( -- ) F#NOS DUP F#TOS F#RND mpfr_pow DROP F#DROP ; \ tos <=
tos^n, does not work for n=powerof[2]
The fix is
: F#^N ( n -- ) F#TOS DUP ROT F#RND mpfr_pow_si DROP ; \ tos <=
tos^n ) this DOES work when n is 2
: F#** ( -- ) F#->N F#^N ; \ tos <= tos^n, does work for n=powerof[2]
Here is a test showing original and fixed code:
FORTH> : bad_F#** ( -- ) F#NOS DUP F#TOS F#RND mpfr_pow DROP F#DROP ; ok
FORTH> F#2.0 F#3.0 bad_F#** F#. @.Inf@e-0001 ok
FORTH> F#2.0 F#3.0 F#** F#.
8.0000000000000000000000000000000000000000e+0000 ok
---( * )---
I've found additional issues with mpfr_zeta. Here the Borwein algorithm
#2 delivers results as good as Mathematica, while mpfr_zeta is far from
the expected 40 decimals.
: F#ZETA ( -- ) F#TOS DUP F#RND mpfr_zeta DROP ; ( tos <= zeta[tos] )
Here is the correct output of the Borwein algorithm ( IIRC you have that
) in iForth vs. the mpfr result for a couple of integer inputs:
FORTH> .zeta \ input -- Borwein -- mpfr
2 1.6449340668482264364724151666460251892189e+0000
1.3116821137232264364724151666460251892189e+0000
3 1.2020569031595942853997381615114499907649e+0000
1.0592003052592036603997381615114499907649e+0000
4 1.0823232337111381915160036965411679027747e+0000
1.0156565710181145098753786965411679027747e+0000
5 1.0369277551433699263313654864570341680570e+0000
1.0046696906572835577705744708320341680570e+0000
6 1.0173430619844491397145179297909205279018e+0000
1.0014700461116642494800475623592799029018e+0000
7 1.0083492773819228268397975498497967595998e+0000
1.0004752616338931211207065516770367498342e+0000
8 1.0040773561979443393786852385086524652589e+0000
1.0001557875704933729187388808635580705751e+0000
9 1.0020083928260822144178527692324120604856e+0000
1.0000514456636556000450761571605095693333e+0000
10 1.0009945751278180853371459589003190170060e+0000
It is unlikely to be a problem with the interface as, e.g., the mpfr_erf
function (~ same call interface) gives the expected results when
compared to Mathematica.
It would help me to see the output of the mpfr and/or mpc for the above
functions. However, it is starting to look like I'm wasting my time with
decade old dll's.
-marcel
Re: bug in mpfr-3.1.0
Author: Krishna Myneni
Date: Sun, 25 Oct 2026 14:48
Date: Sun, 25 Oct 2026 14:48
49 lines
3098 bytes
3098 bytes
On 10/24/26 17:44, marcel hendrix wrote:
> On 10/24/2026 8:29 PM, Krishna Myneni wrote:
...
>> If you post these new calculations which have problems in ver 3.1, I
>> can test them with ver 4.1.
>
...
>
> I've found additional issues with mpfr_zeta. Here the Borwein algorithm
> #2 delivers results as good as Mathematica, while mpfr_zeta is far from
> the expected 40 decimals.
>
> : F#ZETA ( -- ) F#TOS DUP F#RND mpfr_zeta DROP ; ( tos <= zeta[tos] )
>
> Here is the correct output of the Borwein algorithm ( IIRC you have
> that ) in iForth vs. the mpfr result for a couple of integer inputs:
>
> FORTH> .zeta \ input -- Borwein -- mpfr
> 2 1.6449340668482264364724151666460251892189e+0000
> 1.3116821137232264364724151666460251892189e+0000
> 3 1.2020569031595942853997381615114499907649e+0000
> 1.0592003052592036603997381615114499907649e+0000
> 4 1.0823232337111381915160036965411679027747e+0000
I have a double precision version of the zeta function based on your
code, with automated tests from your computed reference values in the
kForth-x packages: forth-src/fsl/extras/zzeta.4th
=== snippet from zzeta.4th ===
...
\ 60 digit reference values computed by Marcel Hendrix
\ using MPFR/MPC version of zzeta.
1e 0e f/ fconstant +inf
cr
TESTING ZETA
t{ 0e zeta -> -5.00000000000000e-01 r}t
t{ 1e zeta -> +inf r}t
t{ 2e zeta -> 1.6449340668482264364724151666460251892190e r}t
t{ 3e zeta -> 1.2020569031595942853997381615114499907651e r}t
t{ 4e zeta -> 1.0823232337111381915160036965411679027749e r}t
...
=== end snippet ===
Later today I will code an mpfr version and print out the 60 digit
reference values.
--
Krishna
Re: bug in mpfr-3.1.0
Author: Krishna Myneni
Date: Mon, 26 Oct 2026 20:35
Date: Mon, 26 Oct 2026 20:35
100 lines
4841 bytes
4841 bytes
On 10/25/26 9:48 AM, Krishna Myneni wrote:
> On 10/24/26 17:44, marcel hendrix wrote:
>> On 10/24/2026 8:29 PM, Krishna Myneni wrote:
> ...
>>> If you post these new calculations which have problems in ver 3.1, I
>>> can test them with ver 4.1.
>>
> ...
>>
>> I've found additional issues with mpfr_zeta. Here the Borwein
>> algorithm #2 delivers results as good as Mathematica, while mpfr_zeta
>> is far from the expected 40 decimals.
>>
>> : F#ZETA ( -- ) F#TOS DUP F#RND mpfr_zeta DROP ; ( tos <= zeta[tos] )
>>
>> Here is the correct output of the Borwein algorithm ( IIRC you have
>> that ) in iForth vs. the mpfr result for a couple of integer inputs:
>>
>> FORTH> .zeta \ input -- Borwein -- mpfr
>> 2 1.6449340668482264364724151666460251892189e+0000
>> 1.3116821137232264364724151666460251892189e+0000
>> 3 1.2020569031595942853997381615114499907649e+0000
>> 1.0592003052592036603997381615114499907649e+0000
>> 4 1.0823232337111381915160036965411679027747e+0000
> I have a double precision version of the zeta function based on your
> code, with automated tests from your computed reference values in the
> kForth-x packages: forth-src/fsl/extras/zzeta.4th
>
> === snippet from zzeta.4th ===
> ...
> \ 60 digit reference values computed by Marcel Hendrix
> \ using MPFR/MPC version of zzeta.
>
> 1e 0e f/ fconstant +inf
> cr
> TESTING ZETA
> t{ 0e zeta -> -5.00000000000000e-01 r}t
> t{ 1e zeta -> +inf r}t
> t{ 2e zeta -> 1.6449340668482264364724151666460251892190e r}t
> t{ 3e zeta -> 1.2020569031595942853997381615114499907651e r}t
> t{ 4e zeta -> 1.0823232337111381915160036965411679027749e r}t
> ...
> === end snippet ===
>
MPFR has an instrinsic Riemann zeta function. I used it to compute and
print the values to 60 digits:
test
0 -0.50000000000000000000000000000000000000000000000000000000000E0
1 0.@Inf@000000000000000000000000000000000000000000000000000000E0
2 0.164493406684822643647241516664602518921894990120679843773556E1
3 0.120205690315959428539973816151144999076498629234049888179227E1
4 0.108232323371113819151600369654116790277475095191872690768298E1
5 0.103692775514336992633136548645703416805708091950191281197419E1
6 0.101734306198444913971451792979092052790181749003285356184241E1
7 0.100834927738192282683979754984979675959986356056523870641728E1
8 0.100407735619794433937868523850865246525896079064985002032911E1
9 0.100200839282608221441785276923241206048560585139488875654860E1
10 0.100099457512781808533714595890031901700601953156447751725779E1
Presently this only works in kForth-32. The 64-bit library interface
does not include the special functions yet. I will have to add these to
work under kForth-64. I am using MPFR 4.2.
--
KM
=== begin code ===
include ans-words
include modules
include syscalls
include mc
include asm
include strings
include lib-interface
include libs/gmp/libmpfr
include libs/gmp/mpfr-utils
\ Set precision before initializing mp vars
256 mpfr_set_default_prec
mpfr_t dst
mpfr_t num
dst mpfr_init
num mpfr_init
\ Compute the zeta function of n
: rzeta ( n -- a )
num swap GMP_RNDN mpfr_set_si drop
dst num GMP_RNDN mpfr_zeta drop
dst
;
: test 11 0 do i dup 2 .r 2 spaces rzeta dst 60 mpfr. cr loop cr ;
=== end code ===
Re: bug in mpfr-3.1.0
Author: marcel hendrix
Date: Tue, 27 Oct 2026 00:21
Date: Tue, 27 Oct 2026 00:21
40 lines
2859 bytes
2859 bytes
On 10/26/2026 9:35 PM, Krishna Myneni wrote: > On 10/25/26 9:48 AM, Krishna Myneni wrote: >> On 10/24/26 17:44, marcel hendrix wrote: >>> On 10/24/2026 8:29 PM, Krishna Myneni wrote: >> ... >>>> If you post these new calculations which have problems in ver 3.1, I >>>> can test them with ver 4.1. [..] >>> FORTH> .zeta \ input -- Borwein -- mpfr >>> 2 1.6449340668482264364724151666460251892189e+0000 >>> 1.3116821137232264364724151666460251892189e+0000 >>> 3 1.2020569031595942853997381615114499907649e+0000 >>> 1.0592003052592036603997381615114499907649e+0000 >>> 4 1.0823232337111381915160036965411679027747e+0000 [..] > MPFR has an instrinsic Riemann zeta function. I used it to compute and > print the values to 60 digits: > > test [..] > 2 0.164493406684822643647241516664602518921894990120679843773556E1 > 3 0.120205690315959428539973816151144999076498629234049888179227E1 > 4 0.108232323371113819151600369654116790277475095191872690768298E1 [..] > Presently this only works in kForth-32. The 64-bit library interface > does not include the special functions yet. I will have to add these to > work under kForth-64. I am using MPFR 4.2. Thank you! This clearly shows that MPFR 4.2 matches Borwein's code and Mathematica, where MPFR 3.0.1 does not. I should upgrade, as this makes all older versions of the MPFR suspect. -marcel
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