My guess is that the wrong answer is the one given by N[expr]
and not N[expr,3]
.
My mind is simple and I cannot manage big numbers, so, let's give 'em names:
aN = 95881665812878; bN = 120000000000000; cN = 121576521638975;
dN = 321097753837557;
eN = Log[1/3 (1 + (19 - 3 Sqrt[33])^(1/3) + (19 + 3 Sqrt[33])^(1/3))];
fN = 321097753837557;
gN = Log[(-1 + 1/3 (1 + (
19 - 3 Sqrt[33])^(1/3) + (19 + 3 Sqrt[33])^(1/3)))/(-6 + 4/3 (
1 + (19 - 3 Sqrt[33])^(1/3) + (19 + 3 Sqrt[33])^(1/3)))];
Let's define a substitution rule that will allow us to use the simple names a ... g
exactRule = {a -> aN, b -> bN, c -> cN, d -> dN, e -> eN, f -> fN, g -> gN};
Now, your expression is
expr = a - b (c - (d e)/Log[5]) + (f g)/Log[5]
where b c
and b d e/Log[5]
are veeeeeery similar one to the other, and this give rise to a cancellation error.
Please note that you can rewrite your expression as
expr1 = a + (b d e + f g - b c Log[5])/Log[5]
This expression seems to be less prone to suffer from cancellation errors, probably because of the different order in which the operations are performed.
Now, the real question is: why is N[expr]
returning a wrong answer, when N[expr,3]
is giving a correct approximation?
Perhaps N[expr, nn]
is computed in high precision and then in nn
precision, while N[expr]
simply forces machine precision, which gets fooled by cancellation errors.
EDIT^2: Yes, I believe it's a cancellation error problem. I tried on paper three forms for the above expression and there always is a double cancellation between two quantities*10^14 and two quantities*10^28. If you substitute each value of a
, b
, c
,..., g
with the corresponding N[a,nn]
, you can see that you need to increase nn
to get consistent answers in all cases.
But if you compute the expression with exact values and then apply N[expr,nn]
you get the correct approximation with nn
precision.
EDIT^3 : To exemplify that, let's use the following accessory procedures (with a caveat: the special case with parameter 0
is introduced to simplify the code for producing the table of values, but it does not produce the effect of N[expr,0]
):
argN[expr_, rule_, n_] := expr /. (N[#, n] & /@ rule)
argN[expr_, rule_, 0] := expr /. (N[#] & /@ rule)
exprN[expr_, rule_, 0] := N[expr /. rule]
exprN[expr_, rule_, n_] := N[expr /. rule, n]
argN[expr, rule, n]
evaluates expression expr
by substituting the parameters specified by the substitution rule
with precision n
, while exprN[expr, rule, n]
returns the numeric value of expr with precision n
after it has been computed according to the substitution rule (the special case n=0
means it will use N[]
without parameters).
Some code to create the correspondent text is here:
argNtext[n_] := If[n == 0, "expr[N[vars]]", StringJoin["expr[N[vars,", ToString[n], "]]"] ]
exprNtext[n_] := If[n == 0, "N[expr]", StringJoin["N[expr,", ToString[n], "]"] ]
And now, what follows will show how different the result is between the different cases, that is expr[N[vars]]
, N[expr[vars]]
and expr[N[vars,n]]
, N[expr[vars],n]
for different values of n
when the parameters are replaced with the exact numbers specified by exactRule
.
Table[{argNtext[k], argN[expr, exactRule, k],
exprNtext[k], exprN[expr, exactRule, k]},
{k, 0, 42, 3}] // TableForm
For Mathematica novices, try to figure out what you will get before evaluating the above table.