MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: RealDigits

  • To: mathgroup at christensen.cybernetics.net
  • Subject: [mg1791] Re: [mg1741] RealDigits
  • From: Richard Mercer <richard at seuss.math.wright.edu>
  • Date: Sun, 30 Jul 1995 21:40:22 -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}?

Good Question! I don't think there is one built in, so here's my entry.

RealFromDigits[item_]:=
N[Plus @@ MapIndexed[(#1*10^-First[#2])&,#1]*10^#2,Length[#1]]& @@ item;

In[2]:=
RealFromDigits[{{3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3}, 1}]
Out[2]=
3.141592653589793

This is elegant because
(1) It's short.
(2) It's unreadable.
(3) It uses the MapIndexed command, which I don't think I've ever used before.
    Everybody should use this command at least once in their lifetime,
    but probably not more than twice.

Richard Mercer

P.S. Who is giving you real numbers in that format?!?


  • Prev by Date: Re: Exponetial Fit
  • Next by Date: Re: RealDigits
  • Previous by thread: Re: RealDigits
  • Next by thread: Re: RealDigits