module Bayes.Tools (
nearlyEqual
, withTempFile
, minBoundForEnum
, maxBoundForEnum
, intValue
) where
import System.IO(openTempFile,hClose)
import System.Directory(getTemporaryDirectory,removeFile)
import System.FilePath
nearlyEqual :: Double -> Double -> Bool
nearlyEqual a b =
let absA = abs a
absB = abs b
diff = abs (ab)
epsilon = 2e-5
in
case (a,b) of
(x,y) | x == y -> True
| x*y == 0 -> diff < (epsilon * epsilon)
| otherwise -> diff / (absA + absB) < epsilon
withTempFile :: (FilePath -> IO a) -> IO a
withTempFile action = do
tempDir <- getTemporaryDirectory
(filePath,fileHandle) <- openTempFile tempDir "bayestest"
hClose fileHandle
result <- action filePath
removeFile (tempDir </> filePath)
return result
minBoundForEnum :: Bounded a => a -> a
minBoundForEnum _ = minBound
maxBoundForEnum :: Bounded a => a -> a
maxBoundForEnum _ = maxBound
intValue :: Enum a => a -> Int
intValue = fromEnum