Closed jpellegrini closed 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)
Currently, STklos always answers +inf.0 for
(log x)
ifx
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.If a bignum fits a double, it will use the usual fast method, without calling the GMP
Numerators and denominators of rationals are treated separately, so if one needs the GMP, the other may still be computed quickly (and later only a double subtraction is needed)
Log will still not work if the result of the operation does not fit a double (in which case it will answer +inf.0).
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
It doesn't seem to make sense to extend trigonometric functions to bignums. While it is reasonable to take the logarithm of a huge number, that is not the same for sin, cos, tan. The same argument applies to
asin
andacos
.~Atan: since
(atan n)
will converge to $\pi/2$ when n goes to+inf.0
, there is not much benefit in allow bignums as arguments.~ --actually, STkos already answers an inexact $\pi/2$ for all bignums! :)Hyperbolic trigs: it would be nice to enhance them, but most implementations do the same as we do.
exp
(notexpt
) could certainly be enhanced, but it's harder than log (we don't have a GMP function that does almost all the work)sqrt
of bignums currently only work for perfect squares (as in several other implementations) - but this can be improved... This is a good candidate for a next PR (Anyway, most implementations do only sqrt of perfect squares for bignums) ----- update I have a patch ready. Will make the PR later