I am trying to compare an array of doubles with a scalar double for equality, but equality is never recognized under certain circumstances. I suspect this is due to the way the double is represented (e.g. 1.0 vs. 1.00), but I can't figure it out.
For example, I generated an array consisting of thousands of double values, the last few of which at some point in time are given
10.6000 -11.0000 10.2000 22.6000 3.4000
If I test equality 10.2 (or 10.2000) with the command array==10.2 (or array=10.2000 ), I return an array from 0s. If I put the values โโshown in the array manually (for example, array=[10.6000 -11.0000 10.2000 22.6000 3.4000] ), the command will succeed (i.e. array==10.2 returns 0 0 1 0 0 ). Can someone explain why equality is successful if I enter the values โโmanually, but fail if the array is created in the context of the program? I can fix the comparison error using an approximate rather than an exact comparison (e.g. (array<10.20001) & (array>10.19999) ), but this seems unsatisfactory.
Edit: values โโin the array are generated by iteratively adding or subtracting the double constant (for example, 0.2 ). Therefore, the modulus of this array at 0.2 should always be 0 . In fact, the module of each element is either 0 or 0.2 , as shown below for the indicated sequence of numbers in the array:
mod(array,0.2) ... 0.2000 0 0.2000 0.2000 0
Again, if the values โโare placed into the array manually and the module is accepted, the expected value of only 0 is expected.
source share