Parsing floating point numbers

Hi all, What is your preferred method of parsing floating point numbers (from String to Float/Double)? Parsec it seems only does positive floats out of the box and PolyParse requires the float to be on scientific form (exponential). While I've worked around these shortcomings in the past I feel that I am reinventing the wheel as surely I am not the only to run into these limitations. How do you parse your floats? Can you recommend a parsing library that handles them solidly? (For my current needs the formats accepted by "read" are sufficient, but I want reasonable error handling (Maybe or Either) instead of an exception on bad inputs.) Thanks, Bjorn Buckwalter

On Sun, 2009-03-08 at 19:34 -0500, Bjorn Buckwalter wrote:
Hi all,
What is your preferred method of parsing floating point numbers (from String to Float/Double)? Parsec it seems only does positive floats out of the box and PolyParse requires the float to be on scientific form (exponential). While I've worked around these shortcomings in the past I feel that I am reinventing the wheel as surely I am not the only to run into these limitations. How do you parse your floats? Can you recommend a parsing library that handles them solidly?
(For my current needs the formats accepted by "read" are sufficient, but I want reasonable error handling (Maybe or Either) instead of an exception on bad inputs.)
fmap fst . listToMaybe . reads jcc

On Sun, Mar 8, 2009 at 9:34 PM, Bjorn Buckwalter
(For my current needs the formats accepted by "read" are sufficient, but I want reasonable error handling (Maybe or Either) instead of an exception on bad inputs.)
Why not readM :: (Monad m, Read a) => String -> m a readM str = case [x | (x,"") <- readsPrec 0 str] of [x] -> return x _ -> fail "readM: failed" Also, I remember seeing this function exported by some module, but I don't remember where. HTH, -- Felipe.

On Sun, 8 Mar 2009, Felipe Lessa wrote:
On Sun, Mar 8, 2009 at 9:34 PM, Bjorn Buckwalter
wrote: (For my current needs the formats accepted by "read" are sufficient, but I want reasonable error handling (Maybe or Either) instead of an exception on bad inputs.)
Why not
readM :: (Monad m, Read a) => String -> m a readM str = case [x | (x,"") <- readsPrec 0 str] of [x] -> return x _ -> fail "readM: failed"
Also, I remember seeing this function exported by some module, but I don't remember where.
http://www.haskell.org/pipermail/libraries/2008-February/009202.html http://www.haskell.org/pipermail/haskell-cafe/2008-June/044590.html Although maybeRead was proposed, I cannot find it: http://hackage.haskell.org/packages/archive/base/4.0.0.0/doc/html/Text-Read.... (Btw. I find it really great, that now the 'base' package documentation can be found on Hackage!)

Although maybeRead was proposed, I cannot find it:
here's a replacement... http://hackage.haskell.org/packages/archive/safe/0.2/doc/html/Safe.html#v%3A... Greetings, Daniel

If you're working with ByteStrings,
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing
might help.
Cheers,
Dan
On Sun, Mar 8, 2009 at 8:34 PM, Bjorn Buckwalter
Hi all,
What is your preferred method of parsing floating point numbers (from String to Float/Double)? Parsec it seems only does positive floats out of the box and PolyParse requires the float to be on scientific form (exponential). While I've worked around these shortcomings in the past I feel that I am reinventing the wheel as surely I am not the only to run into these limitations. How do you parse your floats? Can you recommend a parsing library that handles them solidly?
(For my current needs the formats accepted by "read" are sufficient, but I want reasonable error handling (Maybe or Either) instead of an exception on bad inputs.)
Thanks, Bjorn Buckwalter _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Bjorn Buckwalter
What is your preferred method of parsing floating point numbers (from String to Float/Double)? Parsec it seems only does positive floats out of the box and PolyParse requires the float to be on scientific form (exponential).
Thanks for the bug report. Polyparse is now fixed. New version (polyparse-1.3) available on Hackage. And my apologies. This was purely an oversight - the real-world use-case at the time I wrote the code for Text.Parse.parseFloat was entirely dependent on scientific notation, and I neglected the more usual case. For those looking for a template to implement textual float-parsing that is faster (or has better error messages) than the H'98 Read class, feel free to translate this code into your own parser combinator library of choice: http://www.cs.york.ac.uk/fp/polyparse/haddock/src/Text/Parse.html#parseFloat (Note that the actual instances of the Parse class for Float and Double use "parseSigned parseFloat" to deal with non-positive numbers.) Regards, Malcolm
participants (7)
-
Bjorn Buckwalter
-
Daniel Peebles
-
Daniel Schüssler
-
Felipe Lessa
-
Henning Thielemann
-
Jonathan Cast
-
Malcolm Wallace