# Failed to use SetPrecision

I am calculating the below formula: $$\text{ER2}(\alpha,\text{K},\text{q})\text{:=}1+\sum _{m=0}^{K-1} \binom{K+\alpha }{m} \sum _{r=0}^m \frac{(-1)^r \binom{m}{r}}{\left(\frac{1}{q}\right)^{\alpha +K-m+r}-1};$$ with the code:

ER2[α_, K_, q_] := 1 + Sum[Binomial[α + K, m]
Sum[Binomial[m,
r]*(-1)^r /((1/q)^(α + K + r - m) - 1), {r, 0, m}]
, {m, 0, K - 1}];


when $$\alpha=0,K=268,q=0.1$$. The code produced a negative number -7306.51.
But I know the answer is positive! And Maple produce 3.196969869 as expected.
I think it's the matter of cancellation error, so I tried to use SetPrecision:

ER2PP = SetPrecision[ER2[α, K, q], 50];

Mathematica complaints:

NSum::nslim: Limit of summation -1.00000000000000000000000000000000000000000000000000000000000+K is not a number.

NSum::nslim: Limit of summation -1.00000000000000000000000000000000000000000000000000000000000+K is not a number.

NSum::nslim: Limit of summation -1.00000000000000000000000000000000000000000000000000000000000+K is not a number.

General::stop: Further output of NSum::nslim will be suppressed during this calculation.


• Try ER2[0, 268, 1/10] // N. Jan 21 '15 at 8:14
• The error is due to the real number 0.1. I tried <code>ER2[0, 268, 0.140] // N</code>, and it also produce an positive number. But I still can't understand why it's failed to use SetPrecision... Jan 21 '15 at 8:20
• Sorry for the code format. I mean the code is ER2[0, 268, 0.140] // N. Jan 21 '15 at 8:33
• SetPrecision[exp,p], according to the documentation, "yields a version of expr in which all numbers have been set to have precision p." Thus, it probably changes integer indices to real numbers, which may cause problems. What I find curious is that including SetPrecision inside the definition of ER2, either around the outer Sum or the inner Sum, produces no error messages but also computes the wrong answer to high precision. Jan 21 '15 at 11:37
• I noticed that the precision of ER2 itself is infinity. When substitute q with 0.1, the precision decreased. And maybe that's impossible to SetPrecision like such a summation due to the inner algorithms used by Mathematica. @bbgodfrey Jan 22 '15 at 1:28

The only InexactNumberQ is $$q=0.1$$ so you have two options:

1. replace it with 1/10 i.e. with Rationalize[0.1] i.e. SetPrecision[0.1, Infinity] if you want an exact answer and compute with exact numbers; you can later use N to get an approximation of the exact anser;

2. replace it with SetPrecision[0.1, 50] i.e. 0.150 if you want to do the computation with 50 digits arbitrary precision numbers.

There is no need to use SetPrecision on the whole expression and this can cause problems with other exact number (for example a side effect of using inexact numbers as bounds in Sum is that NSum is used actually).

There is also a problem in your expression SetPrecision[ER2[...], 50] because SetPrecision act once ER2 has already computed the (wrong) result, i.e. SetPrecision is not HoldFirst. You can use Unevaluated.

Clear[ER2]
ER2[α_, K_, q_] :=
1 + Sum[Binomial[α + K, m] Sum[
Binomial[m, r]*(-1)^r/((1/q)^(α + K + r - m) - 1), {r, 0,
m}], {m, 0, K - 1}];
ER2[0, 268, 1/10] // N
ER2[0, 268, SetPrecision[0.1, 50]]
SetPrecision[Unevaluated@ER2[0, 268, 0.1], 50]

3.19697
3.196969869247707935345524749
3.1969698692477079353455247


As you can see, placing SetPrecision in the right place pay off also in terms of precision of the result.

On 10.0 for Mac OS X x86 (64-bit) (December 4, 2014) and with the settings

α = 0; K = 268; q = 1/10;


all works fine:

ER2PP = SetPrecision[ER2[α, K, q], 50]


3.1969698692477078742102832950849543981411237482710

ER2[0, 268, 1/10] // Short


$$\frac{133756473090706713\langle\langle 21765\rangle\rangle 656008497136924720}{418385153946357022\langle\langle 21764\rangle\rangle 593232363264815721}$$

N[%]
`

3.19697