* libguile/numbers.c (VARARG_MPZ_ITERATOR)[!HAVE_DECL_MPZ_INITS]: New
macro.
(mpz_inits, mpz_clears)[!HAVE_DECL_MPZ_INITS]: New functions.
* configure.ac: Check for the declaration of `mpz_inits'.
* libguile/numbers.c (exact_integer_is_perfect_square,
exact_integer_floor_square_root): New static functions.
(scm_sqrt): Use SCM_LIKELY. Add 'scm_t_inum' variable in inum case to
reduce the number of uses of SCM_I_INUM. Rename 'mpz_t' variable.
Remove unneeded sign check. Handle bignums too large to fit in a
double. Handle fractions too large or too small to fit in a
normalized double.
* test-suite/tests/numbers.test ("sqrt"): Add tests.
* libguile/numbers.c (scm_sqrt): Handle exact integers and rationals in
such a way that exact results are returned whenever possible.
* test-suite/tests/numbers.test ("sqrt"): Add tests.
* libguile/numbers.c (idbl2str): Print large numbers in scientific
notation only if the exponent is >= 7 and the least significant
non-zero digit has value >= radix^4.
* test-suite/tests/numbers.test ("number->string"): Add tests.
Fixes <http://bugs.gnu.org/13757>.
* libguile/numbers.c (idbl2str): Reimplement.
(mem2decimal_from_point): Accept negative exponents larger than
SCM_MAXEXP that produce subnormals.
(SCM_MAX_DBL_PREC): Removed preprocessor macro.
(scm_dblprec, fx_per_radix): Removed static variables.
(init_dblprec, init_fx_radix): Removed static functions.
(scm_init_numbers): Remove initialization code for 'scm_dblprec'
and 'fx_per_radix'.
* test-suite/tests/numbers.test ("number->string"): Restore tests that
previously failed. Remove comments about problems in the number
printer that are now fixed.
* libguile/numbers.c (scm_i_divide2double): New function.
(scm_i_divide2double_lo2b): New variable.
(scm_i_fraction2double, log_of_fraction): Use 'scm_i_divide2double'.
(do_divide): Removed. Its code is now in 'scm_divide'.
(scm_divide2real): Removed. Superceded by 'scm_i_divide2double'.
(scm_divide): Inherit code from 'do_divide', but without support for
forcing a 'double' result (that functionality is now implemented by
'scm_i_divide2double'). Add FIXME comments in cases where divisions
might not be as precise as they should be.
(scm_init_numbers): Initialize 'scm_i_divide2double_lo2b'.
* test-suite/tests/numbers.test (dbl-epsilon-exact, dbl-max-exp): New
variables.
("exact->inexact"): Add tests.
("inexact->exact"): Add test for largest finite inexact.
* libguile/numbers.c (scm_inexact_to_exact): Implement conversion of a
double to an exact rational without using the mpq functions.
* test-suite/tests/numbers.test (dbl-mant-dig): Simplify initializer.
(dbl-epsilon, dbl-min-exp): New variables.
("inexact->exact"): Add tests. Fix broken "2.0**i to exact and back"
test, and change it to "2.0**i to exact", to avoid use of
'exact->inexact'.
* libguile/numbers.c (log_of_exact_integer_with_size): Removed.
(log_of_exact_integer): Handle bignums too large to fit in a double
using 'scm_i_big2dbl_2exp' instead of 'scm_integer_length' and
'scm_ash'.
(log_of_fraction): Use 'log_of_exact_integer' instead of
'log_of_exact_integer_with_size'.
* libguile/numbers.c (scm_i_big2dbl_2exp): New static function.
(scm_i_big2dbl): Reimplement in terms of 'scm_i_big2dbl_2exp',
with proper rounding.
* test-suite/tests/numbers.test ("exact->inexact"): Add tests.
* libguile/numbers.c (left_shift_exact_integer,
floor_right_shift_exact_integer, round_right_shift_exact_integer): New
static functions.
(scm_round_ash): New procedure.
(scm_ash): Reimplement in terms of 'left_shift_exact_integer' and
'floor_right_shift_exact_integer'.
* libguile/numbers.h: Add prototype for scm_round_ash. Rename the
second argument of 'scm_ash' from 'cnt' to 'count'.
* test-suite/tests/numbers.test (round-ash, ash): Add new unified
testing framework for 'ash' and 'round-ash'. Previously, the tests
for 'ash' were not very comprehensive; for example, they did not
include a single test where the number to be shifted was a bignum.
* doc/ref/api-data.texi (Bitwise Operations): Add documentation for
'round-ash'. Improve documentation for `ash'.
Fixes <http://bugs.gnu.org/11887>.
* libguile/numbers.c (mem2ureal): New argument 'allow_inf_or_nan'.
Accept infinities and NaNs only if 'allow_inf_or_nan' is true and "#e"
is not present. Check for "inf.0" or "nan." case-insensitively. Do
not accept rationals with zero divisors.
(mem2complex): Pass new 'allow_inf_or_nan' argument to 'mem2ureal',
which is set if and only if a explicit sign was present.
* test-suite/tests/numbers.test ("string->number"): Add tests.
* libguile/numbers.c (scm_product): Avoid signed integer overflow, which
modern C compilers are allowed to assume will never happen, thus
allowing them to optimize out our overflow checks.
* test-suite/tests/numbers.test (*): Add tests.
* libguile/numbers.c (scm_angle): Check the sign of an inexact real
zero, to ensure that (angle -0.0) => pi and (angle 0.0) => 0.0.
* test-suite/tests/numbers.test (angle): Add tests, and increase
precision of tests where the angle should be pi.
* libguile/numbers.c (custom_gmp_malloc, custom_gmp_realloc,
custom_gmp_free): New static functions used by GMP for allocation.
These are just wrappers for scm_malloc, scm_realloc, and free.
(scm_init_numbers): If scm_install_gmp_memory_functions is nonzero,
use mp_set_memory_functions to configure GMP to use
custom_gmp_{malloc,realloc,free} for memory allocation.
(scm_bigprint): Ask gmp for the function used to deallocate the string
returned by mpz_get_str.
* libguile/numbers.h: Declare scm_install_gmp_memory_functions.
* libguile/guile.c: When running the Guile binary, install the
gmp_memory_functions.
Based on a patch by Mark H Weaver <mhw@netris.org>.
* doc/ref/api-data.texi (Exact and Inexact Numbers): doc for scm_is_exact
and scm_is_inexact
* libguile/numbers.c (scm_is_exact, scm_is_inexact): new functions
* libguile/numbers.h: declarations for scm_is_exact and scm_is_inexact
* test/suite/standalone/test-conversion.c (test_is_exact, test_is_inexact):
new tests
* libguile/numbers.c (scm_logand): Fix a type error (comparing a SCM
against an int, when we really wanted to compare the unpacked
fixnum).
* libguile/ports.c (scm_i_set_conversion_strategy_x): Check
scm_conversion_strategy_init, not scm_conversion_strategy.
* libguile/read.c (recsexpr): Fix loops to avoid strange test of SCM
values.
* libguile/numbers.c (scm_exact_integer_sqrt): New C procedure to
compute exact integer square root and remainder.
(scm_i_exact_integer_sqrt): New Scheme procedure `exact-integer-sqrt'
from the R6RS, imported into core guile.
* libguile/numbers.h: Add prototypes.
* module/rnrs/base.scm: Remove broken stub implementation, which would
fail badly when applied to large integers.
* doc/ref/api-data.texi: Add documentation.
* doc/ref/r6rs.texi: Change documentation for `exact-integer-sqrt' to a
stub that xrefs the core docs, as is done for other operations
available in core.
* test-suite/tests/numbers.test: Add tests.
* NEWS: Add news entries.
* libguile/numbers.c (mem2decimal_from_point): Use scm_divide instead of
scm_divide2real when applying a negative exponent, to preserve
exactness in case the "#e" forced exactness specifier is present.
This fixes a bug where numeric literals such as "#e1e-5" yielded
incorrect fractions.
* libguile/arrays.c (scm_i_read_array):
* libguile/backtrace.c (display_backtrace_body):
* libguile/filesys.c (scm_readdir)
* libguile/i18n.c (chr_to_case):
* libguile/ports.c (register_finalizer_for_port):
* libguile/posix.c (scm_nice):
* libguile/stacks.c (scm_make_stack): Clean up a number of
set-but-unused vars. Thanks to Douglas Mencken for the report.
* libguile/numbers.c (scm_log, scm_exp): Fix a few #if cases that should
be #ifdef.
* libguile/numbers.c (scm_quotient, scm_remainder, scm_modulo): Accept
inexact integers as well as exact ones, as required by the R5RS.
* test-suite/tests/numbers.test (quotient, remainder, modulo): Add tests.
* libguile/numbers.c: Update comments regarding GMP earlier than 4.2.
Remove speculations about versions of GMP that had not yet been
released when the comments were written. Replace them with facts that
are now known about the changes made in GMP 4.2.
* libguile/numbers.c (log_of_shifted_double, log_of_exact_integer,
log_of_exact_integer_with_size, log_of_fraction): New internal static
functions used by scm_log and scm_log10.
(scm_log, scm_log10): Robustly handle large integers, large and small
fractions, and fractions close to 1. Previously, computing logarithms
of fractions close to 1 yielded grossly inaccurate results, and the
other cases yielded infinities even though the answer could easily fit
in a double. (log -0.0) now returns -inf.0+<PI>i, where previously it
returned -inf.0. (log 0) now throws a numerical overflow exception,
where previously it returned -inf.0. (log 0.0) still returns -inf.0.
Analogous changes made to `log10'.
* test-suite/tests/numbers.test (log, log10): Add tests.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
* libguile/numbers.c (scm_i_inexact_truncate_divide): Use trunc instead
of floor and ceil. Important for consistency with
scm_truncate_quotient and scm_truncate_remainder.
Signed-off-by: Ludovic Courtès <ludo@gnu.org>
* libguile/numbers.c (scm_c_truncate, scm_truncate_number,
scm_i_inexact_truncate_quotient, scm_i_inexact_truncate_remainder):
Use trunc directly, now that we have its gnulib module.
* libguile/numbers.c (scm_quotient): Reimplement in terms of
scm_truncate_quotient.
(scm_remainder): Reimplement in terms of scm_truncate_remainder.
(scm_modulo): Reimplement in terms of scm_floor_remainder.
(scm_euclidean_quotient, scm_euclidean_remainder,
scm_euclidean_divide): Reimplement in terms of floor and ceiling.
Make them non-extensible, because there is no need; they will work
with any objects that implement the floor and ceiling division
operators, and that can be tested using `negative?'.
* libguile/numbers.c (scm_c_truncate): Use ceil (x) instead of
-floor (-x).
(scm_truncate_number): Implement directly instead of by checking the
sign and using scm_floor or scm_ceiling. Use scm_truncate_quotient
for fractions. Make extensible, so that new number types implemented
in GOOPS will be able to do the job more efficiently, since it is
often easier to implement truncate than floor or ceiling.
(scm_round_number): Optimize fractions case by using
scm_round_quotient. Make extensible, so that new number types
implemented in GOOPS will be able to do the job efficiently.
(scm_floor, scm_ceiling): Optimize fractions case by using
scm_floor_quotient and scm_ceiling_quotient, respectively.
* test-suite/tests/numbers.test: Add test cases.
* libguile/numbers.c: (scm_euclidean_quotient, scm_euclidean_remainder,
scm_euclidean_divide, scm_centered_quotient, scm_centered_remainder,
scm_centered_divide): Optimize case where both arguments are exact and
at least one is a fraction, by reducing to a subproblem involving only
integers, and then adjusting the resulting remainder as needed.
* libguile/numbers.c (scm_euclidean_divide, scm_centered_divide): Change
API to return two values via output arguments of type (SCM *), instead
of packing into a values object.
(scm_i_euclidean_divide, scm_i_centered_divide): New internal wrappers
that call the above functions and pack the result into a values
object.
* libguile/numbers.h: Change prototypes to reflect new API.
* doc/ref/api-data.h (Arithmetic): Update manual.
* libguile/numbers.c (scm_euclidean_quotient, scm_euclidean_divide,
scm_centered_quotient, scm_centered_divide): Fix bug in inum/inum
case, where (quotient most-negative-fixnum -1) would not be converted
to a bignum.
(scm_euclidean_quotient): Be more anal-retentive about calling
scm_remember_upto_here_1 after mpz_sgn, (even though mpz_sgn is
documented as being implemented as a macro and certainly won't
do any allocation). It's better to be safe than sorry here.
(scm_euclidean_quotient, scm_centered_quotient): In the bignum/inum
case, check if the divisor is 1, since this will allow us to avoid
allocating a new bignum.
(scm_euclidean_divide, scm_centered_quotient, scm_centered_divide):
When computing the intermediate truncated quotient (xx / yy) and
remainder, use (xx % yy) instead of (xx - qq * yy), on the theory that
the compiler is more likely to handle this case intelligently and
maybe combine the operations.
(scm_euclidean_divide): In the bignum/inum case, we know that the
remainder will fit in an fixnum, so don't bother allocating a bignum
for it.
(scm_euclidean_quotient, scm_euclidean_remainder,
scm_euclidean_divide, scm_centered_quotient, scm_centered_remainder,
scm_centered_divide): Minor stylistic changes.
* test-suite/tests/numbers.test: Rework testing framework for
number-theoretic division operators to be more efficient and
comprehensive in its testing of code paths and problem cases.
* libguile/numbers.c (scm_atan): Call SCM_WTA_DISPATCH_1 instead of
SCM_WTA_DISPATCH_2 if the second argument is unbound. Arguably,
SCM_WTA_DISPATCH_* should handle that case gracefully, but currently
it doesn't.