In exact_exponent_expt, some calls were missing, and STklos would crash on this, for example:
(expt (expt 2 100) -2)
This patch also includes a test for that.
If an exponent is reeealy too large:
(expt fx-greatest 100000)
STklos will report a SIGABRT from the GMP.
Anyway, when the exponent is too large, several Scheme implementations either crash (Chez, Guile, Sagittarius), or report an error like "our of memory" or "exponent too large" (Gosh, MIT). Kawa and Bigloo return an imprecise infinity -- so the behavior of STklos should be OK.
2. Don't create and initialize a new mpz_t number unnecessarily
Use mpz_ui_pow_ui instead of creating and setting the value of a new mpz number to then call mpz_pow_ui.
Since that function only accpets unsigned integers, we check the sign, pass the number as unsigned, then if the base was negative and the exponent was odd, we multiply by -1.
Call mpz_init(res) for the tc_bignum case.
The first change seems to make exponentiation with fixnum base slightly faster:
1. Include missing calls to
mpz_init
In
exact_exponent_expt
, some calls were missing, and STklos would crash on this, for example:This patch also includes a test for that.
If an exponent is reeealy too large:
STklos will report a
SIGABRT
from the GMP.Anyway, when the exponent is too large, several Scheme implementations either crash (Chez, Guile, Sagittarius), or report an error like "our of memory" or "exponent too large" (Gosh, MIT). Kawa and Bigloo return an imprecise infinity -- so the behavior of STklos should be OK.
2. Don't create and initialize a new
mpz_t
number unnecessarilyUse
mpz_ui_pow_ui
instead of creating and setting the value of a new mpz number to then callmpz_pow_ui
.Since that function only accpets unsigned integers, we check the sign, pass the number as unsigned, then if the base was negative and the exponent was odd, we multiply by -1.
Call
mpz_init(res)
for thetc_bignum
case.The first change seems to make exponentiation with fixnum base slightly faster:
(Average of 20 runs)
The sign change didn't make much of a difference in timings (That is, timings seem similar for iterated computation of
(expt -32 9)
and(expt 32 9)
.3. Call
mpz_clear
before returning fromexpt
This actually makes it faster: