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

best way to go from RealFloat a => a -> Scientific? #2

Open
chessai opened this issue Nov 7, 2019 · 4 comments
Open

best way to go from RealFloat a => a -> Scientific? #2

chessai opened this issue Nov 7, 2019 · 4 comments

Comments

@chessai
Copy link
Member

chessai commented Nov 7, 2019

e.g. Double, Float

@andrewthad
Copy link
Member

At the moment, I'm actually more interested in the other direction: Scientific -> Double, etc. Simply because at the moment, this is only used for decoding JSON, not for encoding anything. Conversion to and from Double is a bit of a pain.

  • From Double. The implementation of doubleDec from small-bytearray-builder is a good starting point. That function writes out the decimal digits to a buffer. The big difference is that here, we only need to fold into an accumulator instead of writing to a buffer. But, the accumulator has to switch from Int to Integer once it gets too big. Tricky. But the nice thing is that we only have to think about things to the right of the decimal.
  • To Double. This is considerably easier. We just normalize, do some bounds checks and then fold the digits into an accumulator. There are some tricks that could be used to help avoid loss of precision. (For pathological users who write 999999999999999999999999999999999999999999999999999999e-40.) However, I'm not too concerned about loss of precision with pathological input since if you're using Double, you've already accepted that your numbers just won't be precise.

I'm less concerned about supporting Float. The code is probably similar, but Float is really uncommon in Haskell code.

@chessai
Copy link
Member Author

chessai commented Nov 7, 2019

Ok

@mightybyte
Copy link

To Double. This is considerably easier.

Is there any public code for this? I only see functionality for consuming Scientific as integral types.

@andrewthad
Copy link
Member

I never ended up needing it, so it never got done. I'd take a PR for it though.

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

No branches or pull requests

3 participants