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 RealDigits to 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?

  • $\begingroup$ If the default format bothers you, you could maybe consider applying RealDigits[] on the FractionalPart[] of your number, and IntegerDigits[] to the IntegerPart[]. $\endgroup$ – J. M. is computer-less Feb 23 '16 at 12:12
  • $\begingroup$ @J.M. RealDigits@FractionalPart[1.019] still has negative second part. So ArrayPad is inevitable. $\endgroup$ – Kuba Feb 23 '16 at 12:17
  • $\begingroup$ @J.M. Maybe I should rather ask, what is the form of RealDigits' output useful for? It was never for me, I always had to built something ugly on that. $\endgroup$ – Kuba Feb 23 '16 at 12:19
  • $\begingroup$ Yes, but that is now because the fractional part is a number in $(0,1)$, so the second part of the output is a nonpositive integer. $\endgroup$ – J. M. is computer-less Feb 23 '16 at 12:19
  • 1
    $\begingroup$ You might consider, as another example, the output of RealDigits[1.234*^-5, 10, Automatic, -1].But, going back to the basic use of RealDigits[]: I suppose the phrasing of the docs could be better, but I had always interpreted the output of RealDigits[] this way: x == FromDigits[#1] b^(#2 - Length[#1]) & @@ RealDigits[x, b] where b is by default 10. $\endgroup$ – J. M. is computer-less Feb 23 '16 at 12:30

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.