haskell / bytestring

An efficient compact, immutable byte string type (both strict and lazy) suitable for binary or 8-bit character data.
http://hackage.haskell.org/package/bytestring
Other
291 stars 141 forks source link

Data.ByteString.Char8.readDouble? #598

Open meooow25 opened 1 year ago

meooow25 commented 1 year ago

We have readInt and readInteger but no readDouble. Of course, the implementation will be more complex compared to readInt, but I would like to see it unless there is some good reason to not have it.

Bodigrim commented 1 year ago

This is currently covered by Data.ByteString.Lex.Fractional, but I don't see fundamental reasons why not have it in bytestring proper.

meooow25 commented 1 year ago

Checking it out, Data.ByteString.Lex.Fractional provides no way to parse "NaN" and "Infinity", which are supported by read. It also appears to sacrifice precision (for speed?) but makes no mention of it in the docs.

ghci> :m + Data.Maybe Data.ByteString.Lex.Fractional Data.ByteString.Char8
ghci> readbslex :: String -> Double; readbslex = fst . fromJust . readExponential . pack
ghci> readbslex "12.3e-2" == readbslex "123e-3"
False
ghci> readDbl :: String -> Double; readDbl = read
ghci> readDbl "12.3e-2" == readDbl "123e-3"
True

If we have this function in bytestring I feel it would be best to match read's behavior (and not just use bytestring-lexing's implementation).

Bodigrim commented 1 year ago

Makes sense to me. @clyring what do you think?

clyring commented 1 year ago

Sorry for the slow response. I would be happy to add these functions with correct rounding behavior. I'm not too fussed about matching read's behavior in other respects.