26

Bug introduced in 8.0, fixed in 10.4, intentionally reintroduced in 11.0 and persisting through 13.0.0 or later


In the following example, inside the minimumX's Module, x is initialized to 1, but the results say otherwise:

solutionQ = Function[{x, d}, IntegerQ[Sqrt[(x^2 - 1)/d]] && x != 1];
minimumX = 
  Compile[{{d, _Integer}}, 
   Module[{x = 1}, While[! solutionQ[x, d], x++]; x]];

minimumX[2] minimumX[3] minimumX[2]

(* 3

7

17 *)

If I define minimumX as:

minimumX = 
  Function[{d}, Module[{x = 1}, While[! solutionQ[x, d], x++]; x]];

or

minimumX = 
  Compile[{{d, _Integer}}, 
   Module[{x = 1}, x = 1; While[! solutionQ[x, d], x++]; x]];

It works as I was expecting:

(*
3

2

3 *)

Is this expected behavior, or should I report a bug?

(Mathematica 9.0.1)

bmf
  • 15,157
  • 2
  • 26
  • 63
P. Fonseca
  • 6,665
  • 2
  • 28
  • 60
  • 7
    This is definitely not expected and you should report it. The localization of x in the instruction {46, Function[{d}, While[ !solutionQ[x, d], x++]], {x, 2, 0, 1, Module}, 2, 0, 0, 6, 0, 17} is clearly not being respected. The same behavior is observed for With in place of Module, but not for Block, which works as it should. It also works correctly in versions 7 and 5.2; the bug seems to have been introduced in version 8, when major changes were made to the VM. (The compiled code generated by all versions is functionally identical and differs only by opcode numbering.) – Oleksandr R. May 12 '13 at 03:38
  • 1
    For completeness, let me just note that the function can be written more optimally as With[{solutionQ = Function[{x, d}, FractionalPart[Sqrt[(x^2 - 1)/d]] == 0 && x != 1]}, minimumX = Compile[{{d, _Integer}}, Module[{x = 1}, While[! solutionQ[x, d], x++]; x]]]. This is somewhat beside the point as far as the question goes since the bug does not manifest in this case, but I'm sure others would have been moved to comment on it if I hadn't. Indeed, I think it's worth emphasizing to WRI support that this particular non-optimal formulation is necessary to expose the bug. – Oleksandr R. May 12 '13 at 04:04
  • 2
    I think, what's worth noticing is: if You define minimumX by SetDelayed(:=) not only Set(=) it works like it should. – Kuba May 12 '13 at 07:32
  • Thank you all. I've just reported the bug. – P. Fonseca May 12 '13 at 10:19
  • I know it has been a long time, but this bug is still there in 10.0.2. One thing I tried though, if you initialize x in the Module by a delayed assignment, that is Module[{x:=1},.. it works, but the resulting numbers are actually floating numbers (* 3. 2. 3. *) ... I see contradicting logic here. – Bichoy May 23 '15 at 18:05
  • 6
    This was fixed in version 10.4 and intentionally rebroken to address a huge slowdown. – Daniel Lichtblau Aug 16 '16 at 14:41
  • @DanielLichtblau but it will eventually have a bright future, right? Just coming with the new compiler technology? – P. Fonseca Aug 18 '16 at 19:01
  • 1
    @P.Fonseca (late reply, I know) I can only say that I hope so. – Daniel Lichtblau Apr 17 '22 at 17:52

0 Answers0