Numerical vs Symbolic contour integration get different result

When I test the symbolic integration along a contour, I want to let the answer to be the residue of the integrand. Here is the code:

Integrate[Sin[x]/(x-(1+I)),{x,1+I-0.01,1+I-0.01I,1+I+0.01,1+I+0.01I,1+I-0.01}]


It gets the result:

1.0365*10^-11 + 5.03064*10^-12 I


Obviously it's wrong, but when I use numerical integration:

NIntegrate[Sin[x]/(x-(1+I)),{x,1+I-0.01,1+I-0.01I,1+I+0.01,1+I+0.01I,1+I-0.01}]


Then the result is :

-3.9896 + 8.15845 I


which is right, because:

N[2 Pi I Residue[Sin[x]/(x - (1 + I)), {x, 1 + I}]]


which the result is:

-3.9896 + 8.15845 I


Here is the problem: 1. Can this be repeated in other version of Mathematica? 2. Is this a bug?

• It's not a bug. You should not feed machine precision numbers to Integrate. This will happen AFAIK in all versions, and the solution to use exact numbers (1/100 instead of 0.01) will also work and will result in a symbolic solution coherent with what NIntegrate returns. (related: mathematica.stackexchange.com/questions/38457/… ) Apr 23, 2018 at 5:03
• @Peltio: You are not right: in the case under consideration Integrate should switch to NIntegrate and perform the right numeric answer. Compare with Integrate[x^2,{x,0,1.0}]. Apr 23, 2018 at 5:06
• Is this a new functionality introduced in which version? Apr 23, 2018 at 5:07
• @Peltio: Integrate[x^2,{x,0,1.0}] performs 0.333333 at least since version 7.0. The same with Integrate[x^2 + y, {x, 0, 1.0}, {y, 0, 1}] and many others. The question you refer to is another cup of tea: pay your attention to "Try not to supply machine numbers to integrals over infinite domains ". Apr 23, 2018 at 5:14
• @user64494 well, Integrate[x^2,{x,0,1.0}] giving 0.333333 does not contradict my claim. Most integrals do come out right even with machine precision numbers. This contour integral though, does not, even if the contour is finite (I guess it's it being so darn close to the singularity the problem, here). Glad to know they fixed something from version 7 on. They evidently did not fix everything :-) Apr 23, 2018 at 7:34

There is a workaround in version 11.3:

j = Integrate[Sin[x]/(x - (1 + I)), {x, 1 + I - 1/100, 1 + I - 1/100*I,
1 + I + 1/100, 1 + I + 1/100*I, 1 + I - 1/100}]//FullSimplify


2 I [Pi] Sin[1 + I]

N[j]


-3.9896 + 8.15845 I

• This workaround no longer needed in 13! Just like FullSimplify. Still there are other integrals that require this hack, like Integrate[1/(Exp[I*s] (0.45 Cos[s]+1)), {s, 0, 2 Pi}] (saw it in official youtube presentation, LOL) Jan 5, 2022 at 19:19