]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Print complex numbers slightly more nicely.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 10 Oct 2012 18:26:50 +0000 (19:26 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Wed, 10 Oct 2012 18:28:50 +0000 (19:28 +0100)
OFC/SecondLevel.hs

index a40c7f2a39dba70b9c41776b2b8676b0d320eb1c..9c16fb95a68e4f9406da50038ad6cdff53c42394 100644 (file)
@@ -73,7 +73,7 @@ instance PrettyPrintable Expression where
     Integrate e -> functionToDoc "integrate" [toDoc e]
     ConstReal r -> double r
     ConstInteger i -> integer i
-    ConstComplex c -> parens $ text ((show $ realPart c) ++ " + i*" ++ (show $ imagPart c) ++ ")")
+    ConstComplex c -> parens $ text $ showComplex c
     LatticeComponent i -> text $ "lattice[" ++ i ++ "]"
     Negate e -> hcat [text "(", toDoc e, text ")"]
     Sum e i -> functionToDoc "sum" [toDoc e, text i]
@@ -85,6 +85,10 @@ instance PrettyPrintable Expression where
     where
     binaryToDoc op lhs rhs = parens $ hcat [toDoc lhs, text op, toDoc rhs]
     functionToDoc name params = text name <> (parens $ hcat (intersperse (text ", ") params))
+    showComplex c
+      | imagPart c == 0 = show $ realPart c
+      | realPart c == 0 = (show $ imagPart c) ++ "i"
+      | otherwise = concat [show $ realPart c, "+", show $ imagPart c, "i"]
 
 data Assignment =
  Assign Expression Expression