* libguile/numbers.c (scm_logbit_p): If the requested bit is the sign
bit (or above), check the sign portably. Otherwise, ensure that we're
testing the bit in a two's complement representation.
(left_shift_exact_integer): Avoid left-shifting negative integers.
* libguile/vm-i-scheme.c (ash): Avoid left-shifting negative integers.
* libguile/numbers.c (left_shift_exact_integer): Fix edge case where
N is -1 and count is SCM_I_FIXNUM_BIT-1 to return the most negative
fixnum. Previously this result was returned as a bignum.
* test-suite/tests/numbers.test (ash): Add tests.
* libguile/numbers.c (scm_ash): Fix (ash -1 SCM_I_FIXNUM_BIT-1) to
return a fixnum instead of a bignum.
* test-suite/tests/numbers.test (ash): Add tests.
* libguile/numbers.c (mem2ureal, left_shift_exact_integer,
floor_right_shift_exact_integer, round_right_shift_exact_integer):
Use 'assert' instead of 'scm_syserror' to indicate a case that should
never happen.
* libguile/numbers.c (scm_numerator, scm_denominator): Handle signed
zeroes and infinities in accordance with the corresponding R6RS flonum
procedures.
* module/rnrs/arithmetic/flonums.scm (flnumerator, fldenominator):
Remove special handling of infinities.
* test-suite/tests/numbers.test (numerator, denominator): Add tests.
Convert existing tests to use 'pass-if-equal'.
* test-suite/tests/r6rs-arithmetic-flonums.test (flnumerator): Fix
broken test of (flnumerator -0.0).
* libguile/numbers.c (double_is_non_negative_zero): Remove.
(idbl2str, scm_max, scm_min, scm_angle, log_of_shifted_double,
scm_log10): Use 'copysign' to check signs of zeroes,
instead of 'double_is_non_negative_zero'.
Fixes <http://bugs.gnu.org/14905>.
Reported by Göran Weinholt <goran@weinholt.se>.
* libguile/numbers.c (scm_rationalize): Rewrite. Previously an
incorrect algorithm was used which failed in many cases.
* test-suite/tests/numbers.test (rationalize): Add tests.
* libguile/numbers.c (scm_num_eq_p): Fix bug comparing fractions to
infinities (reported by Göran Weinholt <goran@weinholt.se>). Fix
erroneous comment describing the logic behind inum/flonum comparison.
Use similar logic for inum/complex comparison to avoid rounding
errors. Make minor indentation fixes and simplifications.
* test-suite/tests/numbers.test (=): Add tests.
* libguile/numbers.c (INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE):
New macro.
(scm_i_divide2double): Use INUM_LOSSLESSLY_CONVERTIBLE_TO_DOUBLE to
determine if our fast path is safe. Previously, negative arguments
were not checked properly.
* test-suite/tests/numbers.test (exact->inexact): Add tests.
* 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.