diff options
| author | Raymond Hogenson <rhogenson@posteo.net> | 2018-05-18 12:01:22 -0800 |
|---|---|---|
| committer | Raymond Hogenson <rhogenson@posteo.net> | 2018-05-18 12:01:22 -0800 |
| commit | fb1f8d3a02908e2fe8c08924c2eacef6b84a7204 (patch) | |
| tree | b128b705019448c44b84c0cc3e0cb65010cb5c71 | |
| parent | 2d2246cde1b3a290fea8605f5f53c37bfc892fe2 (diff) | |
| download | hsc-fb1f8d3a02908e2fe8c08924c2eacef6b84a7204.tar.zst | |
Support exponentiation
Haskell might make this overcomplicated, but it's a complicated problem
to solve, I suppose. Whatever. It works well.
| -rw-r--r-- | Expr.hs | 2 | ||||
| -rw-r--r-- | RealCalc.hs | 6 |
2 files changed, 8 insertions, 0 deletions
@@ -30,6 +30,7 @@ data Expr = Plus Expr Expr | Minus Expr Expr | Times Expr Expr | Div Expr Expr + | Pow Expr Expr | EInt Integer | EFloat Double | E @@ -66,6 +67,7 @@ prefix name fun = Prefix (fun <$ tokenS name) table :: [[Operator String u Data.Functor.Identity.Identity Expr]] table = [ [ prefix "log" Log, prefix "sin" Sin, prefix "cos" Cos, prefix "sqrt" Sqrt ] , [ prefix "-" Negate ] + , [ binary "^" Pow AssocLeft ] , [ binary "*" Times AssocLeft , binary "/" Div AssocLeft ] diff --git a/RealCalc.hs b/RealCalc.hs index 11eebe4..df2e06d 100644 --- a/RealCalc.hs +++ b/RealCalc.hs @@ -54,6 +54,12 @@ simplify (Expr.Times e1 e2) = genericCombine (*) (simplify e1) (simplify e2) simplify (Expr.Div e1 e2) = RFloat (rToDouble (simplify e1) / rToDouble (simplify e2)) +simplify (Expr.Pow e1 e2) = + case (simplify e1, simplify e2) of + (RInt a, RInt b) + | b >= 0 -> RInt (a ^ b) + (a, RInt b) -> RFloat (rToDouble a ^^ b) + (a, RFloat b) -> RFloat (rToDouble a ** b) simplify (Expr.EInt i) = RInt i simplify (Expr.EFloat d) = RFloat d simplify Expr.E = RFloat (exp 1) |
