0

I want use quickcheck in a function that tests if a Maclaurin series is equal to 1/x, for x>1 and x<2. However, for small values of n, quickcheck returns false tests. Additionally, if I put n>100 restriction, for example, quickcheck returns:

"Gave up! Passed only 0 tests.".

Here's my code:

prop_inv :: Float -> Int -> Property
prop_inv x n = (x>1 && x<2) && n>100 ==> inv x n == 1/x

(inv x n is the function that calculates the Maclaurin series.)

AJF
  • 11,767
  • 2
  • 37
  • 64
89 Juan
  • 3
  • 1

1 Answers1

1

The usual way is to make a small newtype that only generates values in the desired range. For example:

newtype BetweenOneAndTwo = BOAT Float deriving Show
instance Arbitrary BetweenOneAndTwo where
    arbitrary = BOAT <$> Test.QuickCheck.choose (1, 2)

prop_inv (BOAT x) (NonNegative n) = inv x (n+100) == 1/x
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • I have this error "Ambiguous occurrence 'choose'" because we have a 'choose' definition on probability.hs and we need it. – 89 Juan Jun 12 '17 at 18:03
  • And it request "No instance for (Show BetweenOneAndTwo)" – 89 Juan Jun 12 '17 at 18:18
  • @FilipeNunes You can use qualified imports to differentiate between the two `choose`s, and you can add `deriving Show` at the end of the newtype declaration to get an auto-generated `Show` instance. I've updated the answer showing how to do these two things. – Daniel Wagner Jun 12 '17 at 18:41
  • Still have this: *Main> quickCheck prop_inv *** Failed! Falsifiable (after 2 tests and 1 shrink): BOAT 1.8863573 NonNegative {getNonNegative = 0} – 89 Juan Jun 12 '17 at 18:55
  • It's possible to evaluate only some decimal place in float in expression inv x (n+100) == 1/x ? – 89 Juan Jun 12 '17 at 19:11
  • @FilipeNunes, this is a whole different question. Yes, but it's not built-in -- you have to program it using math tricks. – luqui Jun 12 '17 at 19:35