egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
69 stars 17 forks source link

Make `log` work on bignums #524

Closed jpellegrini closed 1 year ago

jpellegrini commented 1 year ago

Currently, STklos always answers +inf.0 for (log x) if x is a bignum.

Here's a list of what other implementations do: https://github.com/schemedoc/surveys/blob/master/surveys/log-requires-floating-point.md

We can use some logarithm properties and the GMP function mpz_get_d_2exp to get logs of bignums.

MINI-GMP

The function mpz_get_d_2exp is only available from the full GMP. The mini-gmp does not offer it. So, if the user compiled STklos with the mini-GMP, the behavior will be as it was before this patch (if the number is too large for a double, +inf.0 will be returned).

TESTS

Tests are included.

The fact that we treat mini-GMP and full-GMP differently means that conditional compilation is necessary. But then, in order to write proper tests, it is also necessary to come up with a predicate that can be used to tell if a test makes sense or not: %stklos-has-gmp?.

OTHER RELEVANT FUNCTIONS

jpellegrini commented 1 year ago

@egallesio I have also made something similar for SQRT. If you want I may include it as another commit in this PR. (A survey of systems that compute square roots of some bignums is also in the same link above, https://github.com/schemedoc/surveys/blob/master/surveys/log-requires-floating-point.md)