3
$\begingroup$

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

BaseForm[InputForm[7], 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

$\endgroup$
1
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Dec 5, 2016 at 11:41

1 Answer 1

8
$\begingroup$

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]

enter image description here

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

$\endgroup$
4
  • 3
    $\begingroup$ Which is what should happen, because the underlying processing uses standard machine arithmetic (where round-to-even is the prevalent mode). $\endgroup$ Dec 5, 2016 at 16:35
  • $\begingroup$ @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. $\endgroup$ Dec 5, 2016 at 16:54
  • 1
    $\begingroup$ 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. $\endgroup$ Dec 5, 2016 at 17:09
  • $\begingroup$ I was directed to this link to indicate what rounding will typically be baked in. $\endgroup$ Dec 5, 2016 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.