From 495bdf15c7deb8d1cad954216aaa6a88912726c9 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Wed, 10 Oct 2012 19:26:50 +0100 Subject: [PATCH] Print complex numbers slightly more nicely. --- OFC/SecondLevel.hs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/OFC/SecondLevel.hs b/OFC/SecondLevel.hs index a40c7f2..9c16fb9 100644 --- a/OFC/SecondLevel.hs +++ b/OFC/SecondLevel.hs @@ -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 -- 2.47.3