0
$\begingroup$

I stumbled upon a weird behaviour where inside a For-loop, Mathematica will not output anything into a file in certain iterations. The code I have is symbolically

For[radius = .1, radius < 1.3, radius += .1, 
 ParallelDo[
  Module[{list = {}, value = 1, k = 1}, While[Abs[value] > 10^-20,
    value = function[k, .1, -1 - .5 i, radius];
    AppendTo[list, value];
    k++];
   list >>> 
    "evaldata_r" <> 
     IntegerString[IntegerPart[10*radius], 10] <> "_" <> 
     IntegerString[i] <> ".dat";
   Print[{radius, value}]], {i, 150}]]

Because I want to parallelize my computation i create 150 files and stitch them up afterwards to avoid collisions between parallel evaluations.

My problem right now is that certain radii, namely .8 and 1.1 are calculated and printed to the notebook (which I included for debugging), but no files are created and I end up with 1500 files instead of 1800.

I encountered errors before where Mathematica would not recognize that I had change the directory, but it does not create the missing 300 files anywhere on my system if Windows search can be trusted.

Does anyone have an idea where something could go wrong?

$\endgroup$
  • $\begingroup$ I also just did a wider sweep up to radius 5 with lower stepcounts of i=50, and I can add radius=5 to the list of radii Mathematica is not outputting. $\endgroup$ – Alex Milne Sep 17 at 9:34
  • $\begingroup$ Maybe this explains things for you: For[radius = .1, radius < 1.3, radius += .1, Print@IntegerString[IntegerPart[10*radius], 10]] $\endgroup$ – user6014 Sep 17 at 13:09
1
$\begingroup$

In short, due to how floating point arithmetic works,

In[51]:= IntegerString[IntegerPart[10*(.7 + .1)], 10]
Out[51]= "7"

gives you "7" when you're expecting "8".

In[52]:= 10*(.7 + .1) // FullForm
Out[52]= 7.999999999999999`

I'd recommend throwing a Round in your code to resolve the issue:

list >>> "evaldata_r" <> 
 IntegerString[IntegerPart[Round[10*radius]], 10] <> "_" <> IntegerString[i] <> ".dat";
                           ^^^^^
$\endgroup$
  • $\begingroup$ In hindsight, that is an almost obvious answer :) Thank you very much! $\endgroup$ – Alex Milne Sep 17 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.