calculation with numbers with maximum 3 nonzero digits gives a result with more then 3 nonzero digits

can someone explain me why a calculation with numbers with maximum 3 nonzero digits gives a result with more then 3 nonzero digits:
num2str(0.541*16.0,'%2.30f')
ans =
8.656000000000000600000000000000
Thanks!

 Accepted Answer

This is the limitation of using limited number of binary numbers to represent a real-word value. Even with double precision (8 bytes or 64 bits) floating point data type, it's still impossible to represent every real-world value without errors.
>> num2str(0.541,'%2.30f')
ans =
0.541000000000000040000000000000
>> num2str(16,'%2.30f')
ans =
16.000000000000000000000000000000

3 Comments

Yep.
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Thank you for the answer. By the IEEE 754 norm, shouldn’t matlab or the computer be able to use exact representations and do exact simple operations also, with some real numbers with small finite representations in base 10 and not do it all the time with their representations in base 2?
Look, for example, at MATLAB trying to do a base 2 representation of 0.1:
0.10000000000000001000
0.20000000000000001000
0.40000000000000002000
0.80000000000000004000
1.60000000000000010000
1.20000000000000020000
0.40000000000000036000
0.80000000000000071000
1.60000000000000140000
1.20000000000000280000
0.40000000000000568000
0.80000000000001137000
1.60000000000002270000
1.20000000000004550000
0.40000000000009095000
0.80000000000018190000
1.60000000000036380000
1.20000000000072760000
0.40000000000145519000
0.80000000000291038000
1.60000000000582080000
1.20000000001164150000
0.40000000002328306000
0.80000000004656613000
1.60000000009313230000
1.20000000018626450000
0.40000000037252903000
0.80000000074505806000
1.60000000149011610000
1.20000000298023220000
0.40000000596046448000
0.80000001192092896000
1.60000002384185790000
1.20000004768371580000
0.40000009536743164000
0.80000019073486328000
1.60000038146972660000
1.20000076293945310000
0.40000152587890625000
0.80000305175781250000
1.60000610351562500000
1.20001220703125000000
0.40002441406250000000
0.80004882812500000000
1.60009765625000000000
1.20019531250000000000
0.40039062500000000000
0.80078125000000000000
1.60156250000000000000
1.20312500000000000000
0.40625000000000000000
0.81250000000000000000
1.62500000000000000000
1.25000000000000000000
0.50000000000000000000
1.00000000000000000000
0.00000000000000000000
0.00000000000000000000
0.00000000000000000000
0.00000000000000000000
0.00000000000000000000
my problem is that a guy with a pocket computer did it better, without errors and even correct me.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!