Behaviour of RealDigits expected or wrong?

Is this behavior expected or a bug?

RealDigits[10304.390456]

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 5, 9, 9, 9, 9, 9}, 5}

when I expected

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 6, 0, 0, 0, 0, 0}, 5}

17 significant figures doesn’t make a difference as I would have expected considering that $MachinePrecision->15.9546.

RealDigits[10304.390456000000]

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 5, 9, 9, 9, 9, 9}, 5}

This is far from a the limit of machine-precision number representation.

RealDigits[10304.390456000000 + 10^3 $MachineEpsilon]

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 5, 9, 9, 9, 9, 9}, 5}

18 significant figures does make a difference

RealDigits[10304.3904560000000]

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0}, 5}

=================

  

 

You implicitly changed a decimal fraction to binary, and then explicitly changed it back to decimal.
– John Doty
Oct 13 ’15 at 13:55

  

 

@JohnDoty wholdn’t that give me an error of only $MachineEpsilon i.e around 2.22045*10^-16? This is three orders of magnitude bigger!
– rhermans
Oct 13 ’15 at 13:57

4

 

Expect errors of around x*$MachineEpsilon, where x is the number in question.
– John Doty
Oct 13 ’15 at 14:01

  

 

OK, my bad then.
– rhermans
Oct 13 ’15 at 14:02

1

 

the change in the last case is because you supplied enough digits to force an extended precision representation. ByteCount[10304.390456000000]->16 … ByteCount[10304.3904560000000]-> 80
– george2079
Oct 13 ’15 at 18:18

=================

1 Answer
1

=================

I think the result you are seeing gives the real digits of Mathematica’s internal representation of your decimal input and is, therefore, not a bug.

Perhaps you will find the following an acceptable work-around for the behavior that is bothering you.

RealDigits[SetPrecision[10304.390456, Floor @ $MachinePrecision]]

{{1, 0, 3, 0, 4, 3, 9, 0, 4, 5, 6, 0, 0, 0, 0}, 5}

This should work for any machine precision real.