1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-23 12:00:21 +02:00

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.
This commit is contained in:
Mark H Weaver 2011-01-26 09:34:02 -05:00 committed by Andy Wingo
parent cff5fa3384
commit 7112615f73
5 changed files with 89 additions and 13 deletions

View file

@ -79,6 +79,10 @@
typedef scm_t_signed_bits scm_t_inum;
#define scm_from_inum(x) (scm_from_signed_integer (x))
/* Tests to see if a C double is neither infinite nor a NaN.
TODO: if it's available, use C99's isfinite(x) instead */
#define DOUBLE_IS_FINITE(x) (!isinf(x) && !isnan(x))
/*
@ -581,6 +585,24 @@ SCM_DEFINE (scm_even_p, "even?", 1, 0, 0,
}
#undef FUNC_NAME
SCM_DEFINE (scm_finite_p, "finite?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is neither infinite\n"
"nor a NaN, @code{#f} otherwise.")
#define FUNC_NAME s_scm_finite_p
{
if (SCM_REALP (x))
return scm_from_bool (DOUBLE_IS_FINITE (SCM_REAL_VALUE (x)));
else if (SCM_COMPLEXP (x))
return scm_from_bool (DOUBLE_IS_FINITE (SCM_COMPLEX_REAL (x))
&& DOUBLE_IS_FINITE (SCM_COMPLEX_IMAG (x)));
else if (SCM_NUMBERP (x))
return SCM_BOOL_T;
else
SCM_WRONG_TYPE_ARG (1, x);
}
#undef FUNC_NAME
SCM_DEFINE (scm_inf_p, "inf?", 1, 0, 0,
(SCM x),
"Return @code{#t} if @var{x} is either @samp{+inf.0}\n"