summaryrefslogtreecommitdiff
path: root/libguile/numbers.c
Commit message (Expand)AuthorAgeFilesLines
* Do not assume that sizeof (long) == sizeof (void *) == sizeof (SCM)....This assumption does not hold on systems that use the LLP64 data model. Partially fixes <https://debbugs.gnu.org/22406>. Reported by Peter TB Brett <peter@peter-b.co.uk>. * libguile/numbers.h (scm_t_inum): Move here from numbers.c, and change to be equivalent to 'long' (formerly 'scm_t_signed_bits'). (SCM_MOST_POSITIVE_FIXNUM, SCM_MOST_NEGATIVE_FIXNUM): Define based on SCM_I_FIXNUM_BIT instead of SCM_T_SIGNED_BITS_MAX. (SCM_I_INUM): Adjust definitions to return a 'scm_t_inum', and avoiding the assumption that SCM_UNPACK returns a 'long'. * libguile/numbers.c (scm_t_inum): Move definition to numbers.h. Verify that 'scm_t_inum' fits within a SCM value. (scm_i_inum2big): Remove preprocessor code that forced a compile error unless sizeof (long) == sizeof (void *). Mark H Weaver2017-03-011-7/+3
* Avoid signed integer overflows in numeric conversions....Reported by Miroslav Lichvar <mlichvar@redhat.com> in <https://lists.gnu.org/archive/html/guile-devel/2016-02/msg00045.html> * libguile/conv-integer.i.c: Avoid signed overflow. * libguile/numbers.c (scm_is_signed_integer): Avoid signed overflow. Mark H Weaver2017-03-011-5/+10
* Fix (* x -1) for GOOPS types...* libguile/numbers.c (scm_product): Only reduce (* x -1) to (- x) when X is a bignum. Fixes weirdness when X is not a number and instead multiplication should dispatch to GOOPS. Thanks to Alejandro Sanchez for the report. Andy Wingo2017-02-281-15/+13
* Use labs instead of abs where needed...* libguile/numbers.c (log_of_fraction): Use labs instead of abs on longs. Thanks to Matt Wette for the tip. Andy Wingo2017-02-211-1/+1
* Fix make-polar signedness of zeros on macOS...* configure.ac: Check for __sincos. * libguile/numbers.c (scm_c_make_polar): Fall back to __sincos if possible. Fixes zero signedness of make-polar on macOS. Matt Wette2017-02-211-0/+2
* Speed up number->string...* libguile/numbers.c (scm_number_to_string): Use scm_from_latin1_string where appropriate. Avoids mucking about with iconv. Andy Wingo2017-02-171-3/+3
* Deprecate dynamic roots...* libguile/root.h: * libguile/root.c: Remove these files. * libguile/deprecated.h: * libguile/deprecated.c (scm_internal_cwdr, scm_call_with_dynamic_root) (scm_dynamic_root, scm_apply_with_dynamic_root): Deprecate. Remove all root.h usage, which was vestigial. * module/ice-9/serialize.scm: Use (current-thread) instead of (dynamic-root). Andy Wingo2016-11-211-1/+0
* Fix atan procedure when applied to complex numbers....Fixes a regression introduced in commit ad79736c68a803a59814fbfc0cb4b092c2b4cddf. * libguile/numbers.c (scm_atan): Fix the complex case. * test-suite/tests/numbers.test ("atan"): Add test. Mark H Weaver2016-05-221-2/+2
* Don't return expressions from void functions in numbers.c...Although popular compilers allow it as long as the expression is of type void, it violates C99 and some compilers choke on it. * libguile/numbers.c (scm_euclidean_divide, scm_floor_divide) (scm_ceiling_divide, scm_truncate_divide, scm_centered_divide) (scm_round_divide): Don't use the return statement with an expression from functions with return type void. Mark H Weaver2016-05-221-138/+108
* scm_lfwrite, not scm_lfwrite_unlocked...* libguile/ports.h (scm_lfwrite_unlocked): Remove. * libguile/ports.c (scm_lfwrite): Rename from scm_lfwrite_unlocked. * libguile/numbers.c: * libguile/print.c: Adapt to call scm_lfwrite. Andy Wingo2016-04-261-5/+5
* Merge branch 'stable-2.0'...Conflicts: GUILE-VERSION NEWS guile-readline/ice-9/readline.scm libguile/async.c libguile/backtrace.c libguile/deprecated.h libguile/gc-malloc.c libguile/gdbint.c libguile/init.c libguile/ioext.c libguile/mallocs.c libguile/print.c libguile/rw.c libguile/scmsigs.c libguile/script.c libguile/simpos.c libguile/snarf.h libguile/strports.c libguile/threads.c libguile/vm-i-scheme.c libguile/vm-i-system.c module/srfi/srfi-18.scm test-suite/Makefile.am test-suite/standalone/test-num2integral.c Mark H Weaver2014-04-251-5/+11
|\
| * Avoid undefined behavior regarding signed integers and left shifts....* 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. Mark H Weaver2014-03-111-6/+12
* | Merge branch 'stable-2.0'...Conflicts: module/system/vm/traps.scm test-suite/tests/peval.test Mark H Weaver2014-01-091-2/+21
|\|
| * Implement 'exact-integer?' and 'scm_is_exact_integer'....* libguile/numbers.c (scm_exact_integer_p, scm_is_exact_integer): New procedures. (scm_integer_p): Improve docstring. * libguile/numbers.h (scm_exact_integer_p, scm_is_exact_integer): New prototypes. * doc/ref/api-data.texi (Integers): Add docs. * test-suite/tests/numbers.test ("exact-integer?"): Add tests. Mark H Weaver2014-01-081-2/+21
* | Merge commit 'd360671c1cca335600079f1c5714572d1c2e676d'Andy Wingo2013-11-281-1/+1
|\|
| * Fix edge case in 'ash'....* 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. Mark H Weaver2013-10-031-3/+6
| * Revert "Fix edge case in 'ash'."...This reverts commit 8df68898b9f6ba15171244f1f3549688f13d605f. Mark H Weaver2013-10-031-7/+4
* | Merge remote-tracking branch 'origin/stable-2.0'...Conflicts: libguile/deprecated.h libguile/programs.c libguile/programs.h Andy Wingo2013-10-031-4/+7
|\|
| * Fix edge case in 'ash'....* 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. Mark H Weaver2013-10-031-4/+7
* | Micro-optimize char_decimal_value....* libguile/numbers.c (char_decimal_value): A wee micro-optimization. Andy Wingo2013-08-311-12/+17
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-08-111-6/+23
|\|
| * Fix inappropriate uses of scm_syserror in numbers.c....* 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. Mark H Weaver2013-08-101-4/+5
| * Fix numerator and denominator handling of signed zeroes and infinities....* 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). Mark H Weaver2013-08-091-2/+18
* | Eliminate use of deprecated 'SCM_WTA_DISPATCH_2' in numbers.c....* libguile/numbers.c (scm_gcd): Use 'scm_wta_dispatch_2' not 'SCM_WTA_DISPATCH_2'. Mark H Weaver2013-08-081-1/+1
* | Merge remote-tracking branch 'origin/stable-2.0'...Conflicts: libguile/numbers.c libguile/vm-i-scheme.c Mark H Weaver2013-08-061-222/+312
|\|
| * Use Gnulib's 'isfinite' in numbers.c....* libguile/numbers.c (DOUBLE_IS_FINITE, DOUBLE_IS_POSITIVE_INFINITY, DOUBLE_IS_NEGATIVE_INFINITY): Remove. (scm_odd_p, scm_even_p, scm_finite_p, icmplx2str, scm_rational_p, scm_inexact_to_exact): Use 'isfinite' instead of 'DOUBLE_IS_FINITE'. Mark H Weaver2013-08-061-15/+6
| * Use Gnulib's 'copysign' in numbers.c....* 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'. Mark H Weaver2013-08-061-18/+10
| * Add 'scm_i_from_double' and use it....* libguile/numbers.c (scm_i_from_double): New static function. (scm_from_double): Just call 'scm_i_from_double'. (scm_inf, scm_nan, scm_abs, scm_i_inexact_floor_quotient, scm_i_inexact_floor_remainder, scm_i_inexact_floor_divide, scm_i_inexact_ceiling_quotient, scm_i_inexact_ceiling_remainder, scm_i_inexact_ceiling_divide, scm_i_inexact_truncate_quotient, scm_i_inexact_truncate_remainder, scm_i_inexact_truncate_divide, scm_i_inexact_centered_quotient, scm_i_inexact_centered_remainder, scm_i_inexact_centered_divide, scm_i_inexact_round_quotient, scm_i_inexact_round_remainder, scm_i_inexact_round_divide, scm_max, scm_min, scm_sum, scm_difference, scm_product, scm_divide, scm_truncate_number, scm_round_number, scm_floor, scm_ceiling, scm_expt, scm_sin, scm_cos, scm_tan, scm_sinh, scm_cosh, scm_tanh, scm_asin, scm_acos, scm_atan, scm_sys_asinh, scm_sys_acosh, scm_sys_atanh, scm_real_part, scm_imag_part, scm_magnitude, scm_angle, scm_exact_to_inexact, log_of_shifted_double, log_of_fraction, scm_log10, scm_exp, scm_sqrt, scm_init_numbers): Use 'scm_i_from_double' instead of 'scm_from_double'. Mark H Weaver2013-08-021-117/+123
| * Fix minor formatting error in 'rationalize'....* libguile/numbers.c (scm_rationalize): Fix formatting. Mark H Weaver2013-07-211-2/+2
| * Rewrite 'rationalize' to fix bugs and improve efficiency....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. Mark H Weaver2013-07-211-76/+177
* | Merge remote-tracking branch 'origin/stable-2.0'...Conflicts: libguile/numbers.c Mark H Weaver2013-07-161-27/+53
|\|
| * gcd and lcm support inexact integer arguments....Fixes <http://bugs.gnu.org/14870>. Reported by Göran Weinholt <goran@weinholt.se>. * libguile/numbers.c (scm_gcd, scm_lcm): Support inexact integers. * test-suite/tests/numbers.test (gcd, lcm): Add tests. Mark H Weaver2013-07-161-16/+45
| * min and max: NaNs beat infinities, per R6RS errata....Fixes <http://bugs.gnu.org/14865>. Reported by Göran Weinholt <goran@weinholt.se>. * libguile/numbers.c (scm_min, scm_max): NaNs beat infinities, as per the R6RS errata. * test-suite/tests/numbers.test (min, max): Update tests. Mark H Weaver2013-07-161-10/+8
* | Merge remote-tracking branch 'origin/stable-2.0'...Conflicts: libguile/keywords.c libguile/vm.c Mark H Weaver2013-07-161-33/+82
|\|
| * Avoid lossy conversion from inum to double in numerical comparisons....* libguile/numbers.c (scm_less_p): Avoid converting inums to doubles. * test-suite/tests/numbers.test (<): Add tests. Mark H Weaver2013-07-161-2/+38
| * Fix bugs in numerical equality predicate....* 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. Mark H Weaver2013-07-161-27/+33
| * Fix rounding in scm_i_divide2double for negative arguments....* 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. Mark H Weaver2013-07-161-4/+11
* | Merge remote-tracking branch 'origin/stable-2.0'Mark H Weaver2013-03-281-1/+30
|\|
| * Allow compilation with GMP < 5.0.0....* 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'. Ludovic Courtès2013-03-281-1/+30
* | Merge remote-tracking branch 'origin/stable-2.0'...Conflicts: configure.ac libguile/deprecated.c libguile/deprecated.h libguile/filesys.h libguile/fluids.c libguile/fports.c libguile/gc.c libguile/guile.c libguile/numbers.c libguile/objcodes.c libguile/r6rs-ports.c libguile/smob.c libguile/socket.c libguile/threads.h module/language/scheme/decompile-tree-il.scm module/language/tree-il/peval.scm test-suite/tests/syncase.test Mark H Weaver2013-03-281-606/+1038
|\|
| * Improve sqrt handling of large integers and large and small rationals....* 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. Mark H Weaver2013-03-201-21/+112
| * Optimize inum case of exact-integer-sqrt....* libguile/numbers.c (scm_exact_integer_sqrt): Use GMP for inum case. It is faster than what we had before. Mark H Weaver2013-03-201-16/+8
| * Sqrt returns exact results when possible....* 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. Mark H Weaver2013-03-201-5/+64
| * Optimize scm_i_divide2double for integers less than 2^DBL_MANT_DIG....* libguile/numbers.c (scm_i_divide2double): Optimize for common case when both operands are less than 2^DBL_MANT_DIG (normally 2^53). Mark H Weaver2013-03-191-1/+11
| * Use scientific notation only if there are enough trailing zeroes....* 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. Mark H Weaver2013-03-191-17/+46
| * Reimplement idbl2str number printer....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. Mark H Weaver2013-03-171-212/+179
| * Improve inexact division of exact integers....* 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. Mark H Weaver2013-03-171-80/+204
| * Reimplement 'inexact->exact' to avoid mpq functions....* 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'. Mark H Weaver2013-03-121-14/+27
| * Optimize logarithms using scm_i_big2dbl_2exp...* 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'. Mark H Weaver2013-03-121-18/+12
| * Simplify and improve scm_i_big2dbl, and add scm_i_big2dbl_2exp...* 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. Mark H Weaver2013-03-121-65/+36