gmp.lsp

Module index

source download

Module: gmp.lsp

GNU MP Bignum Library interface

Version: 1.52 updated for new install locations and GMP website
Version: 1.53 took out redefinition of normal int operations to floats
Version: 1.6 added library path for OpenBSD and tested for 64-bit newLISP
Version: 1.7 doc changes
Version: 1.8 doc changes
Version: 1.9 fixes for 64-bit in <,>,>=,<=,factor didn't sign extend 32->64
Version: 2.0 more fixes for 64-bit, allocated space for handles was to small
Version: 2.1 changed deprecated name to term
Version: 2.2 out-comment 64-bit lib on Mac OSX, newLISP is shipped as 32-bit
Version: 2.2 document built-in big integer arithmetik in newLISP
Author: Lutz Mueller, 2007-2011

The GNU MP Bignum Library



Since version 10.4.8, newLISP has built-in support for unlimited precision big-integer arithmetik supporting the integer operators = ,- , *, /, %, ++, --, <, <= , >, >=, != and the functions abs, even?, odd, sgn and zero?. As this module has some functions not available built into newLISP, it is still packaged with the distribution.

This modules interfaces to libgmp which can be obtained from http://gmplib.org/ .

Source code for libgmp.so (UNIX) or lbgmp.dll (Win32) or libgmp.dylib (Mac OS X) is available at this site. After installing the library the correct path-name should be added in the (set 'files ... ) statement around line 130 in this module.

When compiling libgmp for Mac OS X on Intel CPUs and for 32-bit newLISP use:
     ./configure CFLAGS="-m32" ABI=32
     make
     sudo make install
 
This interface module presets the maximum precision to 1024. The precision can be changed to any other value by changing the definition of MAX_PRECISION in the source of this module.

All arguments to the GMP functions in this module must be given as strings, an error will be thrown otherwise. When starting with a 0 the number is assumed to be in octal format when starting with 0x in hexadecimal format.

This file only imports a few functions from the many available in GNU GMP. See the GMP manual for more functions.

Note, that since version 8.9.7 newLISP does all integer arithmetik with 64 bits giving up to 19 digits of precision. For precisions less or equal 19 digits newLISP's built-in 64-bit integer arithmetik is much faster.

Usage

At the beginning of the programfile include a load statement for the module:
 (load "/usr/share/newlisp/modules/gmp.lsp")
 ; or shorter
 (module "gmp.lsp")
 
Example:
 (GMP:+ "123456789012345678901234567890" "123456789012345678901234567890") 
 => "246913578024691357802469135780"

Adding more functions to the library

When adding functions be aware that inside the GMP context +,-,*,/,=,<,>,<=,>= are overwritten for multiple precision and the original operators would have would have to be prefixed with MAIN when used, inside the gmp.lsp module.

Integer arithmetik



§

GMP:+

syntax: (GMP:+ arg1 arg2)
add two integers in arg1 and arg2

§

GMP:-

syntax: (GMP:- arg1 arg2)
subtract arg2 from arg1

§

GMP:*

syntax: (GMP:* arg1 arg2)
multiply arg1 by arg2

§

GMP:/

syntax: (GMP:/ arg1 arg2)
divide arg1 by arg2, round towards '0' (zero)

§

GMP:%

syntax: (GMP:% arg1 arg2)
calc rest of division arg1/arg2

§

GMP:**

syntax: (GMP:** arg1 arg2)
calc power(arg1, arg2)

§

GMP:=

syntax: (GMP:= arg1 arg2)
test for arg1 equal arg2

§

GMP:<

syntax: (GMP:< arg1 arg2)
test for arg1 smaller arg2

§

GMP:>

syntax: (GMP:> arg1 arg2)
test for arg1 bigger arg2

§

GMP:<=

syntax: (GMP:<= arg1 arg2)]
test for arg1 smaller or equal arg2

§

GMP:>=

syntax: (GMP:>= arg1 arg2)
test for arg1 bigger or equal arg2

Bit operations



§

GMP:&

syntax: (GMP:& arg1 arg2)
bitwise and of arg1, arg2

§

GMP:|

syntax: (GMP:| arg1 arg2)
bitwise inclusive or of arg1, arg2

§

GMP:^

syntax: (GMP:^ arg1 arg2)
bitwise exclusive or of arg1, arg2

§

GMP:~

syntax: (GMP:~ arg)
bitwise complement of arg

Number theory



§

GMP:prime?

syntax: (GMP:prime? arg)
check if arg is prime

§

GMP:next-prime

syntax: (GMP:next-prime arg)
calc closes prime greater than arg

§

GMP:factor

syntax: (GMP:factor arg)
calc a list of prime factors for arg

§

GMP:gcd

syntax: (GMP:gcd arg1 arg2)
greatest common divisor of arg1 and arg2

§

GMP:bin

syntax: (GMP:bin arg1 arg2)
calc binomial (arg1 arg2)

§

GMP:fac

syntax: (GMP:fac arg)
arg! factorial(arg)

§

GMP:fib

syntax: (GMP:fib arg)
fibonacci(arg)

Random numbers



§

GMP:seed

syntax: (GMP:seed arg)
seed the random generator

§

GMP:rand

syntax: (GMP:rand arg)
generate random numbers between 0 and arg - 1



- ∂ -

generated with newLISP  and newLISPdoc