Re: RealDigits
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1790] Re: [mg1741] RealDigits
- From: John Fultz <jfultz>
- Date: Sun, 30 Jul 1995 21:39:51 -0400
> 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}?
>
> ==================================
> Samuel H. Cox
> insshc at gsusgi2.gsu.edu
> Department of Risk Management and Insurance
> Georgia State University
> PO Box 4036
> Atlanta, GA 30302-4036
> 404-651-4854 (o)
> 404-651-4219 (f)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Try this:
InverseRealDigits[{l_List, n_Integer}, b_Integer] :=
Fold[N[b * #1 + #2] &, First[l], Rest[l]] / b^(Length[l]-n)
Examples:
In[35]:= RealDigits[Pi//N]
Out[35]= {{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}, 1}
In[36]:= InverseRealDigits[%,10] // FullForm
Out[36]//FullForm= 3.141592653589793
In[37]:= RealDigits[E//N, 2]
Out[37]= {{1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0,
1, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0,
0, 1}, 2}
In[38]:= InverseRealDigits[%,2] // FullForm
Out[38]//FullForm= 2.718281828459045
John Fultz
Wolfram Research, Inc.