What's the mathematica command to get the numerical value of :

$$PV\int_0^\infty \frac{\tan x}{x}\text{d}x?$$

where $PV$ is the principal value.

  • 3
    $\begingroup$ Have you looked at NIntegrate yet? $\endgroup$
    – b3m2a1
    Commented Feb 23, 2021 at 3:11
  • $\begingroup$ @b3m2a1 yes.. didnt work and also tried the WorkingPrecision and still nothing. $\endgroup$ Commented Feb 23, 2021 at 7:19
  • 1
    $\begingroup$ It might be of interest to others, I suppose, that the double exponential oscillatory method can be used to get an OK result on this integrand that has periodic simple poles: Pi/2 - NIntegrate[(Tan[x]/x - Tan[x]/(Round[x + Pi/2, Pi] - Pi/2)), {x, 0, Infinity}, Method -> {"DoubleExponentialOscillatory", "TuningParameters" -> {1, 5}}, MaxRecursion -> 20, WorkingPrecision -> 20] $\endgroup$
    – Michael E2
    Commented Feb 23, 2021 at 19:30

3 Answers 3


The integral is Pi/2, a proof of which may be found on math.SE. Here's a numerical check, integrating $(\tan z)/z$ over parallel paths $z = x \pm a i$ and subtracting $(2 a)/(a^2 + x^2)$ to get NIntegrate to converge. The OP's integral is equal to the sum of the two below.

a = 50;
1/2 NIntegrate[
  Tan[x + a I]/(x + a I) + Tan[x - a I]/(x - a I) -
     (2 a)/(a^2 + x^2) //
    ComplexExpand // Simplify,
  {x, 0, Infinity}, AccuracyGoal -> 16]
1/2 Integrate[(2 a)/(a^2 + x^2), {x, 0, Infinity}, 
  Assumptions -> a > 0]


It was late and I was tired. A shortcut (due to the symmetry of Tan[x]/x) occurred to me this morning:

a = 50;
NIntegrate[Re[Tan[x]/x], {x, 0 + a I, Infinity}, WorkingPrecision -> 50]

(*  1.5707963267948966192313216916397514399782555923723  *)
  • $\begingroup$ Thank you. So we have to manipulate the integrand? The answer (pi/2 ) is well -known but my point is to learn the command to approximate more advanced PV integrals such such $PV \int_0^\infty (\tan x/x)^3 dx.$ $\endgroup$ Commented Feb 23, 2021 at 7:23
  • $\begingroup$ @AliShadhar One of the ideas is to deform the path of integration to go around both sides of a pole in the complex plane (to get the PV). Whether that works numerically depends on the function. Here I took advantage of some symmetry in $\tan x/x$ because of the route I took; in fact, NIntegrate[Re[Tan[x]/x], {x, 0 + a I, Infinity}, AccuracyGoal -> 16, WorkingPrecision -> 20] is simpler. Note that $(\tan x/x)^3$ has a nonzero coefficient of $(x-x_0)^{-2}$ in its Laurent expansion at a pole; so it does not have a principal value (in the traditional sense as I know it). $\endgroup$
    – Michael E2
    Commented Feb 23, 2021 at 15:35
  • $\begingroup$ Thank you Michael. your update is much simpler. By the way, a friend claimed that he mathematically proved that the $PV \int_0^\infty (\tan x/x)^3 dx$ converges which is the reason I am trying to verify such claim on mathematica. $\endgroup$ Commented Feb 23, 2021 at 19:03
  • 1
    $\begingroup$ @AliShadhar Your friend is perhaps using a different definition of PV. For instance $PV \int_0^\pi (\tan x/x)^3\,dx$ diverges to $+\infty$, as it does at each pole. (Numerical check: Table[NIntegrate[(Tan[x]/x)^3, {x, 0, Pi/2 - 10^-k}, WorkingPrecision -> 50, MaxRecursion -> 20] + NIntegrate[(Tan[x]/x)^3, {x, Pi/2 + 10^-k, Pi}, WorkingPrecision -> 50, MaxRecursion -> 20], {k, 10}] shows clearly the contribution of the 48/(Pi^4 (x - Pi/2)^2) term of the Laurent series.) $\endgroup$
    – Michael E2
    Commented Feb 23, 2021 at 19:23
  • 1
    $\begingroup$ @Ali, are you sure your friend was not confusing the Hadamard finite part with the Cauchy principal value? $\endgroup$ Commented Feb 23, 2021 at 21:34

You can evaluate principal-valued integrals using NIntegrate using Method->"PrincipalValue" but you must specify the singular point(s). In your case, there are an infinite number of them. For starters, integrating over just the first one at $\pi/2$, we can write:

NIntegrate[Tan[x]/x, {x, 0, Pi/2, 3}, 
 Method -> "PrincipalValue"]

Out[42]= 1.36501

Notice how I inserted the first singular point $\pi/2$ between the limits of integration. If we wanted to integrate over two of them, then:

In[44]:= NIntegrate[Tan[x]/x, {x, 0, Pi/2, 3 Pi/2, 6}, 
 Method -> "PrincipalValue"]

Out[44]= 1.46884

And if I wanted to integrate over 25 singular points, I would sequence-in a table of 25 singular points into the limits of integration and notice in the plot below it's convergence to $\pi/2$:

totalN = 25;
myTable = Table[
     Tan[x]/x, {x, 0, 
      Sequence @@ Table[Pi/2 + n Pi, {n, 0, index}], (index + 1) Pi}, 
     Method -> "PrincipalValue"]},
   {index, 0, totalN}];
ListPlot[myTable, Joined -> True]

plot of principal-valued integrals

  • $\begingroup$ Thank you Dominic for the efforts. +1 $\endgroup$ Commented Feb 23, 2021 at 19:04

The method "PrincipalValue" does not work on an integral with infinitely many poles, since each pole must be specified. Confining to a finite region of integration is problematic on integrals like $\int_0^\infty (\tan x/x)\,dx$ that converge slowly. NIntegrate has built in the "ExtrapolatingOscillatory" strategy, but even if it would work together with the "PrincipalValue" strategy, you would still have the problem of specifying infinitely many poles. Instead, we can manually call NSum on the principal value integral over each period of Tan[x]. NSum will do the extrapolation for us. (Something similar was done here and is shown in the documenation.) The series still converges slowly, and the results are not as good as my other answer.

cc[n_Integer] := cc[n] =
   NIntegrate[Tan[x]/x, {x, 0 + n Pi, Pi/2 + n Pi, Pi + n Pi}, 
    WorkingPrecision -> 50, 
    Method -> {"PrincipalValue", Method -> "GaussKronrodRule"}];

NSum[cc[n], {n, 0, Infinity}, NSumTerms -> 50, 
  WorkingPrecision -> 50] // AbsoluteTiming

(*  {1.76767, 1.57067}  *)


We can speed up the principal value integral by subtracting a null integral that smashes the pole (something similar was done here). The slow convergence persists, but the following is twice as fast as with the "PrincipalValue" method.

cc[n_Integer] := cc[n] = NIntegrate[
    Tan[x]/x - Tan[x]/(Pi/2 + n Pi), 
    {x, 0 + n Pi, Pi + n Pi}, 
    WorkingPrecision -> 50, Method -> "GaussKronrodRule"];

NSum[cc[n], {n, 0, Infinity}, NSumTerms -> 1000, 
  WorkingPrecision -> 50] // AbsoluteTiming

(*  {10.2142, 1.57078947}  *)

Last@% - Pi/2   (* error *)
(*  -6.85*10^-6  *)
  • $\begingroup$ Still, I thought it was cool to sequence-in all those singularities. :) $\endgroup$
    – Dominic
    Commented Feb 23, 2021 at 15:59
  • $\begingroup$ @Dominic Yes, it was. Personally I like smashing the pole with NIntegrate[Tan[x]/x - Tan[x]/(Pi/2 + n Pi), {x, 0 + n Pi, Pi + n Pi}, WorkingPrecision -> 50, Method -> "GaussKronrodRule"], which I didn't show. It doesn't perform better. $\endgroup$
    – Michael E2
    Commented Feb 23, 2021 at 16:06
  • $\begingroup$ @Dominic Well, it is faster than "PrincipalValue". $\endgroup$
    – Michael E2
    Commented Feb 23, 2021 at 16:14

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.