Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Approximate-equality/Haskell/approximate-equality-1.hs
Normal file
13
Task/Approximate-equality/Haskell/approximate-equality-1.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
class (Num a, Ord a, Eq a) => AlmostEq a where
|
||||
eps :: a
|
||||
|
||||
infix 4 ~=
|
||||
(~=) :: AlmostEq a => a -> a -> Bool
|
||||
a ~= b = or [ a == b
|
||||
, abs (a - b) < eps * abs(a + b)
|
||||
, abs (a - b) < eps ]
|
||||
|
||||
instance AlmostEq Int where eps = 0
|
||||
instance AlmostEq Integer where eps = 0
|
||||
instance AlmostEq Double where eps = 1e-14
|
||||
instance AlmostEq Float where eps = 1e-5
|
||||
17
Task/Approximate-equality/Haskell/approximate-equality-2.hs
Normal file
17
Task/Approximate-equality/Haskell/approximate-equality-2.hs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
test :: [(Double, Double)]
|
||||
test = [(100000000000000.01, 100000000000000.011)
|
||||
,(100.01, 100.011)
|
||||
,(10000000000000.001 / 10000.0, 1000000000.0000001000)
|
||||
,(0.001, 0.0010000001)
|
||||
,(0.000000000000000000000101, 0.0)
|
||||
,(sqrt 2 * sqrt 2, 2.0)
|
||||
,(-sqrt 2 * sqrt 2, -2.0)
|
||||
,(3.141592653589793, 3.141592653589794)
|
||||
,(3.141592653589, 3.141592653589794)]
|
||||
|
||||
-- requires import Text.Printf
|
||||
main = mapM_ runTest test
|
||||
where
|
||||
runTest (a, b) = do
|
||||
printf "%f == %f %v\n" a b (show $ a==b) :: IO ()
|
||||
printf "%f ~= %f %v\n\n" a b (show $ a~=b)
|
||||
Loading…
Add table
Add a link
Reference in a new issue