Did you know ... | Search Documentation: |
Exchanging GMP numbers |
If SWI-Prolog is linked with the GNU Multiple Precision Arithmetic
Library (GMP, used by default), the foreign interface provides functions
for exchanging numeric values to GMP types. To access these functions
the header <gmp.h>
must be included before
<SWI-Prolog.h>
. Foreign code using GMP linked to
SWI-Prolog asks for some considerations.
PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, TRUE)
to force
Prolog's GMP initialization without doing the rest of the Prolog
initialization. If you do not want Prolog rebinding the GMP allocation,
call PL_action(PL_GMP_SET_ALLOC_FUNCTIONS, FALSE)
before initializing Prolog.
Here is an example exploiting the function mpz_nextprime():
#include <gmp.h> #include <SWI-Prolog.h> static foreign_t next_prime(term_t n, term_t prime) { mpz_t mpz; int rc; mpz_init(mpz); if ( PL_get_mpz(n, mpz) ) { mpz_nextprime(mpz, mpz); rc = PL_unify_mpz(prime, mpz); } else rc = FALSE; mpz_clear(mpz); return rc; } install_t install() { PL_register_foreign("next_prime", 2, next_prime, 0); }
TRUE
. Otherwise mpz
is untouched and the function returns FALSE
. Note that mpz
must have been initialised before calling this function and must be
cleared using
mpz_clear() to reclaim any storage associated with it.rdiv/2
),
mpq is filled with the normalised rational number
and the function returns TRUE
. Otherwise mpq is
untouched and the function returns FALSE
. Note that mpq
must have been initialised before calling this function and must be
cleared using
mpq_clear() to reclaim any storage associated with it.TRUE
on success. The mpz argument is not
changed.TRUE
on success. Note that t is unified with an
integer if the denominator is 1. The mpq argument is not
changed.