Permalink
Browse files

fromEnumTo consistent with enumFromTo, barring rounding errors.]

  • Loading branch information...
1 parent 9a03614 commit aadffc67fcf8e981931bedc589154f29f3b3fabf @bjornbm committed Mar 4, 2014
Showing with 17 additions and 5 deletions.
  1. +8 −4 Numeric/Units/Dimensional/DK.hs
  2. +9 −1 Numeric/Units/Dimensional/DK/Test.hs
View
12 Numeric/Units/Dimensional/DK.hs
@@ -88,7 +88,7 @@ module Numeric.Units.Dimensional.DK
import Prelude
( Show, Eq, Ord, Num, Fractional, Floating, RealFloat, Functor, fmap
- , (.), flip, show, (++), undefined, otherwise, (==), (<=), String, unwords
+ , (.), flip, show, (++), undefined, otherwise, (==), (<=), (>=), String, unwords
, map, null, Integer, Int, ($), zipWith, uncurry, concat, iterate, takeWhile
)
import qualified Prelude
@@ -471,17 +471,21 @@ fromIncr :: (Ord a, Num a)
fromIncr x0 dx = iterate (+ dx) x0
-- | @fromIncrTo x0 dx xf@ corresponds to @[x0, x0+dx .. xf]@.
-fromIncrTo :: (Ord a, Num a)
+fromIncrTo :: (Ord a, Fractional a)
=> Quantity d a -> Quantity d a -> Quantity d a -> [Quantity d a]
-fromIncrTo x0 dx xf = takeWhile (<= xf) $ fromIncr x0 dx
+--fromIncrTo x0 dx xf = let op = if dx >= _0 then (<=) else (>=)
+-- in takeWhile (`op` xf) $ fromIncr x0 dx
+fromIncrTo x0 dx xf = if dx >= _0
+ then takeWhile (<= xf + dx / _2) $ fromIncr x0 dx
+ else takeWhile (>= xf + dx / _2) $ fromIncr x0 dx
-- | @fromThen x0 dx@ corresponds to @enumFromThen@ or @[x0, x1 ..]@.
fromThen :: (Ord a, Num a)
=> Quantity d a -> Quantity d a -> [Quantity d a]
fromThen x0 x1 = fromIncr x0 (x1 - x0)
-- | @fromThenTo x0 x1 xf@ corresponds to @enumFromThenTo@ or @[x0, x1 .. xf]@.
-fromThenTo :: (Ord a, Num a)
+fromThenTo :: (Ord a, Fractional a)
=> Quantity d a -> Quantity d a -> Quantity d a -> [Quantity d a]
fromThenTo x0 x1 = fromIncrTo x0 (x1 - x0)
View
10 Numeric/Units/Dimensional/DK/Test.hs
@@ -4,7 +4,9 @@ module Numeric.Units.Dimensional.DK.Test where
import Numeric.Units.Dimensional.DK.Prelude
import qualified Prelude
+import Data.AEq
import Test.HUnit
+import Test.QuickCheck
testPower = TestLabel "Power test" $ TestList
[ TestCase $ (9 *~ one) @=? (3 *~ one) ^ pos2
@@ -31,5 +33,11 @@ tests = TestList
, testShow
]
-main = runTestTT tests
+prop_fromEnumTo x0 x1 xf = let
+ xs = enumFromThenTo (x0::Double) x1 xf
+ ys = fromThenTo (x0*~one) (x1*~one) (xf*~one) /~~ one
+ in take 100 xs ~== take 100 ys
+main = do
+ runTestTT tests
+ quickCheck prop_fromEnumTo

0 comments on commit aadffc6

Please sign in to comment.