Double matlab comparison

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.

+6
source share
3 answers

The reason is that MATLAB truncated the numbers in the array to save only 4 digits after the decimal point in the display. That is, the real value of your array can be [10.600000000001, -10.99999999999, ...] . You are right, this is due to the internal representation of floating point numbers in the computer, which can lead to small errors in the calculations.

Personally, I think that there are two solutions: one is an approximate match, like you, and the second is first around the array (for example, this tool from FileExchange), and then do an exact match.

+6
source

Something, perhaps somewhere in solitary precision, and elsewhere. Binary representation, for example. 10.2 are different in each case, because they end after a different number of bits. Thus, they are different:

 >> if (single(10.2) == 10.2) disp('honk'); end >> if (single(10.2) == single(10.2)) disp('honk'); end honk 

You will need to check for equality within a small difference:

  eps = 0.001; result = abs(array-10.2) < eps; 

You can find the precision used in the array using whos:

 >> whos A Name Size Bytes Class Attributes A 1x2 8 single 
+2
source

Create a MATLAB function file that takes values โ€‹โ€‹modulo (from 3 to 9, that is, from Z3 to Z9) and produces the smallest possible value described by modulo conditions.

Modeling Example:

Z = [3 4 5]; % Modulo Z3, Z4 and Z5

r = [2 1 4]; % Other values

The smallest possible value is 29.

The inputs Z must be an array matrix ... where you can enter any number from 3 to 9 .... and you can enter 3,4,5,6,7,8,9 in any order, in any connections or groups. ..

The inputs r must be equal to the number of inputs z ...

the output should give the smallest possible value, although modulo conditions ...

+1
source

Source: https://habr.com/ru/post/906576/


All Articles