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

New functions scm_is_exact and scm_is_inexact

* 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
This commit is contained in:
Mike Gran 2011-10-09 20:54:37 -07:00
parent b2c4c3e5e3
commit 022dda6901
4 changed files with 58 additions and 0 deletions

View file

@ -747,12 +747,25 @@ otherwise.
@end deffn
@deftypefn {C Function} int scm_is_exact (SCM z)
Return a @code{1} if the number @var{z} is exact, and @code{0}
otherwise. This is equivalent to @code{scm_is_true (scm_exact_p (z))}.
An alternate approch to testing the exactness of a number is to
use @code{scm_is_signed_integer} or @code{scm_is_unsigned_integer}.
@end deftypefn
@deffn {Scheme Procedure} inexact? z
@deffnx {C Function} scm_inexact_p (z)
Return @code{#t} if the number @var{z} is inexact, @code{#f}
else.
@end deffn
@deftypefn {C Function} int scm_is_inexact (SCM z)
Return a @code{1} if the number @var{z} is inexact, and @code{0}
otherwise. This is equivalent to @code{scm_is_true (scm_inexact_p (z))}.
@end deftypefn
@deffn {Scheme Procedure} inexact->exact z
@deffnx {C Function} scm_inexact_to_exact (z)
Return an exact number that is numerically closest to @var{z}, when

View file

@ -536,6 +536,11 @@ SCM_PRIMITIVE_GENERIC (scm_exact_p, "exact?", 1, 0, 0,
}
#undef FUNC_NAME
int
scm_is_exact (SCM val)
{
return scm_is_true (scm_exact_p (val));
}
SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0,
(SCM x),
@ -552,6 +557,11 @@ SCM_PRIMITIVE_GENERIC (scm_inexact_p, "inexact?", 1, 0, 0,
}
#undef FUNC_NAME
int
scm_is_inexact (SCM val)
{
return scm_is_true (scm_inexact_p (val));
}
SCM_PRIMITIVE_GENERIC (scm_odd_p, "odd?", 1, 0, 0,
(SCM n),

View file

@ -165,6 +165,7 @@ typedef struct scm_t_complex
SCM_API SCM scm_exact_p (SCM x);
SCM_API int scm_is_exact (SCM x);
SCM_API SCM scm_odd_p (SCM n);
SCM_API SCM scm_even_p (SCM n);
SCM_API SCM scm_finite_p (SCM x);
@ -241,6 +242,7 @@ SCM_API SCM scm_real_p (SCM x);
SCM_API SCM scm_rational_p (SCM z);
SCM_API SCM scm_integer_p (SCM x);
SCM_API SCM scm_inexact_p (SCM x);
SCM_API int scm_is_inexact (SCM x);
SCM_API SCM scm_num_eq_p (SCM x, SCM y);
SCM_API SCM scm_less_p (SCM x, SCM y);
SCM_API SCM scm_gr_p (SCM x, SCM y);

View file

@ -1078,6 +1078,37 @@ test_locale_strings ()
test_11 ("(string #\\f #\\nul)", NULL, 1, 0);
}
static void
test_is_exact ()
{
if (1 != scm_is_exact (scm_c_eval_string ("3")))
{
fprintf (stderr, "fail: scm_is_exact (\"3\") = 1\n");
exit (EXIT_FAILURE);
}
if (0 != scm_is_exact (scm_c_eval_string ("3.0")))
{
fprintf (stderr, "fail: scm_is_exact (\"3.0\") = 0\n");
exit (EXIT_FAILURE);
}
}
static void
test_is_inexact ()
{
if (1 !=scm_is_inexact (scm_c_eval_string ("3.0")))
{
fprintf (stderr, "fail: scm_is_inexact (\"3.0\") = 1\n");
exit (EXIT_FAILURE);
}
if (0 != scm_is_inexact (scm_c_eval_string ("3")))
{
fprintf (stderr, "fail: scm_is_inexact (\"3\") = 0\n");
exit (EXIT_FAILURE);
}
}
static void
tests (void *data, int argc, char **argv)
{
@ -1091,6 +1122,8 @@ tests (void *data, int argc, char **argv)
test_from_double ();
test_to_double ();
test_locale_strings ();
test_is_exact ();
test_is_inexact ();
}
int