Skip to content

Commit

Permalink
fromEnumTo consistent with enumFromTo, barring rounding errors.]
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornbm committed Mar 4, 2014
1 parent 9a03614 commit aadffc6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 8 additions & 4 deletions Numeric/Units/Dimensional/DK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion Numeric/Units/Dimensional/DK/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.