gmp 6.2.1 and mpfr 4.1.0

General FreeBASIC programming questions.
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Re: gmp 6.2.1 and mpfr 4.1.0

Post by meucat »

Hello srvaldez , thanks a lot

I copied all .bi and dlls from your gmp-6.2.0 package to my win64 , subfolders and this time I got do pass the initial step "#Include gmp.bi"

Then I tried compile your gmpf-fb test.bas (which I renamed to gmptest1.bas) and this time I got these errors

gmptest1.bas(64) error 14: Expected identifier, found 'mpz_t' in 'dim as mpz_t k'
gmptest1.bas(65) error 42: Variable not declared, k in 'mpz_init(@k)'
gmptest1.bas(67) error 14: Expected identifier, found 'k' in 'mpz_fac_ui(@k, 100000)'

I didn´t replace my initial gmp.bi included into FreeBasic with your gmp.bi in 6.2.0 pack , I don´t know if this could be causing errors.

These gmp.bi has some difference in size since mine is 79.756 bytes (2019) and yours is 78.914 (2016)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

I think that you guessed it, replace the gmp.bi with the one from my archive
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Re: gmp 6.2.1 and mpfr 4.1.0

Post by meucat »

Hello srvaldez , great ! I replaced gmp.bi with your gmp.bi file and got compile your test program with no errors

but.. when I run it only prints almost instantly "automatic casting from mpq 1/3 to mpf" and then program stops with no errors or warnings to check

It appears program would continue printing some results like SQR, Fibonacci and so with different precision digits, but it halts in between the "print...." and "for xf="1".... commands

mpq_init(@q2)
mpq_set_ui(@q2, 1, 3) 'q2 = 1/3
zf=q2 'automatic casting from mpq to mpf
print "automatic casting from mpq 1/3 to mpf ";zf.toString("%45.40f") 'other examples: zf.toString("%45.40e"), zf.toString

for xf="1" to 10 step "1"
yf=gmpf_Sqr(xf)
Print "Sqr(";xf;") =",yf.toString("f") 'floating point format, all digits
next

I compiled program just with command "fbc gmptest1.bas" (no errors) , and don´t know if I should set some parameters to compile properly

I am trying understand the syntax and variable defintions from GMP before writing my own programs

(by the way... I still use UBASIC in my old windows XP machine to run arbitrary precision so want to move these ubasic programs to FreeBasic environment)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

meucat wrote: but.. when I run it only prints almost instantly "automatic casting from mpq 1/3 to mpf" and then program stops with no errors or warnings to check
not sure what you mean, but do this, at the end of the program put: Sleep
Sleep will cause the program to wait until a key is pressed, this prevents the console from closing and allows you see the program output
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Re: gmp 6.2.1 and mpfr 4.1.0

Post by meucat »

Hello srvaldez

I think current program doesn´t process any GMP command and it stops at the place I put "program halts at this point" because it only prints "automatic casting from mpq 1/3 to mpf " and then stops returning to main console, not executing last "sleep" command , and not printing any other thing like "Sqr(nnnn)=" or so.

This is main console display after executing program

C:\FreeBASIC-1.07.1-win64>gmptest1
automatic casting from mpq 1/3 to mpf
C:\FreeBASIC-1.07.1-win64>

I tested program against any other GMP command, for instance "zf.tostring()" or else " for "1" to "10" " and program halts immediately before execute these commands with no advice. I suspect there is some issue with 64 bits x 32 bits somewhere in .bi or .dll files.

Take a look source I am compiling with no errors at all



dim shared as long mpf_digits_precision=50

#include once "gmpf-overload.bi"

'==================================================================
'test

set_precision(10) 'set precision before defining variables

dim as gmpf xf, yf, zf 'automatic initialization
dim as __mpq_struct q2
dim as string s
dim as mp_exp_t xp
dim as integer i, j

mpq_init(@q2)
mpq_set_ui(@q2, 1, 3) 'q2 = 1/3
zf=q2 'automatic casting from mpq to mpf
print "automatic casting from mpq 1/3 to mpf ";zf.toString("%45.40f") 'other examples: zf.toString("%45.40e"), zf.toString

´ *********************
´ program halts at this point
´**********************

for xf="1" to 10 step "1"
yf=gmpf_Sqr(xf)
Print "Sqr(";xf;") =",yf.toString("f") 'floating point format, all digits
next
print
print "change default precision to 50 digits"
print
set_precision(50) 'change default precision
xf.set_prec 'must also change precision of variables previously defined
yf.set_prec
zf.set_prec
for xf="1" to 10 step "1"
yf=gmpf_Sqr(xf)
Print "Sqr(";xf;") =",yf.toString("%45.40f") 'floating point format, digits.decimals
next
zf=q2
print "automatic casting from mpq 1/3 to mpf ->";zf.toString("%45.40f")
print "print free format ";zf 'floating point format, all digits
print "print sci format ";zf.toString("e") 'sci format, all digits
print

set_precision(2880) 'set precision to 2880 digits
xf.set_prec 'since variable was previously initialized with 50 digits prec.
'it needs to be re-initialized to the higher prec.
print "calculate the first 116 fibonnaci numbers"
s=string(23,"9")+"8"+string(24,"9")
xf=s
xf=1/xf
s=xf.toString("%2885.2880f")
s=mid(s,4)
'mpf_get_str (s, @xp, 10, 2880, @xf.num)
j=1
for i=1 to 58
print mid(s,j,24),mid(s,j+24,24)
j+=48
next

mpq_clear(@q2)
'automatic cleanup of mpf variables

'print "press any to exit ";
'sleep

dim as mpz_t k
mpz_init(@k)
dim as double t=timer
mpz_fac_ui(@k, 100000)
t=timer-t
Print "fac time ";t

sleep
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Post by meucat »

Hello srvaldez

Just to simplify problem, I shorted program to this one.

When I remove command xf = "1" and execute SQR with empty string xf, it reaches end of program and prints ok "before SQR" and "after SQR" at all , but whenever I let execute xf="1" the program only print "before SQR" and then stops.

dim shared as long mpf_digits_precision=50

#include once "gmpf-overload.bi"

'==================================================================
'test

set_precision(10) 'set precision before defining variables

dim as gmpf xf, yf, zf 'automatic initialization
dim as __mpq_struct q2
dim as string s
dim as mp_exp_t xp
dim as integer i, j

mpq_init(@q2)
mpq_set_ui(@q2, 1, 3) 'q2 = 1/3
zf=q2 'automatic casting from mpq to mpf

print "before SQR"

xf="1"
yf=gmpf_Sqr(xf)

print "after SQR"
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

hi meucat
I am at a loss as to why it doesn't work for you, may I ask you that you rename your present FreeBasic folder and install the latest official release from the news section of this forum?
also, re-download the gmp+mpfr archive and after extracting it drag-and-drop your new FreeBasic folder on top of drag&dropFBfolder.bat answer yes to any prompts, then try again and let me know how it went.

<edit> just follow the above steps, no need to manually copy lib or bi files
and try the examples in the newly extracted gmp+mpfr folder without any changes at first other than perhaps to put a sleep statement at the end of the program, to make sure that they run ok

the reason that I forget to put a sleep at the end is that I use the geany IDE and the sleep command is not needed if program is launched from geany
Last edited by srvaldez on Feb 24, 2021 12:00, edited 2 times in total.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

meucat wrote: (by the way... I still use UBASIC in my old windows XP machine to run arbitrary precision so want to move these ubasic programs to FreeBasic environment)
if you need any trig or exponential functions then I recommend that you use mpfr instead of gmp
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Re: gmp 6.2.1 and mpfr 4.1.0

Post by meucat »

Hello drvaldez

I unpacked FB and it created folder "FreeBASIC-1.07.3-win64-gcc-5.2.0" then unpacked GMP+MPFR and it created folder "gmp+mpfr". Into this folder there is "drag&dropFBfolder.bat" but I am lost about "drag and drop" operation, and windows doesn´t allow to me execute this BAT file for security reasons.

Since I'm more used to working in DOS command style I tried create a freebasic and gmp shorcuts on windows main screen just to execute drag & drop but it didn´t work in any way. Wouldn´t it possible to execute this operation by making "copy & paste" of something else instead?

thanks again
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

where is the FreeBASIC-1.07.3-win64-gcc-5.2.0 folder located?
is it in restricted area like "C:\Program Files" ?
you are only supposed to run the batch file by drag-and-drop the FreeBASIC-1.07.3-win64-gcc-5.2.0 folder onto it
anyway, all the batch file does is copy the lib and bi files to the proper directories in your FB folder
meucat
Posts: 12
Joined: Jan 20, 2014 1:53

Re: gmp 6.2.1 and mpfr 4.1.0

Post by meucat »

hello drvaldes ... aleluiah !

At first program compiled ok but when executing says "libgmp-10.dll" not found , then I copied this dll file to root folder and miracle was made .

Sqr( 1) = 1.000000000000000000
Sqr( 2) = 1.414213562373095049
Sqr( 3) = 1.732050807568877294
Sqr( 4) = 2.000000000000000000
Sqr( 5) = 2.236067977499789696
Sqr( 6) = 2.449489742783178098
Sqr( 7) = 2.645751311064590590
Sqr( 8) = 2.828427124746190098
Sqr( 9) = 3.000000000000000000
Sqr( 10) = 3.162277660168379332

.....

(until print 116 fibonacci numbers)

program appears to run fast, and soon I will making some testings on it

thanks a lot for your help
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

congrats meucat :-)
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: gmp 6.2.1 and mpfr 4.1.0

Post by jdebord »

According to mpfr-fb.bi :

Code: Select all

Constructor mpfr ( )
	mpfr_init2(@num, mpfr_digits_precision * 3.33)
	mpfr_set_si(@num, 0, mpfr_rounding_mode)
End Constructor
Does this mean that the number of bits is equal to 3.33 times the number of digits (rounded, of course)

E. g. for a standard double precision number (about 16 digits) :

16 * 3.33 ~ 53 bits for the mantissa (+ 11 bits for the exponent of 2 = 64 bits total)
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: gmp 6.2.1 and mpfr 4.1.0

Post by srvaldez »

Hi jdebord
3.33 is a rounded up value from 1/log10(2), I figured that 3.33 was a practical value for this purpose.
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: gmp 6.2.1 and mpfr 4.1.0

Post by jdebord »

Thank you srvaldez. Yes, 3.33 is practical :)

I have another question : is there a reason to declare some functions as operators (e. g. Acos) and others as functions (e. g. Acosh) ?
Post Reply