Understanding the number of digits given by RealDigits

• To: mathgroup at smc.vnet.net
• Subject: [mg93141] Understanding the number of digits given by RealDigits
• From: Alain Cochard <alain at geophysik.uni-muenchen.de>
• Date: Tue, 28 Oct 2008 04:55:04 -0500 (EST)
• Organization: Universites Paris VI/Paris VII - France

```The documentation says that

RealDigits[x, b, len] gives a list of len digits.

and that

If len is larger than Log[10, b]Precision[x], then remaining
digits are filled in as Indeterminate.

Now, for a machine number, say 0.1:

N[Log[10, 2]Precision[0.1]]

Out[468]= 4.80281

while

N[Log[10, 2]^-1 Precision[0.1]]

Out[470]= 53.

which makes much more sense to me.  So is there a typo in the
documentation, or am missing something obvious?  (Actually, it says
later, about the binary representation of a machine number, that
"MachinePrecision is equal to the number of bits times log_(10)(2)".)

Anyway, it does not always give the same number of digits:

In[471]:= x=.1 ; y=.125;

In[472]:= lx=RealDigits[x,2]

Out[472]= {{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,

>     0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,

>     0, 0, 1, 1, 0, 1, 0}, -3}

In[473]:= Length[lx[[1]]]

Out[473]= 53

In[474]:= flx=RealDigits[x,2,60]

Out[474]= {{1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,

>     0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,

>     0, 0, 1, 1, 0, 1, 0, Indeterminate, Indeterminate, Indeterminate,

>     Indeterminate, Indeterminate, Indeterminate, Indeterminate}, -3}

In[475]:= Length[Cases[flx[[1]],Except[Indeterminate]]]

Out[475]= 53

So far, so good, but:

In[476]:= ly=RealDigits[y,2]

Out[476]= {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

>     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

>     0, 0, 0, 0, 0, 0, 0}, -2}

In[477]:= Length[ly[[1]]]

Out[477]= 53

In[478]:= fly=RealDigits[y,2,60]

Out[478]= {{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

>     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

>     0, 0, 0, 0, 0, 0, 0, 0, Indeterminate, Indeterminate, Indeterminate,

>     Indeterminate, Indeterminate, Indeterminate}, -2}

In[479]:= Length[Cases[fly[[1]],Except[Indeterminate]]]

Out[479]= 54

So there is one extra digit in this case whereas:

In[482]:= {Precision[x],Precision[y]}

Out[482]= {MachinePrecision, MachinePrecision}

>From the few tests I did, it seems that this happens when the number
has an exact binary expansion.  But why is it doing that, especially
that in this case the extra digit is always zero?  Is it related to
the so-called "guard digit" needed in the IEEE754 floating point
computations?  Does RealDigits provide us with the binary
representation which Mathematica is using internally?

So those were preliminary questions, that I asked myself while trying
to understand how Mathematica works.  The real question is: Is there way to
determine the *full* binary representation of a machine number?  This
means that if I stop a computation at some point, determine that
binary representation for some numbers, and resume the computation
later on, feeding Mathematica with that representation, it should give exactly