필터 지우기
필터 지우기

Problem with if function and decimal numers

조회 수: 3 (최근 30일)
Nicola
Nicola 2014년 1월 13일
댓글: Nicola 2014년 1월 13일
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

채택된 답변

Image Analyst
Image Analyst 2014년 1월 13일
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

추가 답변 (1개)

Amit
Amit 2014년 1월 13일
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
Nicola
Nicola 2014년 1월 13일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by