# Mathematica sometimes failing to write to file

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[i] <> ".dat";


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?

• 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. – Alex Milne Sep 17 '19 at 9:34
• Maybe this explains things for you: For[radius = .1, radius < 1.3, radius += .1, Print@IntegerString[IntegerPart[10*radius], 10]] – ktm Sep 17 '19 at 13:09

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";
^^^^^
`
• In hindsight, that is an almost obvious answer :) Thank you very much! – Alex Milne Sep 17 '19 at 16:46