I have very strange problem involving $\operatorname{erf}$ functions:

f1[r_] := NIntegrate[Exp[-(ky/(2 Sqrt[π])- (r[[1]] + I r[[2]]) Sqrt[π])^2], {ky, -π, π}];

f2[r_] := N[(Erf[(r[[1]] + I r[[2]]) π - Sqrt[π]/2,(r[[1]] + I r[[2]]) π + Sqrt[π]/2]), 3];

f3[r_] := N[π (-Erf[1/2 Sqrt[π] (-1 + 2 (r[[1]] + I r[[2]]))] +
               Erf[1/2 Sqrt[π] (1 + 2 (r[[1]] + I r[[2]]))]), 3];

f4[r_] := N[π Erf[1/2 Sqrt[π] (-1 + 2 (r[[1]] + I r[[2]])),
                  1/2 Sqrt[π] (1 + 2 (r[[1]] + I r[[2]]))], 3]

When I evaluate these functions, I get these results:

f1[{0, -4.4}]
4.90038*10^25 + 1.28849*10^11 I

f2[{0, -4.4 }]
-2.29748*10^81 + 0. I

f3[{0, -4.4}]
4.90038*10^25 + 0. I

f4[{0, -4.4}]
4.90038*10^25 + 0. I

The real part is same for numerical integration and Erf[p, q] function, but the imaginary part is way off. Is there any way to correct this?

  • 1
    $\begingroup$ How odd. With[{t = -4.4 I}, NIntegrate[Exp[-(ky/(2 Sqrt[π]) - t Sqrt[π])^2], {ky, -π, π}]] gives a real answer, but NIntegrate[Exp[-(ky/(2 Sqrt[π]) - (-4.4 I) Sqrt[π])^2], {ky, -π, π}] does not. $\endgroup$ Nov 2, 2017 at 18:10
  • $\begingroup$ I've even tried to find the series expansion for erf function and then plug in values but it did not help either. $\endgroup$ Nov 3, 2017 at 7:39
  • $\begingroup$ Personally, I'm more inclined to rely on the result of two-argument Erf[]. Is there any reason why you need to use NIntegrate[]? $\endgroup$ Nov 3, 2017 at 7:42
  • 1
    $\begingroup$ This is appearing in my condensed matter physics research. NIntegrate is consequence of doing approximation from summation over momentum space to the integration over momentum space in thermodynamic limit. The results obtained from NIntegrate[] and summation are in a agreement so I know that NIntegrate[] is giving valid results, valid in a sense that probability distribution is finite. If I do Erf[] difference result the probability distribution diverges so the results are not physically valid. $\endgroup$ Nov 3, 2017 at 23:19
  • $\begingroup$ The problem you presented here, and the problem you describe in your last comment, are seemingly at odds. In your post, it's the third and fourth functions that are sensible, and the first one with NIntegrate[] that is broken. $\endgroup$ Nov 4, 2017 at 9:45

2 Answers 2


I think you're just seeing numerical noise with the NIntegrate function. Notice that the imaginary part is close to 15 orders of magnitude smaller than the real part:

Normalize[4.90038*10^25 + 1.28849*10^11 I]

1. + 2.62937*10^-15 I

Typically, you can use extended precision numbers to avoid this complex fuzz:

f1[r_] := NIntegrate[
    Exp[-(ky/(2 Sqrt[π])-(r[[1]]+I r[[2]]) Sqrt[π])^2],
    {ky, -π, π},
    WorkingPrecision -> 30

f1[{0, -4.4`30}]



One way to get rid of machine-precision rounding noise is to use a relative-error Chop with a small multiple of $MachineEpsilon:

relChop[z_?NumericQ, e_: 10 $MachineEpsilon] := Chop[z/Abs[z], e] Abs[z]

relChop[f1[{0, -4.4}]]
(*  4.90038*10^25  *)

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.