Re: ReadDigits
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1756] Re: ReadDigits
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Wed, 26 Jul 1995 00:48:08 -0400
- Organization: University of Colorado, Boulder
In article <3ui5a6$231 at news0.cybernetics.net>,
Samuel H. Cox <insshc at gsusgi2.gsu.edu> wrote:
>The function RealDigits[x, b] returns a list of two items. For example,
>
>RealDigits[Pi //N]
>{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}, 1}
>
>What is the inverse of this function? That is, given the base b, a list
>{...} of base b digits, and an interger n, how do we elegantly obtain x for
>which RealDigits[x, b] = {{...},n}?
Here's one:
In[70]:=
unRealDigits[{mantissa_, exponent_}, radix_:10] :=
With[{r = N[radix]},
Fold[r #1 + #2 &, 0, mantissa] /
r ^ (Length[mantissa]-exponent)
]
In[71]:=
RealDigits[N[Pi]]
Out[71]=
{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}, 1}
In[72]:=
unRealDigits[%]
Out[72]=
3.14159
In[73]:=
RealDigits[N[Pi], 8]
Out[73]=
{{3, 1, 1, 0, 3, 7, 5, 5, 2, 4, 2, 1, 0, 2, 6, 4, 3, 0}, 1}
In[74]:=
unRealDigits[%, 8]
Out[74]=
3.14159
In[75]:=
% == %%%
Out[75]=
True
Dave Wagner
Principia Consulting
(303) 786-8371
dbwagner at princon.com
http://www.princon.com/princon