After doing some line-by-line debugging over the weekend, I managed to find where the problem was.
Basically, unbeknownst to me, the threads created by the parallel.for
did not inherit the CultureInfo (this is the normal behaviour of threads, and I didn't know that). What was happening then was that strings like 3.256
were being parsed to 3256.0
. This caused the issues I found with the output.
(Note: the default locale on my computer is set to use a comma as decimal separator, but I had set to the full stop in program.cs for all my code. I had incorrectly assumed this would be inherited by new threads)
The correct parallel snippet looks like this:
CultureInfo newCulture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
newCulture.NumberFormat.NumberDecimalSeparator = ".";
Parallel.For(0, M, i =>
{
Thread.CurrentThread.CurrentCulture = newCulture;
double d;
try
{
d = Double.Parse(lData[i]);
}
catch (Exception)
{
throw new Exception("Wrong formatting on data number " + (i + 1) + " on line " + (lCount + 1));
}
GlobalVar.sgData[lCount % N][i] = d;
});
Thanks to all who pitched in with comments and opinions. Good information to improve my programming.
I updated the question tags to reflect where the issue really was.