1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 11:50:28 +02:00
Commit graph

574 commits

Author SHA1 Message Date
Mark H Weaver
8b56bcec44 Optimize truncate, round, floor, and ceiling
* 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.
2011-02-14 20:31:14 +01:00
Mark H Weaver
8f9da3406b Add four new sets of fast quotient and remainder operators
* libguile/numbers.c (scm_floor_divide, scm_floor_quotient,
  scm_floor_remainder, scm_ceiling_divide, scm_ceiling_quotient,
  scm_ceiling_remainder, scm_truncate_divide, scm_truncate_quotient,
  scm_truncate_remainder, scm_round_divide, scm_round_quotient,
  scm_round_remainder): New extensible procedures `floor/',
  `floor-quotient', `floor-remainder', `ceiling/', `ceiling-quotient',
  `ceiling-remainder', `truncate/', `truncate-quotient',
  `truncate-remainder', `round/', `round-quotient', and
  `round-remainder'.

* libguile/numbers.h: Add function prototypes.

* test-suite/tests/numbers.test: Add tests.

* doc/ref/api-data.texi (Arithmetic): Add documentation.

* NEWS: Add NEWS entry.
2011-02-14 20:30:53 +01:00
Mark H Weaver
03ddd15bae Optimize division operators handling of fractions
* 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.
2011-02-14 20:30:49 +01:00
Mark H Weaver
5fbf680be9 Make divide functions return values via (SCM *) output arguments
* 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.
2011-02-14 20:30:30 +01:00
Mark H Weaver
4a46bc2a5f Fixes and improvements to number-theoretic division operators
* 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.
2011-02-12 13:00:43 +01:00
Mark H Weaver
bc3d34f587 Add comment about handling of exactness specifiers
* libguile/numbers.c: Add discussion on the handling of exactness
  specifiers to the comment above the string-to-number conversion
  functions.
2011-02-12 13:00:43 +01:00
Mark H Weaver
18104cac0b Fix extensibility of 1-argument atan
* 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.
2011-02-12 13:00:43 +01:00
Ralf Wildenhues
ffb62a43dc fix typos in the manual bits generated from source comments.
* libguile/bitvectors.c, libguile/chars.c,
libguile/deprecated.c, libguile/numbers.c, libguile/random.c,
libguile/read.c, libguile/root.c, libguile/srfi-1.c,
libguile/srfi-13.c, libguile/srfi-14.c, libguile/uniform.c:
Fix typos, add missing newlines.
2011-02-09 22:28:49 +00:00
Mark H Weaver
9d427b2cc3 Improved exactness handling for complex number parsing
When parsing non-real complex numbers, apply exactness specifiers on
per-component basis, as is done in PLT Scheme.  For complex numbers
written in rectangular form, exactness specifiers are applied to the
real and imaginary parts before calling scm_make_rectangular.  For
complex numbers written in polar form, exactness specifiers are applied
to the magnitude and angle before calling scm_make_polar.

There are two kinds of exactness specifiers: forced and implicit.  A
forced exactness specifier is a "#e" or "#i" prefix at the beginning of
the entire number, and applies to both components of a complex number.
"#e" causes each component to be made exact, and "#i" causes each
component to be made inexact.  If no forced exactness specifier is
present, then the exactness of each component is determined
independently by the presence or absence of a decimal point or hash mark
within that component.  If a decimal point or hash mark is present, the
component is made inexact, otherwise it is made exact.

After the exactness specifiers have been applied to each component, they
are passed to either scm_make_rectangular or scm_make_polar to produce
the final result.  Note that this will result in a real number if the
imaginary part, magnitude, or angle is an exact 0.

Previously, both forced and implicit exactness specifiers applied to
the number as a whole _after_ calling scm_make_rectangular or
scm_make_polar.

For example, (string->number "#i5.0+0i") now does the equivalent of:

  (make-rectangular (exact->inexact 5.0) (exact->inexact 0))

which yields 5.0+0.0i.  Previously it did the equivalent of:

  (exact->inexact (make-rectangular 5.0 0))

which yielded 5.0.

* libguile/numbers.c (mem2ureal): Receive a forced exactness specifier
  (forced_x), create and maintain our own implicit exactness specifier
  flag local to this component (implicit_x), and apply these exactness
  specifiers within this function.  Previously, we received a pointer to
  an implicit exactness specifier flag from above, and the exactness
  specifiers were applied from within scm_i_string_length.

  (mem2complex): Receive a forced exactness specifier parameter and pass
  it down to mem2ureal.  Previously, we passed down a pointer to an
  implicit exactness specifier flag instead.

  (scm_i_string_to_number): No longer create an implicit exactness
  specifier flag here, and do not apply exactness specifiers here.  All
  we do here now regarding exactness is to parse the "#e" or "#i" prefix
  (if any) and pass this information down to mem2ureal via mem2complex
  in the form of an explicit exactness specifier (forced_x).

  (scm_c_make_polar): If the cosine and sine of the angle are both NaNs
  and the magnitude is zero, return 0.0+0.0i instead of +nan.0+nan.0i.
  This case happens when the angle is not finite.

* test-suite/tests/numbers.test (string->number): Move the test cases
  for non-real complex numbers into a separate table in which the
  expected real and imaginary parts are separate entries.  Add several
  new test cases.
2011-02-03 10:50:24 +01:00
Mark H Weaver
041fccf6aa Fix non-portable usage of isinf' in max' and `min'
* numbers.c: Add new macros DOUBLE_IS_POSITIVE_INFINITY and
  DOUBLE_IS_NEGATIVE_INFINITY.
  (scm_max, scm_min): Use the new macros to detect particular
  infinities.  Previously we checked the return value of `isinf'
  to determine the sign of the infinity, but that is not portable.
2011-02-03 10:43:09 +01:00
Mark H Weaver
c721848287 Support non-real complex numbers with inexact zero imaginary part
Add the ability to represent non-real complex numbers whose imaginary
part is an _inexact_ zero (0.0 or -0.0), per R6RS.  Previously, such
numbers were immediately changed into inexact reals.

* libguile/numbers.c: Remove from the list of `General assumptions' in
  numbers.c that objects satisfying SCM_COMPLEXP() have a non-zero
  complex component.  This is no longer true.  Also add a warning
  about another unrelated assumption that is not entirely correct
  (that floor(r) == r implies that mpz_set_d will DTRT; it won't
  if r is infinite).

  (icmplx2str): Always print the imaginary part, even if it is zero.
  Also handle a negative zero imaginary part more gracefully.  It
  now prints 0.0-0.0i, where previously it would print 0.0+-0.0i.

  (mem2ureal): Replace scm_from_double (0.0) with flo0.

  (scm_c_make_rectangular): Always create non-real complex numbers.
  Previously it would create inexact reals if the specified imaginary
  part was zero.

  (scm_make_rectangular): If the imaginary part is an _exact_ 0, return
  the real part unchanged (possibly exact), otherwise return a non-real
  complex number (possibly with an inexact zero imaginary part).
  Previously, it would return an inexact real number whenever the
  imaginary part was any kind of zero.

  (scm_make_polar): If the magnitude is an exact 0, return an exact 0.
  If the angle is an exact 0, return the magnitude unchanged (possibly
  exact).  Otherwise return a non-real complex number (possibly with an
  inexact zero imaginary part).  Previously, it would return a real
  number whenever the imaginary part was any kind of zero.

  (scm_imag_part): Return an exact 0 if applied to a real number.
  Previously it would return an inexact zero if applied to an inexact
  real number.

  (scm_inexact_to_exact): Accept complex numbers with inexact zero
  imaginary part.  In that case, simply use the real part and ignore the
  imaginary part.  Essentially we coerce the inexact zero imaginary part
  to an exact 0.

* test-suite/tests/numbers.test: Add many test cases, and modify
  existing tests as needed to reflect these changes.  Also add a new
  internal predicate: `almost-real-nan?' which tests for a non-real
  complex number with zero imaginary part whose real part is a NaN.

* doc/ref/api-data.texi (Complex Numbers): Update description of complex
  numbers to reflect these changes: non-real complex numbers in Guile
  need not have non-zero imaginary part.  Also, each part of a complex
  number may be any inexact real, not just rationals as was previously
  stated.  Explicitly mention that each part may be an infinity, a NaN,
  or a signed zero.

  (Complex Number Operations): Change the formal parameter names of
  `make-polar' from `x' and `y' to `mag' and `ang'.

* NEWS: Add news entries.
2011-02-02 21:34:01 +01:00
Mark H Weaver
9b9ef10cf0 Improve handling of signed zeroes
* libguile/numbers.c (scm_abs): (abs -0.0) now returns 0.0.  Previously
  it returned -0.0.  Also move the REALP case above the BIGP case,
  and consider it SCM_LIKELY to be REALP if not INUMP.
  (scm_difference): (- 0 0.0) now returns -0.0.  Previously it returned
  0.0.  Also make sure that (- 0 0.0+0.0i) will return -0.0-0.0i.

* test-suite/tests/numbers.test (abs, -): Add test cases, and change
  some tests to use `eqv?' instead of `=', in order to test exactness
  and distinguish signed zeroes.
2011-02-02 21:28:18 +01:00
Mark H Weaver
8deddc948d Trigonometric functions return exact numbers in some cases
* libguile/numbers.c (scm_sin, scm_cos, scm_tan, scm_asin, scm_acos,
  scm_atan, scm_sinh, scm_cosh, scm_tanh, scm_sys_asinh, scm_sys_acosh,
  scm_sys_atanh): Return an exact result in some cases.

* test-suite/tests/numbers.test: Add test cases.

* NEWS: Add NEWS entry
2011-02-02 21:26:35 +01:00
Mark H Weaver
2e2743113a Fix min' and max' handling of NaNs, infinities, and signed zeroes
* libguile/numbers.c (scm_min, scm_max): Properly order the real
  infinities and NaNs, per R6RS, and also take care to handle signed
  zeroes properly.  Note that this ordering is different than that of
  `<', `>', `<=', and `>=', which return #f if any argument is a real
  NaN, and consider the real zeroes to be equal.  The relevant real
  infinity (-inf.0 for min, +inf.0 for max) beats everything, including
  NaNs, and NaNs beat everything else.  Previously these were handled
  improperly in some cases, e.g.:
  (min 1/2 +nan.0) now returns +nan.0 (previously returned 0.5),
  (max 1/2 +nan.0) now returns +nan.0 (previously returned 0.5),
  (min -inf.0 +nan.0) now returns -inf.0 (previously returned +nan.0),
  (max +inf.0 +nan.0) now returns +inf.0 (previously returned +nan.0),
  (min -0.0  0.0) now returns -0.0 (previously returned  0.0),
  (max  0.0 -0.0) now returns  0.0 (previously returned -0.0),
  (max  0   -0.0) now returns  0.0 (previously returned -0.0),
  (max -0.0  0  ) now returns  0.0 (previously returned -0.0).

* test-suite/tests/numbers.test (min, max): Add many more test cases
  relating to NaNs, infinities, and signed zeroes.  Change most existing
  test cases to use `eqv?' instead of `=', in order to check exactness.
2011-02-02 21:21:21 +01:00
Mark H Weaver
7f41099e99 Move comment about trig functions back where it belongs
* libguile/numbers.c: Move a comment about the trigonometric functions
  next to those functions.  At some point they became separated, when
  scm_expt was placed between them.
2011-02-01 21:11:31 +01:00
Mark H Weaver
5e7918077a Handle products with exact 0 differently
* libguile/numbers.c (scm_product): Handle exact 0 differently.  A
  product containing an exact 0 now returns an exact 0 if and only if
  the other arguments are all exact.  An inexact zero is returned if and
  only if the other arguments are all finite but not all exact.  If an
  infinite or NaN value is present, a NaN value is returned.
  Previously, any product containing an exact 0 yielded an exact 0,
  regardless of the other arguments.

  A note on the rationale for (* 0 0.0) returning 0.0 and not exact 0:
  The exactness propagation rules allow us to return an exact result in
  the presence of inexact arguments only if the values of the inexact
  arguments do not affect the result.  In this case, the value of the
  inexact argument _does_ affect the result, because an infinite or NaN
  value causes the result to be a NaN.

  A note on the rationale for (* 0 +inf.0) being a NaN and not exact 0:
  The R6RS requires that (/ 0 0.0) return a NaN value, and that (/ 0.0)
  return +inf.0.  We would like (/ x y) to be the same as (* x (/ y)),
  and in particular, for (/ 0 0.0) to be the same as (* 0 (/ 0.0)),
  which reduces to (* 0 +inf.0).  Therefore (* 0 +inf.0) should return
  a NaN.

* test-suite/tests/numbers.test: Add many multiplication tests.

* NEWS: Add NEWS entry.
2011-02-01 21:11:12 +01:00
Mark H Weaver
605f698026 Fix bugs in `rationalize'
* libguile/numbers.c (scm_rationalize): Fix bugs.  Previously, it
  returned exact integers unmodified, although that was incorrect if
  the epsilon was at least 1 or inexact, e.g. (rationalize 4 1) should
  return 3 per R5RS and R6RS, but previously it returned 4.  Also
  handle cases involving infinities and NaNs properly, per R6RS.

* test-suite/tests/numbers.test: Add test cases for `rationalize'.

* NEWS: Add NEWS entry
2011-02-01 21:08:52 +01:00
Mark H Weaver
bfe1f03aac Improve extensibility of expt' and integer-expt'
* libguile/numbers.c (scm_integer_expt): No longer require that the
  first argument be a number, in order to improve extensibility.  This
  allows us to efficiently raise arbitrary objects to an integer power
  as long as we can multiply those objects.  For example, this allows us
  to efficiently exponentiate matrices if we define only multiplication
  methods for matrices.  Note also that scm_expt calls this procedure
  whenever the exponent is an integer, regardless of the type of the
  first argument.  Also rearrange the order in which we test special
  cases.

* test-suite/tests/numbers.test (expt, integer-expt): Comment out tests
  that required `(expt #t 0)' and `(integer-expt #t 0)' to throw
  exceptions.  Add tests for (expt #t 2) and `(integer-expt #t 2)
  instead.

* NEWS: Add NEWS entry
2011-01-31 21:58:27 +01:00
Mark H Weaver
ac6ce16bc9 Rename {euclidean,centered}_quo_rem to {euclidean,centered}_divide
* libguile/numbers.c (euclidean_quo_rem): Rename to euclidean_divide.
  (centered_quo_rem): Rename to {euclidean,centered}_divide.

* libguile/numbers.h: Rename euclidean_quo_rem to euclidean_divide and
  centered_quo_rem to centered_divide.

* doc/ref/api-data.texi: Rename euclidean_quo_rem to euclidean_divide and
  centered_quo_rem to centered_divide.
2011-01-31 20:22:42 +01:00
Mark H Weaver
2519490c50 Improve extensibility of core numeric procedures
* libguile/numbers.c (scm_quotient, scm_remainder, scm_modulo,
  scm_zero_p, scm_positive_p, scm_negative_p, scm_real_part,
  scm_imag_part, scm_numerator, scm_denominator, scm_magnitude,
  scm_angle, scm_exact_to_inexact): Change from SCM_GPROC to
  SCM_PRIMITIVE_GENERIC.  As a side effect, all of these procedures now
  have documentation strings.

  (scm_exact_p, scm_inexact_p, scm_odd_p, scm_even_p, scm_finite_p,
  scm_inf_p, scm_nan_p, scm_expt, scm_inexact_to_exact, scm_log,
  scm_log10, scm_exp, scm_sqrt): Change from SCM_DEFINE to
  SCM_PRIMITIVE_GENERIC, and make sure the code allows these functions
  to be extended in practice.

  (scm_real_part, scm_imag_part, scm_numerator, scm_denominator,
  scm_inexact_to_exact): Simplify type dispatch code.

  (scm_sqrt): Rename formal argument from x to z, since complex numbers
  are supported.

  (scm_abs): Fix empty FUNC_NAME.

* libguile/numbers.h (scm_finite_p): Add missing prototype.

  (scm_inf_p, scm_nan_p): Rename formal parameter from n to x, since
  the domain is the real numbers.

* test-suite/tests/numbers.test: Test for documentation strings.  Change
  from `expect-fail' to `pass-if' for several of these, and add tests
  for others.  Also add other tests for `real-part' and `imag-part',
  which previously had none.
2011-01-30 23:06:07 +01:00
Mark H Weaver
ff62c16828 Add two new sets of fast quotient and remainder operators
* libguile/numbers.c (scm_euclidean_quo_and_rem, scm_euclidean_quotient,
  scm_euclidean_remainder, scm_centered_quo_and_rem,
  scm_centered_quotient, scm_centered_remainder): New extensible
  procedures `euclidean/', `euclidean-quotient', `euclidean-remainder',
  `centered/', `centered-quotient', `centered-remainder'.

* libguile/numbers.h: Add function prototypes.

* module/rnrs/base.scm: Remove incorrect stub implementations of `div',
  `mod', `div-and-mod', `div0', `mod0', and `div0-and-mod0'.  Instead do
  renaming imports of `euclidean-quotient', `euclidean-remainder',
  `euclidean/', `centered-quotient', `centered-remainder', and
  `centered/', which are equivalent to the R6RS operators.

* module/rnrs/arithmetic/fixnums.scm (fxdiv, fxmod, fxdiv-and-mod,
  fxdiv0, fxmod0, fxdiv0-and-mod0): Remove redundant checks for division
  by zero and unnecessary complexity.
  (fx+/carry): Remove unneeded calls to `inexact->exact'.

* module/rnrs/arithmetic/flonums.scm (fldiv, flmod, fldiv-and-mod,
  fldiv0, flmod0, fldiv0-and-mod0): Remove redundant checks for division
  by zero and unnecessary complexity.  Remove unneeded calls to
  `inexact->exact' and `exact->inexact'

* test-suite/tests/numbers.test: (test-eqv?): New internal predicate for
  comparing numerical outputs with expected values.

  Add extensive test code for `euclidean/', `euclidean-quotient',
  `euclidean-remainder', `centered/', `centered-quotient',
  `centered-remainder'.

* test-suite/tests/r6rs-arithmetic-fixnums.test: Fix some broken test
  cases, and remove `unresolved' test markers for `fxdiv', `fxmod',
  `fxdiv-and-mod', `fxdiv0', `fxmod0', and `fxdiv0-and-mod0'.

* test-suite/tests/r6rs-arithmetic-flonums.test: Remove `unresolved'
  test markers for `fldiv', `flmod', `fldiv-and-mod', `fldiv0',
  `flmod0', and `fldiv0-and-mod0'.

* doc/ref/api-data.texi (Arithmetic): Document `euclidean/',
  `euclidean-quotient', `euclidean-remainder', `centered/',
  `centered-quotient', and `centered-remainder'.

  (Operations on Integer Values): Add cross-references to `euclidean/'
  et al, from `quotient', `remainder', and `modulo'.

* doc/ref/r6rs.texi (rnrs base): Improve documentation for `div', `mod',
  `div-and-mod', `div0', `mod0', and `div0-and-mod0'.  Add
  cross-references to `euclidean/' et al.

* NEWS: Add NEWS entry.
2011-01-30 23:00:38 +01:00
Mark H Weaver
a16982ca4f Add SCM_LIKELY and SCM_UNLIKELY for optimization
* libguile/numbers.c (scm_abs, scm_quotient, scm_remainder, scm_modulo):
  Add SCM_LIKELY and SCM_UNLIKELY in several places for optimization.

  (scm_remainder): Add comment about C99 "%" semantics.
  Strip away a redundant set of braces.
2011-01-30 23:00:27 +01:00
Mark H Weaver
c960e55600 Infinities and NaNs are no longer rational
* libguile/numbers.c (scm_rational_p): Return #f for infinities and
  NaNs, per R6RS.  Previously it returned #t for real infinities
  and NaNs.  They are still considered real by scm_real `real?'
  however, per R6RS.  Also simplify the code.

  (scm_real_p): New implementation to reflect the fact that the
  rationals and reals are no longer the same set.  Previously it just
  called scm_rational_p.

  (scm_integer_p): Simplify the code.

* test-suite/tests/numbers.test: Add test cases for `rational?'
  and `real?' applied to infinities and NaNs.

* doc/ref/api-data.texi (Real and Rational Numbers): Update docs to
  reflect the fact that infinities and NaNs are no longer rational, and
  that `real?'  no longer implies `rational?'.  Improve discussion of
  infinities and NaNs.

* NEWS: Add NEWS entries, and combine with an earlier entry about
  infinities no longer being integers.
2011-01-30 13:08:53 +01:00
Mark H Weaver
2e6e1933b4 equal?' and eqv?' are now equivalent for numbers
Change `equal?' to work like `eqv?' for numbers.
Previously they worked differently in some cases, e.g.
when comparing signed zeroes or NaNs.  For example,
(equal? 0.0 -0.0) returned #t but (eqv? 0.0 -0.0)
returned #f, and (equal? +nan.0 +nan.0) returned #f
but (eqv? +nan.0 +nan.0) returned #t.

* libguile/numbers.c (scm_real_equalp, scm_bigequal,
  scm_complex_equalp, scm_i_fraction_equalp): Move to eq.c.

* libguile/eq.c (scm_real_equalp): Compare flonums using
  real_eqv instead of ==, so that NaNs are now considered
  equal, and to distinguish signed zeroes.

  (scm_complex_equalp): Compare real and imaginary
  components using real_eqv instead of ==, so that NaNs are
  now considered equal, and to distinguish signed zeroes.

  (scm_bigequal): Use scm_i_bigcmp instead of duplicating it.

  (real_eqv): Test for NaNs using isnan(x) instead of
  (x != x), and use SCM_UNLIKELY for optimization.

  (scm_eqv_p): Use scm_bigequal, scm_real_equalp,
  scm_complex_equalp, and scm_i_fraction_equalp to compare
  numbers, instead of inline code.  Those predicates now do
  what scm_eqv_p formerly did internally.  Replace if
  statements with switch statements, as is done in
  scm_equal_p.  Remove useless code to check equality of
  fractions with different SCM_CELL_TYPEs; this was for a
  tentative "lazy reduction bit" which was never developed.

  (scm_eqv_p, scm_equal_p): Remove useless code to check
  equality between inexact reals and non-real complex numbers
  with zero imaginary part.  Such numbers do not exist,
  because the current code is careful to never create them.

* test-suite/tests/numbers.test: Add test cases for
  `eqv?' and `equal?'.  Change existing test case for
  `(equal? +nan.0 +nan.0)' to expect #t instead of #f.

* NEWS: Add NEWS entries.
2011-01-30 13:08:47 +01:00
Mark H Weaver
b5c40589ec Fix bugs when negating SCM_MOST_POSITIVE_FIXNUM+1
* libguile/numbers.c (scm_difference, scm_product):
  Fix bugs when negating SCM_MOST_POSITIVE_FIXNUM+1,
  aka -SCM_MOST_NEGATIVE_FIXNUM.  Previously, these cases
  failed to normalize the result to a fixnum, causing
  `=', `eqv?' and `equal?' to fail, e.g.:
  (= most-negative-fixnum (- 0 (- most-negative-fixnum)))
  (= most-negative-fixnum (* -1 (- most-negative-fixnum)))
  (= most-negative-fixnum (* (- most-negative-fixnum) -1))

* test-suite/test/numbers.test: Add test cases to detect
  bugs when negating SCM_MOST_POSITIVE_FIXNUM+1 and
  SCM_MOST_NEGATIVE_FIXNUM by various methods.
2011-01-28 13:52:46 +01:00
Andy Wingo
10391e06e0 domain of inf?, finite?, nan? is the real numbers
* libguile/numbers.c (scm_inf_p, scm_finite_p, scm_nan_p): The domain of
  these functions is the real numbers.  Error on other input.

* doc/ref/api-data.texi (Reals and Rationals): Update the documentation
  accordingly.

* test-suite/tests/numbers.test ("finite?", "inf?"): Update tests.
2011-01-28 13:43:37 +01:00
Mark H Weaver
a4955a0412 Remove useless code from do_divide
* libguile/numbers.c (do_divide): Remove code which handled a case
  that never occurs: a zero bignum.
2011-01-28 12:26:23 +01:00
Mark H Weaver
41df63cf16 Optimize scm_exact_p by making use of SCM_INEXACTP
* libguile/numbers.c (scm_exact_p): Optimize by making use of the
  SCM_INEXACTP macro.
  (scm_inexact_p): Move it next to scm_exact_p, and add else's.

* test-suite/tests/numbers.test: Add test cases for `exact?'
  and `inexact?' applied to infinities and NaNs.
2011-01-28 12:24:24 +01:00
Mark H Weaver
7112615f73 Implement finite?' in core and fix R6RS finite?' and `infinite?'
* libguile/numbers.c (scm_finite_p): Add new predicate `finite?' from
  R6RS to guile core, which returns #t if and only if its argument is
  neither infinite nor a NaN.  Note that this is not the same as (not
  (inf? x)) or (not (infinite? x)), since NaNs are neither finite nor
  infinite.

* test-suite/tests/numbers.test: Add test cases for `finite?'.

* module/rnrs/base.scm: Import `inf?' as `infinite?' instead of
  reimplementing it.  Previously, the R6RS implementation of
  `infinite?' did not detect non-real complex infinities, nor did it
  throw exceptions for non-numbers.  (Note that NaNs _are_ considered
  numbers by scheme, despite their name).

  Import `finite?' instead of reimplementing it.  Previously, the R6RS
  implementation of `finite?' returned #t for both NaNs and non-real
  complex infinities, in violation of R6RS.

* NEWS: Add NEWS entries, and reorganize existing numerics-related
  entries together under one subheading.

* doc/ref/api-data.texi (Real and Rational Numbers): Add docs for
  `finite?' and scm_finite_p.
2011-01-28 12:21:14 +01:00
Mark H Weaver
cff5fa3384 Add SCM_INUM1 to numbers.h, and make use of it and SCM_INUM0 in numbers.c
* libguile/numbers.h: Add SCM_INUM1, a name for the fixnum 1.  This is
  analogous to SCM_INUM0, a name for 0, which already existed.

* libguile/numbers.c: Change occurrences of SCM_I_MAKINUM (0) and
  SCM_I_MAKINUM (1) to SCM_INUM0 and SCM_INUM1, respectively.
2011-01-28 12:18:12 +01:00
Ludovic Courtès
a917871527 Remove `scm_lfwrite_str'.
* libguile/ports.c (scm_lfwrite_str): Remove.

* libguile/ports.h (scm_lfwrite_str): Remove declaration.

* libguile/numbers.c (scm_i_print_fraction): Use `scm_display' instead
  of `scm_lfwrite_str'.
2011-01-23 01:14:51 +01:00
Andy Wingo
8e43ed5d0b infinities are no longer integers
* libguile/numbers.c (scm_is_integer): Infinities are not integers, per
  the R6RS.
  (scm_even_p, scm_odd_p): Passing an infinity to even? or odd? is an
  error.

* test-suite/tests/numbers.test ("integer?"): Adapt test.
  ("expt"): Add tests for +inf.0 and -inf.0 exponents.

* NEWS: Add NEWS entries.
2011-01-23 00:06:24 +01:00
Mark H Weaver
01c7284ae5 Fix bugs in expt and integer-expt
* libguile/numbers.c (scm_expt): Fix bug that caused expt to throw an
  exception whenever the base was exact and the exponent was an
  inexact integer, e.g. (expt 5 6.0).

  (scm_expt): Fix bug that caused expt to introduce spurious imaginary
  parts in the result when the base was an inexact negative real and
  the exponent was an integer, e.g. (expt -1.0 2)

  (scm_integer_expt, scm_expt): Change behavior of (integer-expt 0 -1),
  and therefore also (expt 0 -1), to return NaN, per R6RS (actually,
  R6RS says we should throw an exception or return an "unspecified
  number object", but for now we use NaN).  Formerly we returned 0, per
  R5RS. R5RS claims that 0^x=0 for all non-zero x, but that's
  mathematically incorrect, and probably an oversight.

  (scm_integer_expt): Consistently throw a wrong-argument-type exception
  when the exponent is inexact.  Formerly, it didn't always check this
  if the base was 0, 1, or -1.

* test-suite/tests/numbers.test ("integer-expt", "expt"): Add tests.
2011-01-20 23:28:37 +01:00
Andy Wingo
e25f37271a fix a number of assuptions that a long could hold an inum
* libguile/bytevectors.c:
* libguile/goops.c:
* libguile/instructions.c:
* libguile/numbers.c:
* libguile/random.c:
* libguile/read.c:
* libguile/vm-i-scheme.c: Fix a number of assumptions that a long could
  hold an inum. This is not the case on platforms whose void* is larger
  than their long.

* libguile/numbers.c (scm_i_inum2big): New helper, only implemented for
  sizeof(void*) == sizeof(long); produces a compile error on other
  platforms. Basically gmp doesn't have a nice interface for converting
  between mpz values and intmax_t.
2010-11-19 15:22:43 +01:00
Andy Wingo
9b5fcde6f9 lower-case hexadecimal digits again
* libguile/numbers.c: Default to lower-case hexadecimal digits again.
2010-11-18 13:10:45 +01:00
Andy Wingo
a6f3af1618 fix string->number for bases > 16
* libguile/numbers.c (scm_iuint2str): Add an assertion on the domain of
  the radix. Use the number_chars table to write the string, instead of
  doing strange math. Same effect, though.
  (mem2uinteger, char_decimal_value): Change logic to allow all ascii
  alphabetic chars as decimals, not just a-f. Thanks to Nils Gey for the
  report.

* test-suite/tests/numbers.test ("number->string"): Add some tests.
2010-11-15 23:43:30 +01:00
Ludovic Courtès
03604fcf50 Allocate complex numbers in contiguous pointer-less memory.
* libguile/numbers.h (SCM_COMPLEX_MEM): Remove.
  (SCM_COMPLEX_REAL): Change to just fetch the `real' field of the
  pointed-to `scm_t_complex'.
  (SCM_COMPLEX_IMAG): Likewise.
  (scm_t_complex)[type, pad]: New fields.

* libguile/numbers.c (scm_c_make_rectangular): Allocate the whole
  complex contiguously, with `scm_gc_malloc_pointerless'.
2010-10-13 00:06:30 +02:00
Ludovic Courtès
978c52d108 Allocate inexact numbers in pointer-less memory.
* libguile/numbers.c (scm_from_double): Use `scm_gc_malloc_pointerless'
  instead of `scm_double_cell'.
2010-10-13 00:06:29 +02:00
Ludovic Courtès
864e7d424e Fix bignum memory leak.
The `mpz_t' associated with a bignum would never be freed, so an
expression like `(while #t (expt 2 5000))' would quickly lead to memory
exhaustion.

* libguile/numbers.c (finalize_bignum): New function.
  (make_bignum): Register it as a finalizer for P.
2010-09-27 11:20:14 +02:00
Ludovic Courtès
d017fcdfcb Allocate bignums in pointer-less memory.
* libguile/numbers.c (make_bignum): New function.
  (scm_i_mkbig, scm_i_long2big, scm_i_ulong2big, scm_i_clonebig,
  scm_i_dbl2big, scm_i_mpz2num): Use it.
2010-09-27 11:10:01 +02:00
Michael Gran
23295dc35c Incorrect parsing of NaN complex numbers
* libguile/numbers.c (mem2complex): modified
* test-suite/tests/reader.test: new complex NaN reader test
2010-09-25 09:07:21 -07:00
Ludovic Courtès
f328f86230 Clarify feature macro conditionals.
* libguile/numbers.c (scm_log10): Check whether `HAVE_COMPLEX_DOUBLE'
  and `HAVE_CLOG10' are defined instead of checking whether they are
  non-zero.
  (scm_sqrt): Likewise for `HAVE_COMPLEX_DOUBLE' and
  `HAVE_USABLE_CSQRT'.
2010-09-08 00:38:49 +02:00
Ludovic Courtès
2e65b52f8a Use Gnulib's isnan' and isinf' modules.
This updates Gnulib to v0.0-4219-g84cdd8b.

* m4/gnulib-cache.m4: Add `isinf' and `isnan'.

* configure.ac: Remove checks for `floatingpoint.h', `ieeefp.h', and
  `nan.h'.

* libguile/gen-scmconfig.c (main): Remove definitions of
  `SCM_HAVE_FLOATINGPOINT_H', `SCM_HAVE_IEEEFP_H', and
  `SCM_HAVE_NAN_H'.

* libguile/numbers.c (isnan)[SCO && !HAVE_ISNAN]: Remove.
  (isinf)[SCO && !HAVE_ISINF]: Remove.
  (xisinf, xisnan): Remove.  Change callers to use `isinf' and `isnan'.
  (guile_ieee_init): Remove the `defined HAVE_ISINF' and `define
  HAVE_ISNAN' conditions.

* libguile/numbers.h: Remove code conditional on
  `SCM_HAVE_FLOATINGPOINT_H', `SCM_HAVE_IEEEFP_H', or `SCM_HAVE_NAN_H'.
2010-09-08 00:34:27 +02:00
Andy Wingo
8a4ed2dd34 remove "discouraged" infrastructure
* libguile/discouraged.h: Remove.

* libguile/deprecated.c (scm_internal_select, scm_thread_sleep)
  (scm_thread_usleep): Deprecate formerly discouraged names.

* libguile/eq.h (SCM_EQ_P):
* libguile/pairs.h (SCM_NULLP, SCM_NNULLP, SCM_CONSP, SCM_NCONSP):
* libguile/boolean.h (SCM_FALSEP, SCM_NFALSEP, SCM_BOOLP, SCM_BOOL):
  (SCM_NEGATE_BOOL, SCM_BOOL_NOT): Undiscourage these names, because I'm
  not sure deprecating them will do any good.

* libguile.h:
* libguile/gen-scmconfig.c:
* libguile/numbers.c:
* libguile/init.c:
* libguile/Makefile.am:
* configure.ac: Remove bits that referenced discouraged.h, and dealt
  with the "discouraging" system.
2010-08-08 14:15:47 +02:00
Andy Wingo
220058a835 deprecated all discouraged functions
* libguile/Makefile.am:
* libguile/discouraged.c: Remove discouraged.c.

* libguile/deprecated.c:
* libguile/deprecated.h:
* libguile/discouraged.h: All functions and declarations moved from
  discouraged.[ch] to deprecated.[ch], adding deprecation warnings.

* libguile/init.c: Remove discouraged init.

* libguile/numbers.c (scm_num2float, scm_num2double): Deprecate.

* test-suite/standalone/test-num2integral.c: Port to modern API.
2010-08-08 13:48:35 +02:00
Andy Wingo
4ca4826997 remove SCM_HAVE_T_INT64, SCM_HAVE_T_UINT64
* libguile/__scm.h:
* libguile/numbers.h:
* libguile/random.c:
* libguile/srfi-4.c:
* libguile/srfi-4.h:
* libguile/numbers.c:
* test-suite/standalone/test-conversion.c:
* libguile/gen-scmconfig.c: As we require 64-bit integers in
  configure.ac, remove conditional definition of 64-bit types.
2010-07-26 15:00:49 +02:00
Andy Wingo
5a8fc758b0 expt / integer-expt fixes
* libguile/numbers.c (scm_integer_expt): Validate the first arg as a
  number.
  (scm_expt): Delegate to scm_integer_expt iff x is exact. Fixes
  fractions.test, which I broke recently

* test-suite/tests/numbers.test ("integer-expt"): Add test for
  (integer-expt #t 0).
2010-06-10 18:14:02 +02:00
Andy Wingo
a4082ab57e fix (expt #t 0)
* libguile/numbers.c (scm_expt): Fix check regarding when to dispatch to
  integer-expt.

* test-suite/tests/numbers.test ("expt"): Add test.
2010-06-07 23:27:55 +02:00
Ludovic Courtès
8c93b597b3 Fix the visibility of a few of internal symbols.
* libguile/gc.h (scm_i_gc): Make internal.
* libguile/posix.h (scm_i_locale_mutex): Likewise.
* libguile/arrays.h (scm_i_tc16_array): Likewise.
* libguile/numbers.c (scm_i_num_less_p): Likewise.
* libguile/discouraged.h (scm_i_init_discouraged): Likewise.
* libguile/continuations.c (scm_i_dummy): Made static.
* libguile/gc.c (scm_i_cell_validation_already_running): Likewise.
* libguile/discouraged.h (scm_i_init_discouraged): Likewise.
2010-05-28 17:02:13 +02:00
Andy Wingo
e7efe8e793 decruftify scm_sys_protects
* libguile/root.h
* libguile/root.c (scm_sys_protects): It used to be that for some reason
  we'd define a special array of "protected" values. This was a little
  silly, always, but with the BDW GC it's completely unnecessary. Also
  many of these variables were unused, and none of them were good API.
  So remove this array, and either eliminate, make static, or make
  internal the various values.

* libguile/snarf.h: No need to generate calls to scm_permanent_object.

* guile-readline/readline.c (scm_init_readline): No need to call
  scm_permanent_object.

* libguile/array-map.c (ramap, rafe): Remove the dubious nullvect
  optimizations.

* libguile/async.c (scm_init_async): No need to init scm_asyncs, it is
  no more.

* libguile/eval.c (scm_init_eval): No need to init scm_listofnull, it is
  no more.

* libguile/gc.c: Make scm_protects a static var.
  (scm_storage_prehistory): Change the sanity check to use the address
  of protects.
  (scm_init_gc_protect_object): No need to clear the scm_sys_protects,
  as it is no more.

* libguile/keywords.c: Make the keyword obarray a static var.
* libguile/numbers.c: Make flo0 a static var.
* libguile/objprop.c: Make object_whash a static var.
* libguile/properties.c: Make properties_whash a static var.

* libguile/srcprop.h:
* libguile/srcprop.c: Make scm_source_whash a global with internal
  linkage.

* libguile/strings.h:
* libguile/strings.c: Make scm_nullstr a global with internal linkage.

* libguile/vectors.c (scm_init_vectors): No need to init scm_nullvect,
  it's unused.
2009-12-05 12:38:43 +01:00