8
$\begingroup$

Out of curiousity, please consider following expression:

Sum[(-1)^(n + 1)/n, {n, 1, 100000}]

When evaluated using Wolfram Alpha:

Result: 0.693142 - 1.3494*10^-16 I , complex number

When evaluated using Mathematica v.10.2 (to 6 decimal places):

Result: 0.693142, rational number

Why Wolfram Alpha evaluates expression stated above to a complex number?

$\endgroup$
3
  • 2
    $\begingroup$ Pure speculation: Maybe WA evaluates a^b as Exp[Log[a]*b]. That would give complex numbers in intermediate calculations, where the imaginary part doesn't perfectly cancel out due to numerical inaccuracy. Compare: Sum[Exp[Log[(-1.)]*(n + 1)]/n, {n, 1, 100000}] in Mathematica gives a complex result, too (though not the same...) $\endgroup$ Commented Apr 7, 2016 at 16:11
  • $\begingroup$ After a discussion with my mentor, he suggested that it must be a rounding error with which I tend to agree. If WA evaluates it as a^b it would be rather inefficient although generic at the same time. It's rather curious to say the least. $\endgroup$ Commented Apr 7, 2016 at 16:44
  • $\begingroup$ Sum[(-1)^(n + 1)/n, {n, 1, m}] /. m -> 100000. or (Sum[(-1)^(n + 1)/n, {n, 1, m}] /. m -> 100000) // N or NSum[(-1)^(n + 1)/n, {n, 1, 1000000}] // Chop $\endgroup$
    – Bob Hanlon
    Commented Apr 7, 2016 at 23:20

1 Answer 1

10
$\begingroup$

For large enough bounds, NSum is used. Compare timings:

NSum[(-1)^(n + 1)/n, {n, 1, 100000}] // AbsoluteTiming
{0.004744, 0.693142 - 1.3494*10^-16 I}
N[Sum[(-1)^(n + 1)/n, {n, 1, 100000}]] // AbsoluteTiming
{1.84727, 0.693142}
$\endgroup$

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.