I’ve found a strange behaviour in a piece of code I wrote which does not happens when I try to reproduce it out of the program (i.e. with MATLAB).
I’m doing a operation over an array as follow:
a[i] = a[i] / (sqrt(b[i]) * sqrt(c[i]));
where a, b and c are arrays of type float. The values of a, b and c prior division range from 2000 to 6000000
The result of the operation is zero for all the elements of the array, but looking at the values of the numerator and denominator separately, for a given element, the operation is 89509.0 / 90240,375 which is equal to 0,991895258.
So, I wondering why the full operation within the program using float does not work, and it works separately as expected. Probably due to hours of obfuscated debugging?
When I run across a similar situation, I find that it often helps to break it down into steps and see which steps behave properly and which do not. For example, for this, it could be broken down into the following:
Now I can run through each item and see where it fails (if it fails). Fact is, when I do this, I often find the problem while creating the steps, and then it is fixed.