Filter löschen
Filter löschen

Problem with if function and decimal numers

3 Ansichten (letzte 30 Tage)
Nicola
Nicola am 13 Jan. 2014
Kommentiert: Nicola am 13 Jan. 2014
Good evening, i have a problem with this code:
a=1:0.1:2;
b=0.1;
for i=1:1:10
angoloutile(i)=a(i);
end
for j=1:1:10
integ = fix(angoloutile(j)) ;
fract = abs(angoloutile(j)- integ);
if fract == b
angoloutile(j)=angoloutile(j)+0.5
end
end
for k=1:1:10
angoloutile(k)
end
it doesn't take the IF function
if fract == b
but i'm sure that angoloutile(2) is 1.1 Thanks

Akzeptierte Antwort

Image Analyst
Image Analyst am 13 Jan. 2014
The problem was using fix instead of rounding. Try this where I use int32() to cast to the nearest integer. I also cleaned up your if to make it more efficient and reduce the number of if tests by a factor of 10.
a=1:0.1:2;
for i=1:1:10
angoloutile(i)=a(i);
end
for j=1:1:10
integ = fix(angoloutile(j));
fract = abs(angoloutile(j)- integ);
fract=fract*10;
deci=int32(fract);
fprintf('j = %d, angoloutile(j) = %f, integ = %d, fact = %f, deci = %d\n', ...
j, angoloutile(j), integ, fract, deci);
if deci == 1
angoloutile(j)=angoloutile(j)+0.5;
elseif deci == 2
angoloutile(j)=angoloutile(j)-0.08;
elseif deci == 3
angoloutile(j)=angoloutile(j)-0.12;
elseif deci == 4
angoloutile(j)=angoloutile(j)-0.16;
elseif deci == 5
angoloutile(j)=angoloutile(j)-0.20;
elseif deci == 6
angoloutile(j)=angoloutile(j)-0.24;
elseif deci == 7
angoloutile(j)=angoloutile(j)-0.28;
elseif deci == 8
angoloutile(j)=angoloutile(j)-0.32;
elseif deci == 9
angoloutile(j)=angoloutile(j)-0.36;
end
end
% Print to command window.
angoloutile

Weitere Antworten (1)

Amit
Amit am 13 Jan. 2014
The issue is very small difference in floating point numbers, like 1.00000 and 1.0000000001. There is negligible difference however they are not equal. You can do something like:
tol = 1e-6;
if (fract-b) < tol
This will work.
  1 Kommentar
Nicola
Nicola am 13 Jan. 2014
I'm sorry but your answer doesn't work for me. I changed the code and now is:
a=1:0.1:2;
for i=1:1:10
angoloutile(i)=a(i);
end
for j=1:1:10
integ = fix(angoloutile(j));
fract = abs(angoloutile(j)- integ);
fract=fract*10;
deci=fix(fract);
if deci == 1
angoloutile(j)=angoloutile(j)+0.5;
end
if deci == 2
angoloutile(j)=angoloutile(j)-0.08;
end
if deci == 3
angoloutile(j)=angoloutile(j)-0.12;
end
if deci == 4
angoloutile(j)=angoloutile(j)-0.16;
end
if deci == 5
angoloutile(j)=angoloutile(j)-0.20;
end
if deci == 6
angoloutile(j)=angoloutile(j)-0.24;
end
if deci == 7
angoloutile(j)=angoloutile(j)-0.28;
end
if deci == 8
angoloutile(j)=angoloutile(j)-0.32;
end
if deci == 9
angoloutile(j)=angoloutile(j)-0.36;
end
end
for k=1:1:10
angoloutile(k)
end
I need that if 1.0 nothing happens,if 1.1 it becomes 1.6, if 1.2 it becomes 1.12, if 1.3 it becomes 1.18 etc... The problem is that this trick doesn't work for 1.2!I don't know why it writes 1.7! and also 1,4 it writes 1,28 and not 1,24! Thanks so much

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by