# Math Help - software problem?

1. ## software problem?

I've the following code in Matlab:

Code:
clear;
s=100;
for i = 0.05 : 0.05 : s
if i == 1 %8.05
i
end
end
in some cases ( for example 1 or .05 and so on) it works, the code prints out i, but in many other cases ( for example 10) it doesn't work it doesn't write out i

I don't know why I tried many things for example using 10.0000 or double().

if anybody has a good idea please write it me! Thanks

Hi again!

I wrote this code:

Code:
clear;
k = 0;

for j = 0.05 : 0.05 : 15

for i = 0.05 : 0.05 : 15
if i == 10 % if I use i == j it works but in this situation the code doesn't write out i
i
k = k+1;
end
end

end

2. Originally Posted by ftomi7
I've the following code in Matlab:

Code:
clear;
s=100;
for i = 0.05 : 0.05 : s
if i == 1 %8.05
i
end
end
in some cases ( for example 1 or .05 and so on) it works, the code prints out i, but in many other cases ( for example 10) it doesn't work it doesn't write out i

I don't know why I tried many things for example using 10.0000 or double().

if anybody has a good idea please write it me! Thanks

Hi again!

I wrote this code:

Code:
clear;
k = 0;

for j = 0.05 : 0.05 : 15

for i = 0.05 : 0.05 : 15
if i == 10 % if I use i == j it works but in this situation the code doesn't write out i
i
k = k+1;
end
end

end
Your question is difficult to understand, but at a guess is that your problem is a result of trying to test floating point numbers for equality. This is always a problem as floats are only under certain circumstances exact representations of the numbers you think they are.

Try this:

Code:
clear;
k = 0;

for j = 0.05 : 0.05 : 15

for i = 0.05 : 0.05 : 15
if abs(i-10) < 1e-11
i
k = k+1;
end
end

end
CB

3. thanks

but I don't understand in which situations it works... why is good 8 and why isn't good 10?

for example it never will be equal:

for i = 0.05 : 0.05 : 15
if i == 10
i
end
end

4. Originally Posted by ftomi7
thanks

but I don't understand in which situations it works... why is good 8 and why isn't good 10?

for example it never will be equal:

for i = 0.05 : 0.05 : 15
if i == 10
i
end
end
It does not matter what you understand or do not, it has to do with which decimal fractions can be represented exactly in binary. Never compare floats for equality.

CB

5. I think I found a solution, but it may not the best, but it works:

for i = 0.05 : 0.05 : 15
if round(i*10000) == round(10*10000)
i;
end
end

In the above code I convert double to int.

6. Originally Posted by ftomi7
I think I found a solution, but it may not the best, but it works:
Originally Posted by ftomi7

Code:
for i = 0.05 : 0.05 : 15
if round(i*10000) == round(10*10000)
i;
end
end

In the above code I convert double to int.

1. No you don't. The result of round is not an int type but is still a float but with the fractional part set to zero, as long as the integer part fits into 54 bits (IIRC) this is exactly representable in a float.

2. Did you read my previous post that gave a perfectly acceptable method of doing what you want (more efficiently and in fewer operations)?

CB