>From f0e96e00a247d9a31e73f4fe901b6471ce73e902 Mon Sep 17 00:00:00 2001 From: Mark H Weaver Date: Wed, 26 Jan 2011 09:36:05 -0500 Subject: [PATCH] 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. --- libguile/numbers.c | 40 +++++++++++++++++++--------------------- test-suite/tests/numbers.test | 9 ++++++++- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/libguile/numbers.c b/libguile/numbers.c index 174ad23..f417559 100644 --- a/libguile/numbers.c +++ b/libguile/numbers.c @@ -503,15 +503,28 @@ SCM_DEFINE (scm_exact_p, "exact?", 1, 0, 0, "otherwise.") #define FUNC_NAME s_scm_exact_p { - if (SCM_I_INUMP (x)) - return SCM_BOOL_T; - if (SCM_BIGP (x)) + if (SCM_INEXACTP (x)) + return SCM_BOOL_F; + else if (SCM_NUMBERP (x)) return SCM_BOOL_T; - if (SCM_FRACTIONP (x)) + else + SCM_WRONG_TYPE_ARG (1, x); +} +#undef FUNC_NAME + + +SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0, + (SCM x), + "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n" + "else.") +#define FUNC_NAME s_scm_inexact_p +{ + if (SCM_INEXACTP (x)) return SCM_BOOL_T; - if (SCM_NUMBERP (x)) + else if (SCM_NUMBERP (x)) return SCM_BOOL_F; - SCM_WRONG_TYPE_ARG (1, x); + else + SCM_WRONG_TYPE_ARG (1, x); } #undef FUNC_NAME @@ -3364,21 +3377,6 @@ SCM_DEFINE (scm_integer_p, "integer?", 1, 0, 0, #undef FUNC_NAME -SCM_DEFINE (scm_inexact_p, "inexact?", 1, 0, 0, - (SCM x), - "Return @code{#t} if @var{x} is an inexact number, @code{#f}\n" - "else.") -#define FUNC_NAME s_scm_inexact_p -{ - if (SCM_INEXACTP (x)) - return SCM_BOOL_T; - if (SCM_NUMBERP (x)) - return SCM_BOOL_F; - SCM_WRONG_TYPE_ARG (1, x); -} -#undef FUNC_NAME - - SCM scm_i_num_eq_p (SCM, SCM, SCM); SCM_PRIMITIVE_GENERIC (scm_i_num_eq_p, "=", 0, 2, 1, (SCM x, SCM y, SCM rest), diff --git a/test-suite/tests/numbers.test b/test-suite/tests/numbers.test index d9a75f3..27de045 100644 --- a/test-suite/tests/numbers.test +++ b/test-suite/tests/numbers.test @@ -240,7 +240,11 @@ (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1))))) (pass-if "sqrt ((fixnum-max+1)^2 - 1)" - (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1))))))) + (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1))))) + + (pass-if (not (exact? +inf.0))) + (pass-if (not (exact? -inf.0))) + (pass-if (not (exact? +nan.0))))) ;;; ;;; exp @@ -1559,6 +1563,9 @@ (pass-if (not (inexact? (- 1 fixnum-min)))) (pass-if (inexact? 1.3)) (pass-if (inexact? 3.1+4.2i)) + (pass-if (inexact? +inf.0)) + (pass-if (inexact? -inf.0)) + (pass-if (inexact? +nan.0)) (pass-if-exception "char" exception:wrong-type-arg (not (inexact? #\a))) -- 1.5.6.5