summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Ethier <justin.ethier@gmail.com>2015-09-28 22:43:13 -0400
committerJustin Ethier <justin.ethier@gmail.com>2015-09-28 22:43:13 -0400
commit74acc3a4d5ae9bd0ce58efa2778d6dd4ac6a2e16 (patch)
treecdefb7c7faff7f6026b2f1e4d4d06ed67b840f81
parent59d79b55034ec4e3f1236509b55b6af6c18740ad (diff)
Issue #203 - Tweaked (real?) to be true if both (zero? (imag-part z)) and (exact? (imag-part z)) are truev3.19.1
-rw-r--r--hs-src/Language/Scheme/Numerical.hs15
-rw-r--r--tests/t-numerical-ops.scm5
2 files changed, 17 insertions, 3 deletions
diff --git a/hs-src/Language/Scheme/Numerical.hs b/hs-src/Language/Scheme/Numerical.hs
index 95e4f76f..ed30d3e9 100644
--- a/hs-src/Language/Scheme/Numerical.hs
+++ b/hs-src/Language/Scheme/Numerical.hs
@@ -479,7 +479,12 @@ numRealPart badArgList = throwError $ NumArgs (Just 1) badArgList
-- |Retrieve imaginary part of a complex number
numImagPart :: [LispVal] -> ThrowsError LispVal
-numImagPart [(Complex c)] = return $ Float $ imagPart c
+numImagPart [(Complex c)] = do
+ let n = imagPart c
+ f = Float n
+ if isFloatAnInteger f
+ then return $ Number $ floor n
+ else return f
numImagPart [(Float _)] = return $ Number 0
numImagPart [(Rational _)] = return $ Number 0
numImagPart [(Number _)] = return $ Number 0
@@ -594,7 +599,13 @@ isReal :: [LispVal] -> ThrowsError LispVal
isReal ([Number _]) = return $ Bool True
isReal ([Rational _]) = return $ Bool True
isReal ([Float _]) = return $ Bool True
-isReal ([Complex c]) = return $ Bool $ (imagPart c) == 0
+isReal ([Complex c]) = do
+ imagPt <- numImagPart [(Complex c)]
+ isExact <- isNumExact [imagPt]
+ isZero <- numBoolBinopEq [imagPt, (Number 0)]
+ case (isExact, isZero) of
+ (Bool True, Bool True) -> return $ Bool True
+ _ -> return $ Bool False
isReal _ = return $ Bool False
-- |Predicate to determine if given number is a rational.
diff --git a/tests/t-numerical-ops.scm b/tests/t-numerical-ops.scm
index 2ea38ec1..e868f18c 100644
--- a/tests/t-numerical-ops.scm
+++ b/tests/t-numerical-ops.scm
@@ -11,8 +11,11 @@
(assert/equal (complex? 3+4i) #t)
(assert/equal (complex? 3) #t)
(assert/equal (real? 3) #t)
-(assert/equal (real? -2.5+0.0i) #t)
+(assert/equal (real? -2.5+0i) #t)
+(assert/equal (real? -2.5+0.1i) #f)
+;(assert/equal (real? -2.5+0.0i) #f)
;Issue #14: (assert/equal (real? #e1e103) #t)
+; TODO: (assert/equal (rational? 3.5) #t)
(assert/equal (rational? 6/10) #t)
(assert/equal (rational? 6/3) #t)
(assert/equal (integer? 3+0i) #t)