Inconsistent calculation results.

Hello,

I'm seeing some odd behavior in my program and I can't seem to find the reason.

The majority of time is spent on simple calculations that look up several numbers from a distance matrix and add or substract those numbers.

Now, as an attempt at low-level optimizations, I changed the calculation functions slightly: using the fact that (a - b + c) = (-b + c + a) and that my Distance matrix is symmetric (d[a][b] == d[b][a]), I changed the sequence of the calculations in hopes of getting faster lookups. So, as example:

1
2
3
COST exampleCost(int I, int J) {
    return d[i][j+1] + d[j][i+1] + d[i][i+1] + d[i][j];
}

might become:
1
2
3
COST exampleCost(int I, int J) {
    return d[i][i+1] + d[i][j] + d[i][j+1] + d[i+1][j];
}

With the goal being that each lookup is closer together compared to the first version.

Effectivity aside, I'm seeing odd results: just changing around the order of the calculation (and reversing indeces) for some reason is changing the results. Due to the nature of the program, it's nearly impossible to debug and manually find the cause unless I have a very good idea what the possible causes are.

So, my question is: is there anything else than "you messed up one of the functions" that could cause this? How do I check for it?
(Alternatively: would this change ever have any significant effect?)

Extra info:
-All COST-related variables are doubles.
-The distance table is conceptually a NxN matrix (N = 5000 in this case and expected to get larger in future testing) and practically a std::vector<COST> of size N². Lookups are done through operator(i, j) that returns vector[i*N+j].
-I'm certain (?) the Distance matrix is symmetrical: it's generated by calculating cost(i, j) for i <= j and copying from (j, i) in case i > j.
-I'm compiling with /fp:precise, Win7 64bit, using MSV express 2010.
It's my understanding that matrix calculations are not commutative.
There are no matrix calculations, just adding and substracting doubles. The doubles are stored in the matrix, but the values in the matrix are never changed. Values are looked up in the matrix and the result is returned to the calling function.
All the checks I can think of report no problems. As far as I can tell, none of the calculations are wrong, just unprecise to some margin.

Is there a way to get a bit more consistency other than activating /fp:precise (which doesn't seem to lead to different results from /fp:fast; not in terms of result or speed)? Note that these calculations happen incredibly often, so minimal overhead is a requirement.
Topic archived. No new replies allowed.