# Why does this rounding happen at machine precision?

Clearly, dividing by two in a binary number shifts the decimal point like this:

BaseForm[InputForm, 2]
BaseForm[InputForm[7/2 // N], 2]


It gives: 2^^111 and 2^^11.1

Now, why does this not give a similar result?

BaseForm[InputForm[
1 + (1*$MachineEpsilon + 2*$MachineEpsilon + 4*$MachineEpsilon)/ 1], 2] BaseForm[InputForm[ 1 + (1*$MachineEpsilon + 2*$MachineEpsilon + 4*$MachineEpsilon)/2],
2]


The only difference here is that the second line has a division by $2$.

2^^1.0000000000000000000000000000000000000000000000000111

and

2^^1.00000000000000000000000000000000000000000000000001

Can anyone explain this last result? I would expect 2^^1.000000000000000000000000000000000000000000000000011

• Explain in what way? It looks like the number is rounded up, but that was already covered in your prior question I think? What additional information do you seek? Dec 5, 2016 at 11:41

I think we can find out what's happening by comparing (real digits of) exact values with (real digits of) rounded machine precision values:

Clear[roundingDirection, numberFromRealBits]
numberFromRealBits[bits_] := bits.(2^-Range[Length[bits]])
roundingDirection[exactBits_, machineBits_] := Module[{e, m},
e = numberFromRealBits[exactBits];
m = numberFromRealBits[machineBits];
If[e == m, "Exact", If[e > m, "Down", "Up"]]]

Grid[Table[
value = 1 + i*2^-53;
machine = N[value];
{fractionalExact, integer} = RealDigits[value, 2, 55];
{fractionalMachine, integer} = RealDigits[machine, 2];
{1 + eps*i/2, Column[{"Exact value", "Machine precision"}],
Grid[{fractionalExact[[30 ;;]], fractionalMachine[[30 ;;]]}],
roundingDirection[fractionalExact, fractionalMachine]}, {i, 0,
11}], Frame -> All] So it seems like if the least significant bit is 0, the value is rounded down, if it's 1, it's rounded up, i.e. round to even

• Which is what should happen, because the underlying processing uses standard machine arithmetic (where round-to-even is the prevalent mode). Dec 5, 2016 at 16:35
• @DanielLichtblau: I thought so, but couldn't find a reference. The closest thing I've found was the IEEE 754 spec, which contains several "rounding direction attributes" that the user can apparently choose from. If you have an actual reference that would probably be better than my "empirical" answer. Dec 5, 2016 at 16:54
• Unfortunately I do not have a reference. But I believe round-to-even is the common default and also I do not think we change the default when building the Mathematica kernel. I'll ask around though. Dec 5, 2016 at 17:09
• I was directed to this link to indicate what rounding will typically be baked in. Dec 5, 2016 at 18:49