0
$\begingroup$

I am trying to evaluate an expression :

(-((I (k2 - k1) (-1 + E^(2 I (k2 - k1) \[Pi])))/(-1 + (k2 - 
      k1)^2))) /. {k1 -> 2, k2 -> 7}

This gives output as 0. exactly. Then I tried this :

N[ (-((I (k2 - k1) (-1 + E^(2 I (k2 - k1) \[Pi])))/(-1 + (k2 - 
       k1)^2)))] /. {k1 -> 2, k2 -> 7}

This gives output as -2.55135*10^-16 + 0. I. Then I read up on the documentation of N[expr,n] and find that it can evaluate expr to n precision and try that.

N[ (-((I (k2 - k1) (-1 + E^(2 I (k2 - k1) \[Pi])))/(-1 + (k2 - 
       k1)^2))), 2] /. {k1 -> 2, k2 -> 7}

This gives output as 0.*10^-1 + 0.*10^-2 I. Now, I try this for different values of precision n and conclude that the output is zero upto the accuracy provided by precision.

But why does the usage of N[..] without any precision give me non zero result? It is of the order of 10^-16 so I thought that maybe MachinePrecision is set to 16 but no, trying N[...,16] results in exactly zero again.

Can someone please shed light on this so that I can trust the output given by N[] ?

$\endgroup$
  • 2
    $\begingroup$ It's roundoff error. Either use Chop[] on a machine precision evaluation (Chop[N[expr]]) or use arbitrary precision exclusively (N[expr, prec]). $\endgroup$ – J. M.'s technical difficulties Apr 29 at 23:42
  • $\begingroup$ @J.M.'stechnicaldifficulties So, in short, we should never use N[ ] without specifying precision even though Mathematica doesn't show any errors? $\endgroup$ – Nitin Apr 30 at 16:51
  • $\begingroup$ "Never" is too strong a word. By default, if the second argument of N[] is unspecified, it is set to MachinePrecision. $\endgroup$ – J. M.'s technical difficulties May 2 at 11:31
  • $\begingroup$ Got it. Thanks :) $\endgroup$ – Nitin May 2 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.