4

I have a numerical integral that evaluates fine for floating point arguments with low precision (e.g., 4 digits), with argument where only zeros follow the first, e.g., 4 non-zero digits, or with fractions with infinite precision. However, the evaluation crashes without error message when I enter a number that has 30 digits precision and non-zero values in these 30 digits.

Here is the specific code:

(* Define the integral function. *)
θ[ϱ_, ρ0_] := 2 ArcTan[Exp[(ϱ - ρ0)]] + 2 ArcTan[Exp[(ϱ + ρ0)]]
mz[ϱ_, ρ0_] := Cos[θ[ϱ, ρ0]]
Ka[k_] := EllipticK[k^2/(-1 + k^2)]/Sqrt[1 - k^2]
k[p_, y_, z_] := Sqrt[4 y z/(p^2 + (y + z)^2)]
g[p_, x_, ϱ_] := 
  1/Pi Sqrt[1/(x ϱ)] (k[0, x, ϱ] Ka[k[0, x, ϱ]] - k[p, x, ϱ] Ka[k[p, x, ϱ]])
I3[ρ0_, δ_, rmax_] := 
  δ^2 Re[
    NIntegrate[
      ϱ x (1 - mz[x, ρ0]) g[1,x δ , ϱ δ ] mz[ϱ, ρ0] Boole[x - ϱ < 0], 
      {x, 0, ρ0 + 15}, {ϱ, 0, rmax}, 
      PrecisionGoal -> 7, 
      Method -> 
        {"GlobalAdaptive", "MaxErrorIncreases" -> 100000, 
          Method -> {"GaussKronrodRule", "Points" -> 3}}, 
      MaxRecursion -> 20, 
      WorkingPrecision -> 30] + 
    NIntegrate[
      ϱ x (1 - mz[x, ρ0]) g[1, x δ, ϱ δ] mz[ϱ, ρ0]  Boole[x - ϱ > 0], 
      {x, 0, ρ0 + 15}, {ϱ, 0, rmax}, 
      PrecisionGoal -> 7, 
      Method -> 
        {"GlobalAdaptive", "MaxErrorIncreases" -> 100000, 
          Method -> {"GaussKronrodRule", "Points" -> 3}}, 
      MaxRecursion -> 20, 
      WorkingPrecision -> 30]]

(* This works fine, just gives a warning that the precision of the second argument is smaller than the working precision of the integral. *)
I3[10, 1.677 , 10^5]

(* This also works fine. *)
I3[10, 1.6770000000000000000000000000000000000000000000 , 10^5]

(* This will crash without error message or just output the NIntegrate command without executing it. *)
I3[10, 
 SetPrecision[1.677, 30] , 10^5]

The crash is reproducible with Mathematica 10.3.1.0 and 10.4. Switching the internet connection off, as suggested here, does not help.

Any suggestions how to evaluate the integral? As a workaround, I would be happy with a way to use SetPrecision in a way that it converts

1.677

to

1.67700000000000000000000000000

instead of

1.67700000000000004618527782441
Felix
  • 3,871
  • 13
  • 33
  • You can get the result by rationalizing the second argument, I3[10, 1677/1000, 10^5]. – Anton Antonov Mar 08 '16 at 21:01
  • Works fine in v9.0.1. Have you reported this to WRI? – xzczd Apr 14 '16 at 05:04
  • I cannot cut-and-paste the code above, seems to have syntax issues. – Daniel Lichtblau Apr 14 '16 at 14:44
  • No, I didn't report it yet. I'm not sure what you mean with syntax issues, but I just copy-pasted the code from above in a fresh notebook with a new kernel and it ran without any issues (apart from the one explained in the question). – Felix Apr 15 '16 at 06:29

1 Answers1

3

Perhaps this will work for you.

ToArbitraryPrecison[x_?NumericQ, digits_Integer?Positive] := 
  N[Rationalize[x, 10^-digits], digits]

ToArbitraryPrecison[1.677, 30]

1.67700000000000000000000000000

I3[10, ToArbitraryPrecison[1.677, 30], 10^5]

-70.828063489122124235733396956

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
  • Thanks for the workaround. I guess the behavior is a bug that should be reported and the question should get the bug tag, right? – Felix Mar 08 '16 at 22:20