If the number is displaying as 2.0000... on the screen, it isn't exactly 2 but it is close to 2. If a number is exactly 2 it will display as 2 without the 0's past the decimal point printed. So you apparently have done some calculations that result in a number that is very slightly less than 2.
Basically, the result of your floating point calculations cannot be represented exactly in IEEE double precision arithmetic. So this is leading to your problem. E.g., your calculations with the exact decimal conversions shown (note that 1.2 and 0.2 cannot be represented in IEEE double precision exactly):
>> mul = 2*6
mul =
12
>> XVY = 10
XVY =
10
>> XVZ = mul/XVY
XVZ =
1.2000
>> XVZ_1 = floor(XVZ)
XVZ_1 =
1
>> XVW = 1.2 - XVZ_1
XVW =
0.2000
>> T1 = XVW*10
T1 =
2.0000 <-- 0's printing after decimal point means number is not exactly 2
>> floor(T1)
ans =
1
>> num2strexact(XVZ)
ans =
1.1999999999999999555910790149937383830547332763671875
>> num2strexact(XVW)
ans =
0.1999999999999999555910790149937383830547332763671875
>> num2strexact(T1)
ans =
1.999999999999999555910790149937383830547332763671875
Best Answer