So while thinking about Replacing numbers of which you only know certain digits I have faced, again, a problem that I find hard to extract from RealDigits' output something I want, what should be natural to do based on docs:
gives a list of the digits in the approximate real number x, together with the number of digits that are to the left of the decimal point.
Nice explanation till the point that for e.g. 0.01 you will get negative number of digits that are to the left... Which probably mean that Abs of that that is number of 0s inserted just on the right side.
The broad question is:
- maybe I'm expecting from
RealDigitsto much, then I would like to have better intuition with working with it. So I need better definition than the one from docs.
In case when that's unclear/too broad, here's the short question:
- Basing on the definition above, it should be easy, or at least there should be a neat functional way to extract from the real number, digits that are on the left and right side of the decimal point.
For example:
1.019 ==> {{1}, {0,1,9, 0, 0, ....}} (*or*) {{1}, {0, 1, 8, 9, 9, 9, ....}
(*since the latter is the representation of my input dictated by
binary system with finite precision*)
My approach
So, in linked topic I've done something what (almost) works but is ugly considering want to do something basic with built in function's result.
f = TakeDrop[
If[Negative[#2], ArrayPad[#1, {Abs[#2], 0}], #1],
Clip[#2, {0, \[Infinity]}]
] & @@ RealDigits[#] &
f /@ {1.01, 1.019}
{ {{1}, {0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}, {{1}, {0, 1, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9}} }
IMO, this should be possible to do with less effort. How?
RealDigits[]on theFractionalPart[]of your number, andIntegerDigits[]to theIntegerPart[]. – J. M.'s missing motivation Feb 23 '16 at 12:12RealDigits@FractionalPart[1.019]still has negative second part. SoArrayPadis inevitable. – Kuba Feb 23 '16 at 12:17RealDigits'output useful for? It was never for me, I always had to built something ugly on that. – Kuba Feb 23 '16 at 12:19RealDigits[1.234*^-5, 10, Automatic, -1].But, going back to the basic use ofRealDigits[]: I suppose the phrasing of the docs could be better, but I had always interpreted the output ofRealDigits[]this way:x == FromDigits[#1] b^(#2 - Length[#1]) & @@ RealDigits[x, b]wherebis by default 10. – J. M.'s missing motivation Feb 23 '16 at 12:30RealDigitsfails with simple examples like.019thenRealDigits[.019, 10, 10, -1]works well but is not general enough to take care about1.019. – Kuba Feb 27 '16 at 08:02