Output with precision 5
This commit is contained in:
parent
b4bbd298a0
commit
e38576c1d8
1 changed files with 17 additions and 2 deletions
19
app/Main.hs
19
app/Main.hs
|
@ -2,16 +2,31 @@ module Main (main) where
|
|||
|
||||
import Text.Parsec
|
||||
|
||||
import Lib (exprparser, evaluate)
|
||||
import Lib (exprparser, evaluate, replaceVars)
|
||||
|
||||
import Data.Map (Map)
|
||||
import qualified Data.Map as Map
|
||||
import Data.Ratio
|
||||
|
||||
import System.IO
|
||||
|
||||
main :: IO ()
|
||||
main = ioLoop
|
||||
|
||||
useResult (Right e) = show . evaluate $ e
|
||||
precision = 5 :: Int
|
||||
|
||||
showRatio :: Int -> Rational -> String
|
||||
showRatio p r = (if (r < 0) then "-" else "") ++ prepoint_digits ++ "." ++ postpoint_digits
|
||||
where
|
||||
prepoint_digits = init . show . round $ (r * 10)
|
||||
postpoint_digits = (take p) . (drop (length prepoint_digits)) . show . round $ (r * 10^p)
|
||||
|
||||
useResult (Right e) = (showRatio precision) . evaluate $ replaceVars e vars
|
||||
useResult (Left e) = show e
|
||||
|
||||
vars :: Map String Rational
|
||||
vars = Map.fromList [("pi", 245850922 % 78256779), ("e", 271801 % 99990)]
|
||||
|
||||
ioLoop :: IO ()
|
||||
ioLoop = do done <- isEOF
|
||||
if done
|
||||
|
|
Loading…
Reference in a new issue