# I want to find the number of decimal places, and my code is perfectly fine. But for some reason, when my value is 0.888, the length of the decimal is returned as 16.

2 views (last 30 days)

Answered: Walter Roberson on 29 Aug 2022
I want to find the number of decimal places, and my code is perfectly fine. But for some reason, when my value is 0.888, the length of the decimal is returned as 16. And it seems that every number greater than 0.888 will result in 16 decimal places.
x=0.888;
m=length(x);
n=zeros(1,m);
for j=1:m
i=0;
while round(x(j))~=x(j)
x(j)=x(j)*10;
i=i+1;
end
n(j)=i;
end
y=max(n(j));

Chunru on 29 Aug 2022
MATLAB use IEEE double precision format for default numeric type.
The number of significant bits for representing double is 53bit or 15.95 decimal digits. For a number near 1.0, the number of accurate decimal digits is around 16 decimal places (similar to the significant digits).
For large number (abs(x)>>1) and small number (abs(x)<<1), you should check the significant digits instead of decimal places.

Walter Roberson on 29 Aug 2022
In any finite-maximum-length representation of numbers, it is inevitable that some numbers will have to be approximated and will internally not be stored exactly as their common representations. For example if you use 10 decimal digits then 1/3 would be 0.3333333333 and multiply that by 3 you would get 0.9999999999 rather than 1 exactly. If you use finite length decimal then (1/3)*3 is not exactly 1.
If you do not used fixed maximum length, if you use as much storage as needed to achieve your precision goals, then operations on the numbers must take variable amounts of time even with hardware assistance, and you have to start to manage arrays not as pure numeric but rather as containing descriptions of each number and where the actual storage for it is. An operation on a 450 digit number may take more than 30 times as long as an operation on a number represented as fixed 15 digits.
Nearly all general purpose computer hardware these days uses fixed-length binary representation, whether 32 bits or 64 bits or 80 bits or (uncommon) 128 bits. This kind of binary representation cannot, however, exactly represent exactly 1/10th, for the same mathematical reasons why 10 digit decimal cannot exactly represent 1/3. 0.888 is not stored as 888/1000 in binary hardware.
IBM has a line of hardware that supports decimal floating point, such as for banking: it can be done, it just isn't common. And it just trades off for a different set of values not being exactly represented.
... and of course if you do switch to something like a variable-length representation as the ratio of two integers, complete with performance drop and storage increase... then you still cannot represent π or sqrt(2)...