PDA

View Full Version : GMP library: free library for arbitrary precision arithmetic



ErosOlmi
23-12-2007, 19:38
In response to Sandy question about rounding problems found into calculator gui examples ... http://community.thinbasic.com/index.php?topic=1406.0
I was searching for a library able to make calculations with arbitrary precision and able to output results with the wanted precision into a string.

I found GMP library at http://gmplib.org/
and the corresponsing compiled DLL (version 4.1.2 at http://www.cs.nyu.edu/exact/core/gmp/)

Made some tests and attached you can find what I was able to get so far. Adding new exported functions to the script is quite easy but I stopped for a while in order to better understand the library.

In the attached file you will find the following files to be placed into a testing directory you like:

gmp.dll (dll library)
gmp.h (C header file used to understand sub/functions declares)
gmp.tbasic (thinBasic example)
gmp-man-4.2.2.pdf (manual)


Have fun with it.
I will work on it more in new year because it seems a good starting.

Ciao
Eros

_________________________________________________
PS:
GMP library works on integer, rational and floating point numbers. It has 323 exported functions! I just declared few of them working on floating point numbers in order to test the possibility to have this library as standard in one of the next thinBasic distribution. Tests went fine so the possibility to have GMP as thinBasic standard is quite high.

If someone succede in declaring more functions (using C header file and PDF maual), please post them here and I will add into final thinBasic include file. Thanks.

kryton9
24-12-2007, 01:12
Eros, I unzipped it and ran it from the folder I unzipped too and got a crash. Should I move the dll some place special? I thought it would read it from the same place as the script?

Michael Clease
24-12-2007, 02:03
Same here

sandyrepope
24-12-2007, 03:24
Same here. I tried moving the dll to Lib but this didn't make any difference.

Thanks
Sandy

I was playing around with the script and got an error message I don't understand.

ErosOlmi
24-12-2007, 10:04
You are right.
Seems it is working only on my development laptop while on other computer I have I get GPF too.
I will check what's the problem. Sorry about that.

ErosOlmi
24-12-2007, 10:18
Stupid me. I changed the way BYREF strings (both dynamic and ASCIIZ) are passed to external DLLs, so I needed also to distribute thinCore.dll
Please find attached to first post the latest thinCore.dll as separated file so you do not have to download again all.

Put thinCore.dll into thinBasic installation directory replacing current one.
All the rest can be placed into a sample script directory. If experiments will go fine, in one of the next future release GMP will be incorporated as standard library with a dedicated include file.

Let me know.
Eros

Michael Hartlef
24-12-2007, 10:50
Thanks Eros, I hope I find some spare minutes to check this out.

zak
29-12-2008, 13:35
hi
thanks for introducing us to GMP math Library, and as suggested i have added another functions so the available now for mpf (+ - * / sqrt pow), i have copied the form from textbox examples, but i need to know how to change the font type and font size of the textboxes.
from site :
ftp://deltatrinity.dyndns.org/gmp-4.2.1_DLL_SharedLibs/pentium4/
a more recent gmp dll v 4.2.1 called libgmp-3.dll rename it to gmp.dll
and about precision there is in the site:
http://www.nabble.com/Re:-about-precision-and-format-p5313487.html
someone said that if we choose precision 2048 then the number of digits at the right of the mantissa is 2048*log10(2) = 616.51 digits.
so the precision used here 2048
try pow 2.2 , 3 will produce 10.647999...up to digit 616 from the period
second question: can we color the period and make it a bigger size so we can discriminate the left integer side and the right fractional side especially if the period is located somewhere far in a big number
the calculator_2 example in samplescripts by Psch, is excellent, i may borrow it's structure once i have more functions, clicking on buttons are easier than typing
one drawback of the GMP library is the lacking of Logarithm function, i think it is available in mpfr library
another notice: is that the pow function is restricted to an integer exponent
regards

Petr Schreiber
29-12-2008, 17:37
Hi Zak,

very interesting code.
I attach version, where:

operations presented in form of combobox
controls are getting bigger when resizing
integer and floating point part of the result have different colors
dialog is handled via callback - much less CPU intensive script


Hope you like it :),
Petr

UPDATED ATTACHEMENT

zak
29-12-2008, 18:44
yes Petr it is a fantastic code , full of ideas and inventions, i will spend some time in studying it. in the next month (next year) i will try to add the integer numbers (MPZ) because they have the primality checking, and the next_prime function.
you missed to delete the line number 143 in the subtraction code,it gives an error.
thanks very much
and thanks for Eros for his ideas and declarations.

Petr Schreiber
29-12-2008, 20:44
Hi Zak,

I corrected the bug on line 143 and added operation selection in combobox :)


Petr

GSAC3
29-12-2008, 22:29
Eros--

Could you point me to the URL where I can find you'r revised thinCore.dll?

I can't seem to locate it -- must be having a "senior moment".

Don

ErosOlmi
29-12-2008, 23:45
Don,

the origin of this thread are very old, date back in 2007 (see dates of first posts of this thread) so current thinBasic version already has all the necessary things done.
Just use latest thinBasic and you will have all what you need.

Ciao
Eros

zak
30-12-2008, 06:30
hello Petr
can you add precision, in a different list, so the user can choose what he want from may be four choices, since you choose precision as 10, it is very small. the choices you may have is 1000 digits to the right of the period, 500, 100.
internally you will calculate the number for gmp as:
dim prec as long
'suppose the user want to choose 1000 digits:
prec = 1000/log10(2) = 3321.9280948873623478703194294894
prec=int(prec)
global Precision as long value prec

by the way, how do you design your forms and controls, i am thinking to use vb6 design then just copy the "geometric" coordinates for the controls

see you next year.

Petr Schreiber
30-12-2008, 12:07
Hi Zak,

interesting idea. I must admit I do not understand how precision currently works, or maybe what are the formating rules for output. Will check it out next year ;D

Regarding dialog design - I design dialogs mostly via code directly.
It would be nice community project to have dialog designer written in ThinBasic itself, I am sure it is possible, just have look at SampleScripts/UI/MDI/MDI_Test_DialogEdit.tbasic.


Petr

jack
31-12-2008, 04:57
I made an attempt at extracting the functions from the gmp manual, but left out the file and random functions

José Roca
31-12-2008, 08:34
A mingw compilation for Windows of version 4.2.4 is available here:
http://sourceforge.net/project/showfiles.php?group_id=204414&package_id=245196&release_id=649190

zak
31-12-2008, 12:16
hello
thanks jack for your functions , it is the best gift in the new year, i
am sure the forum people will be happy when they return from holidays.
i have utilized your declarations to add the isprime, nextprime. factorial functions to the calc_v0_2.tbasic by Petr, and have added the (reset outb) before the sprint...
to José Roca i have downloaded the new dll and it is working well, thanks for the link

José Roca
31-12-2008, 12:33
I have a translation for PowerBASIC. Will check with the translation for thinBasic. After a first look, I see some discrepancies like the use of LONG PTR, e.g.



DECLARE FUNCTION mpz_get_d_2exp CDECL LIB "gmp.dll" Alias "__gmpz_get_d_2exp" (BYVAL exp AS LONG ptr, BYREF op AS mpz) AS DOUBLE


In BASIC is more adequate to use:



DECLARE FUNCTION mpz_get_d_2exp CDECL LIB "gmp.dll" Alias "__gmpz_get_d_2exp" (BYREF exp AS LONG, BYREF op AS mpz) AS DOUBLE


Also, unless thinBasic does it differently...



DECLARE SUB mpz_array_init CDECL LIB "gmp.dll" Alias "__gmpz_array_init" (BYREF integer_array() AS mpz, BYVAL array_size AS LONG, BYVAL fixed_num_bits AS LONG)


should be



DECLARE SUB mpz_array_init CDECL LIB "gmp.dll" Alias "__gmpz_array_init" (BYREF integer_array AS mpz, BYVAL array_size AS LONG, BYVAL fixed_num_bits AS LONG)


If you use integer_array(), PB will pass the array descriptor instead of a pointer to the first element of the array.

jack
31-12-2008, 12:44
thanks for the tips José :)

Petr Schreiber
31-12-2008, 16:06
Thanks Jack,

nice example, worked well on my PC :)


Petr

José Roca
02-01-2009, 03:47
My translation for PowerBASIC is available here:
http://www.jose.it-berater.org/smfforum/index.php?topic=2967.0

jack
02-01-2009, 05:29
José I tested it in ThinBasic and the only thing it did not like was the the two macros, commenting them out and it works.

thank you José :)

Petr Schreiber
02-01-2009, 11:38
Hi Jack,

Josés headers are very good, MACROs are feature of PowerBASIC, but in this case we can use wrapper functions instead of those naughty macros:


FUNCTION mpn_divexact_by3(BYREF dst AS DWORD, BYREF src AS DWORD, BYVAL nsize AS LONG) AS DWORD
FUNCTION = mpn_divexact_by3c(dst, src, nsize, 0)
END FUNCTION

FUNCTION mpn_divmod_1(BYREF qp AS DWORD, BYREF np AS DWORD, BYVAL nsize AS LONG, BYVAL dlimb AS DWORD) AS DWORD
FUNCTION = mpn_divrem_1(qp, 0, np, nsize, dlimb)
END FUNCTION

jack
03-01-2009, 01:01
thanks Petr, I was wondering if thinbasic users would be interested in a free public domain multiprecision integer library?
LibTomMath is such a library the author also wrote LibTomFloat, his web site http://math.libtomcrypt.com/