source download ## gmp.lsp

## 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 deprecatednametoterm

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 functionsabs,even?,odd,sgnandzero?. 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 installThis interface module presets the maximum precision to 1024. The precision can be changed to any other value by changing the definition ofMAX_PRECISIONin 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 aloadstatement 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 thegmp.lspmodule.

## Integer arithmetik

§

## GMP:+

syntax: (GMP:+arg1arg2)

add two integers inarg1andarg2§

## GMP:-

syntax: (GMP:-arg1arg2)

subtractarg2fromarg1§

## GMP:*

syntax: (GMP:*arg1arg2)

multiplyarg1byarg2§

## GMP:/

syntax: (GMP:/arg1arg2)

dividearg1byarg2, round towards '0' (zero)§

## GMP:%

syntax: (GMP:%arg1arg2)

calc rest of divisionarg1/arg2§

## GMP:**

syntax: (GMP:**arg1arg2)

calc power(arg1,arg2)§

## GMP:=

syntax: (GMP:=arg1arg2)

test forarg1equalarg2§

## GMP:<

syntax: (GMP:<arg1arg2)

test forarg1smallerarg2§

## GMP:>

syntax: (GMP:>arg1arg2)

test forarg1biggerarg2§

## GMP:<=

syntax: (GMP:<=arg1arg2)]

test forarg1smaller or equalarg2§

## GMP:>=

syntax: (GMP:>=arg1arg2)

test forarg1bigger or equalarg2

## Bit operations

§

## GMP:&

syntax: (GMP:&arg1arg2)

bitwiseandofarg1,arg2§

## GMP:|

syntax: (GMP:|arg1arg2)

bitwise inclusiveorofarg1,arg2§

## GMP:^

syntax: (GMP:^arg1arg2)

bitwise exclusiveorofarg1,arg2§

## GMP:~

syntax: (GMP:~arg)

bitwise complement ofarg

## Number theory

§

## GMP:prime?

syntax: (GMP:prime?arg)

check ifargis prime§

## GMP:next-prime

syntax: (GMP:next-primearg)

calc closes prime greater thanarg§

## GMP:factor

syntax: (GMP:factorarg)

calc a list of prime factors forarg§

## GMP:gcd

syntax: (GMP:gcdarg1arg2)

greatest common divisor ofarg1andarg2§

## GMP:bin

syntax: (GMP:binarg1arg2)

calc binomial (arg1arg2)§

## GMP:fac

syntax: (GMP:facarg)

arg! factorial(arg)§

## GMP:fib

syntax: (GMP:fibarg)

fibonacci(arg)

## Random numbers

§

## GMP:seed

syntax: (GMP:seedarg)

seed the random generator§

## GMP:rand

syntax: (GMP:randarg)

generate random numbers between 0 and arg - 1

- ∂ -

generated with newLISP and newLISPdoc