Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce read decoder and add note about rational decoder for Double #29

Closed
chshersh opened this issue Nov 30, 2018 · 3 comments · Fixed by #31
Closed

Introduce read decoder and add note about rational decoder for Double #29

chshersh opened this issue Nov 30, 2018 · 3 comments · Fixed by #31

Comments

@chshersh
Copy link

It was proposed to add rational decoder in #22 to improve accuracy of decoding Double values. Unfortunately, this decoder also doesn't work. There's 6 years old issue in text library regarding rational decoder:

I was quite surprised to find this issue in text library because I was assuming that rational decoder should solve all problems with decoding Double. But my hedgehog property-based tests fail when I use rational decoder. The Encoder works properly though 👍

-           1.0000000000034547e-2
+           1.0000000000034549e-2

As a dirty and quick solution I'm using Read instance for Double to parse values and it works without any problems 👍 So I propose to add read decoder to sv library and add note to rational and double decoders about their accuracy.

This is the decoder (initially implemented by @vrom911, we're using it for Double and UTCTime at the moment and it works like a charm 😊):

readDecoder :: forall a . (Read a, Typeable a) => Decode' ByteString a
readDecoder = contents >>== \c -> maybe
    (badDecode $ errorMsg c)
    pure
    (readMaybe @a $ decodeUtf8 c) 
  where
    errorMsg :: ByteString -> ByteString
    errorMsg c = "Expect '" <> typeName @a <> "' but couldn't parse: " <> c
@gwils
Copy link
Member

gwils commented Nov 30, 2018

Thanks for letting me know about this. I'd like to get something like that into the library. I'd like for the user to be able to make the error on their own, either using the Bytestring or not.

Off the top of my head, something like the following (possibly with more general types)

read :: Read a => Decode' Bytestring a
read' :: Read a => (ByteString -> DecodeError' ByteString) ->  Decode' a

I will get around to this eventually. I'd accept a pull request much sooner :) although I wouldn't want TypeApplications like you're using there since I'd like to keep supporting GHC 7.10 for the time being.

@gwils
Copy link
Member

gwils commented Jan 2, 2019

@gwils
Copy link
Member

gwils commented Jan 3, 2019

I'm playing around with encoding and liking double-conversion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants