• Welcome to Jose's Read Only Forum 2023.
 

GMP - GNU Multi-Precision Library v. 4.2.4

Started by José Roca, January 01, 2009, 07:45:24 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
From Wikipedia, the free encyclopedia

The GNU Multiple-Precision Library, also known as GMP, is a free library for arbitrary-precision arithmetic, operating on signed integers, rational numbers, and floating point numbers. There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface. The basic interface is for C but wrappers exist for other languages including C++, OCaml, Perl, and Python.

The main target applications for GMP are cryptography applications and research, Internet security applications, and computer algebra systems and research.

GMP aims to be faster than any other bignum library for all operand sizes. Some important factors towards this end are:



  • Using fullwords as the basic arithmetic type.

  • Using different algorithms for different operand sizes. The algorithms that are fastest for really big numbers are seldom fastest for small numbers.

  • Highly optimized assembly code for the most important inner loops, specialized for different processors.

The first GMP release was made in 1991. It is continually developed and maintained, with a new release about once a year. The current release is 4.2.4.

GMP is part of the GNU project (although the fact that its website is not on gnu.org might cause confusion), and is distributed under the GNU LGPL.

GMP is used for integer arithmetic in many computer algebra systems such as Mathematica.

The attached file contains my translation of the GMP header as well as libgmp-3.dll and the manual in PDF format.


The DLL has been downloaded from SourceForge: http://sourceforge.net/project/showfiles.php?group_id=204414&package_id=245196&release_id=649190

José Roca

#1
 
An small example to demonstrate the use of GMP, taken from the thinBasic forum: http://community.thinbasic.com/index.php?topic=1410.msg17871


' SED_PBCC - Use the console compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "gmp.inc"

FUNCTION PBMAIN () AS LONG

   LOCAL x AS MPF_STRUCT
   LOCAL y AS MPF_STRUCT
   LOCAL z AS MPF_STRUCT

   ' Used as output buffer
   LOCAL outb AS ASCIIZ * 2000
   ' Used for output format
   LOCAL f AS ASCIIZ * 20
   f = "%77.64Fe"

   ' Initialize Multi Precision Floating variables to "Precision" bit precision
   LOCAL Precision AS LONG
   Precision = 512
   mpf_init2(x, Precision)
   mpf_init2(y, Precision)
   mpf_init2(z, Precision)

   ' ADD 2 float numbers
   ' Set y to value of 2 base 10
   mpf_set_str(y, "100e00", 10)
   ' Perform: z = sqrt(y)
   mpf_sqrt(z, y)
   ' Perform: x = y + z
   mpf_add(x, y, z)

   ' Reset to spaces the output buffer
   RESET outb
   ' sprintf the value of x into the string outb
   gmp_sprintf(outb, f, x)
   PRINT outb
   PRINT "Printed as double: " mpf_get_d(x)

   ' Square root of a number
   ' Set x to value of 2 base 10
   mpf_set_str(x, "2", 10)
   ' Perform: y = sqrt(x)
   mpf_Sqrt(y, x)
   ' Reset to spaces the output buffer
   RESET outb
   ' sprintf the value of y into the string Outb
   gmp_sprintf(outb, f, y)
   PRINT outb
   PRINT "Printed as double: " mpf_get_d(y)
   
   ' Deallocate Multi Precision Floating variables
   mpf_clear(x)
   mpf_clear(y)
   mpf_clear(z)

   ' *** Integer ***

   LOCAL i AS MPZ_STRUCT
   LOCAL j AS MPZ_STRUCT
   LOCAL k AS MPZ_STRUCT

   mpz_init(i)
   mpz_init(j)
   mpz_init(k)
   
   mpz_set_str(i,"2",10)
   mpz_set_str(j,"7",10)
   mpz_mul(k,i,j)

   RESET outb
   PRINT "----------------------------------------"
   PRINT "Integer " & CHR$(34) & " 2 * 7 " & CHR$(34)
   gmp_sprintf(outb, "%Zd", k)
   PRINT outb
   mpz_clear(k)
   mpz_clear(j)
   mpz_clear(i)

   ' *** Rational ***

   LOCAL n AS MPQ_STRUCT
   LOCAL m AS MPQ_STRUCT
   LOCAL q AS MPQ_STRUCT

   mpq_init(n)
   mpq_init(m)
   mpq_init(q)

   mpq_set_str(n,"4/12",10)
   mpq_canonicalize(n)
   mpq_set_str(m,"1/5",10)
   mpq_add(q,n,m)

   RESET outb
   PRINT "----------------------------------------"
   PRINT "Rational " & chr$(34) & " 1/3 + 1/5 " & chr$(34)
   gmp_sprintf(outb,"%Qd",q)
   PRINT outb
   mpq_clear(q)
   mpq_clear(m)
   mpq_clear(n)   

   PRINT "----------------------------------------"
   PRINT "All done. Press any key to finish"
   WAITKEY$

END FUNCTION