String to Double conversion in Haskell

I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: http://sequence.svcs.cs.pdx.edu/node/373 My questions are: a) is there a standard library implementation of string -> Double and float? b) Why is it that the ByteString only supports readInt? Is there a reason for it? Thanks, Daryoush Mehrtash

dmehrtash:
I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: [1]http://sequence.svcs.cs.pdx.edu/node/373
Use the bytstring-lexing library, http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing Which provides a copying and non-copying lexer for doubles, readDouble :: ByteString -> Maybe (Double, ByteString) unsafeReadDouble :: ByteString -> Maybe (Double, ByteString) -- Don

On 24 Aug 2008, at 23:23, Don Stewart wrote:
dmehrtash:
I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: [1]http://sequence.svcs.cs.pdx.edu/node/373
Use the bytstring-lexing library,
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing
Which provides a copying and non-copying lexer for doubles,
readDouble :: ByteString -> Maybe (Double, ByteString) unsafeReadDouble :: ByteString -> Maybe (Double, ByteString)
Incidentally, is there any reason we can't have this for Lazy BSes? Bob

I am curious to understand the logic, the "Haskell Think", here. Why is it
that the byteString only supports conversion to int.
daryoush
On Sun, Aug 24, 2008 at 2:23 PM, Don Stewart
dmehrtash:
I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: [1]http://sequence.svcs.cs.pdx.edu/node/373
Use the bytstring-lexing library,
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing
Which provides a copying and non-copying lexer for doubles,
readDouble :: ByteString -> Maybe (Double, ByteString) unsafeReadDouble :: ByteString -> Maybe (Double, ByteString)
-- Don

We wrote a readInt a long time ago, because we needed it. It turns out that really though, there's a large class of functions for parsing bytestrings should come with, so I've started adding those to the bytestring-lexing package. In hindisight ByteString.readInt should have been in a separate package. -- Don dmehrtash:
I am curious to understand the logic, the "Haskell Think", here. Why is it that the byteString only supports conversion to int.
daryoush On Sun, Aug 24, 2008 at 2:23 PM, Don Stewart <[1]dons@galois.com> wrote:
dmehrtash: > I am trying to convert a string to a float. It seems that Data.ByteString > library only supports readInt. After some googling I came accross a > possibloe implementation: [1][2]http://sequence.svcs.cs.pdx.edu/node/373 >
Use the bytstring-lexing library,
[3]http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing
Which provides a copying and non-copying lexer for doubles,
readDouble :: ByteString -> Maybe (Double, ByteString) unsafeReadDouble :: ByteString -> Maybe (Double, ByteString) -- Don
References
Visible links 1. mailto:dons@galois.com 2. http://sequence.svcs.cs.pdx.edu/node/373 3. http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing

Because normally, Prelude.read covers this. Don's link is the most
efficient, but you can also do
(read . ByteString.unpack $ bytestring) :: Double
to get a Double from a printed representation of most numbers.
2008/8/24 Daryoush Mehrtash
I am curious to understand the logic, the "Haskell Think", here. Why is it that the byteString only supports conversion to int.
daryoush On Sun, Aug 24, 2008 at 2:23 PM, Don Stewart
wrote: dmehrtash:
I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: [1]http://sequence.svcs.cs.pdx.edu/node/373
Use the bytstring-lexing library,
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/bytestring-lexing
Which provides a copying and non-copying lexer for doubles,
readDouble :: ByteString -> Maybe (Double, ByteString) unsafeReadDouble :: ByteString -> Maybe (Double, ByteString)
-- Don
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- I try to take things like a crow; war and chaos don't always ruin a picnic, they just mean you have to be careful what you swallow. -- Jessica Edwards

2008/8/24 Daryoush Mehrtash
I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: http://sequence.svcs.cs.pdx.edu/node/373
My questions are:
a) is there a standard library implementation of string -> Double and float? b) Why is it that the ByteString only supports readInt? Is there a reason for it?
Hi Daryoush, are you really looking for ByteString -> Float conversion, or just plain String -> Float? The latter is really simple, the function is called 'read' and is available in the Prelude: $ ghci GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> read "3.14" :: Float 3.14 /Bjorn

Bjorn,
I am initializing a list from a file. I am reading the lines from the file,
splitting them into bytestring and then converting them to float. Should I
be using String -> Float or ByteString -> Float?
thanks
Daryoush
On Tue, Aug 26, 2008 at 6:01 AM, Bjorn Bringert
2008/8/24 Daryoush Mehrtash
: I am trying to convert a string to a float. It seems that Data.ByteString library only supports readInt. After some googling I came accross a possibloe implementation: http://sequence.svcs.cs.pdx.edu/node/373
My questions are:
a) is there a standard library implementation of string -> Double and float? b) Why is it that the ByteString only supports readInt? Is there a reason for it?
Hi Daryoush,
are you really looking for ByteString -> Float conversion, or just plain String -> Float? The latter is really simple, the function is called 'read' and is available in the Prelude:
$ ghci GHCi, version 6.8.3: http://www.haskell.org/ghc/ :? for help Loading package base ... linking ... done. Prelude> read "3.14" :: Float 3.14
/Bjorn
/

dmehrtash:
Bjorn, I am initializing a list from a file. I am reading the lines from the file, splitting them into bytestring and then converting them to float. Should I be using String -> Float or ByteString -> Float?
I'd try reading the file entirely as a bytestring, then splitting out the Doubles, using ByteString -> Float. -- Don
participants (5)
-
Bjorn Bringert
-
Daryoush Mehrtash
-
Don Stewart
-
Jefferson Heard
-
Thomas Davie