MATLAB: How to prevent Matlab from randomly adding trailing zeros to integers

trailing zeros

Matlab randomly adds trailing zeros to integers (see example below). Subsequently it does not accept these values as indices in a vector leading to errors.
if true
4.82*100-101
ans =
381
>> 4.81*100-101
ans =
380.0000
>> 4.80*100-101
ans =
379
end
If I use these results as indices in a vector 381 and 397 work fine but 380.000 doesn't. Why does matlab add those zeros? All results should be integers. How do I prevent this from happening?

Best Answer

In the examples you give, the ‘integers’ are the results of mathematical operations, not simple definitions of integers. (Integers entered as integers can be represented exactly in binary.)
The ‘trailing zeros’ displayed are not really trailing zeros, but zeros between the most significant and least significant decimal digits. For a comprehensive discussion, see: Why is 0.3 - 0.2 - 0.1 (or similar) not equal to zero?