[Haskell-cafe] Re: Proper round-trip HughesPJ/Parsec for Doubles?

Andy Gimblett haskell at gimbo.org.uk
Tue Feb 23 11:08:29 EST 2010


>> Short version: How can I pretty print and parse values of type Double
>> such that those operations are each other's inverse?
>
> Maybe you have more luck with show and read (without Parsec.Token).
>
> Your example:
> x = 9.91165677454629
>
> fails because the computation performed by the parser
> 9.0 + 0.91165677454629 yields 9.911656774546291

That seems to do the trick!  Below, for the record, the code I've come  
up with (I threw away the Either Integer Double part so it's a bit  
simpler, also).  I'm sure it can be improved, but this is passing all  
tests reliably, it seems.

Many thanks, Christian and Daniel, for your help!

Best,

-Andy

parseDouble :: Parser Double
parseDouble = try $ do (symbol toks) "-"
                        n <- floater
                        return $ negate n
               <|> floater
   where toks = makeTokenParser emptyDef

-- This could definitely be improved, but it's working. :-)
floater :: Parser Double
floater = do w <- many1 digit
              char '.'
              f <- many1 digit
              e <- optionMaybe $ do char 'e' -- Optional exponent part
                                    n <- option "" (char '-' >> return  
"-") -- Optional negation in exponent
                                    m <- many1 digit
                                    return $ n ++ m
              case e of Nothing -> return $ read $ w ++ "." ++ f
                        Just e' -> return $ read $ w ++ "." ++ f ++  
"e" ++ e'

--
Andy Gimblett
http://gimbo.org.uk/



More information about the Haskell-Cafe mailing list