Open chessai opened 4 years ago
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.
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.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.
Ok
To Double. This is considerably easier.
Is there any public code for this? I only see functionality for consuming Scientific
as integral types.
I never ended up needing it, so it never got done. I'd take a PR for it though.
e.g. Double, Float