Stop Mathematica from rounding the value

There is a system option:

SystemOptions["SymbolicSumThreshold"]
(*  {"SymbolicSumThreshold" -> 1000000}  *)

Above a sum of length 10^6, Sum will try a symbolic method. I'm not sure it's worth trying to explain why a symbolic method will fail on a random summand.

Some alternatives:

xn[n_] := With[{opts = SystemOptions["SymbolicSumThreshold"]},
   Internal`WithLocalSettings[
    SetSystemOptions[{"SymbolicSumThreshold" -> n}],
    1/n Sum[x, {j, 1, n}],
    SetSystemOptions[opts]
    ]
   ];

xn[n_] := 1/n Sum[x, {j, 1, n}, Method -> "Procedural"];

xn[n_] := 1/n Total@RandomInteger[{1, 6}, n];   (* bypasses  x  *)

The last one will be fastest, probably the fastest possible.


Sum is, at its core, a symbolic method intended to deduce sums of large, perhaps infinite series. To actually add up numbers, use Total.

xn[n_] := 1/n Total[Table[x, {j, 1, n}]]
xn[10^8]
349988177/100000000

A better way to define xn is

xn[n_Integer /; Positive[n]] := Mean[RandomInteger[6, n]]

This definition requires n to be a positive integer, which is a reasonable constraint for this problem.

Let's look at some evaluations. I useSeedRandom to get reproducible results.

SeedRandom[42]; Table[xn[i], {i, 0, 5}]

{xn[0], 3, 5/2, 4/3, 9/4, 13/5}

Note that zero is not accepted and that the results are all exact numbers.

Now lets look at n = 10^7.

SeedRandom[42]; x10to7 = xn[10^7]

5999573/2000000

x10to7 // N  // InputForm

2.9997865

In this case, the machine value contains an accurate representation of the exact number.

2.9997865`100 - x10to7

0.*10^-100