floor(0.2835*10000) = 2834 WHY???

1 view (last 30 days)
Shiqin Su
Shiqin Su on 8 Dec 2017
Commented: James Tursa on 8 Dec 2017
floor(0.2835*10000) = 2834 WHY???
Jan on 8 Dec 2017
Because the Matlab is a deterministic system, you can examine the effect by your own. If floor(0.2835*10000) replies 2834, the value of 0.2835*10000 must be smaller than 2835. Check this:
0.2835 * 10000 - 2835
You will find out, that the actual question does not concern floor(), but either the manually defined "0.2835" or the multiplication by 10000. The solution is given below: 0.2835 cannot be represented in binary form with limited precision exactly.
Welcome to the world of numerics!

Sign in to comment.

Accepted Answer

Birdman on 8 Dec 2017
Edited: Birdman on 8 Dec 2017
Because actually, 0.2835*10000 is never exactly 2835, it is 2834.9.... This happens due to the usage of IEEE 754 double precision standard. Since floor means rounding toward negative infinity, the result is 2834.
Edit: answer is edited after Guillaume's comment.
James Tursa
James Tursa on 8 Dec 2017
Just to complete the thread, the exact conversions are:
>> num2strexact(0.2835)
ans =
>> num2strexact(0.2835*10000)
ans =

Sign in to comment.

More Answers (1)

Andreas Goser
Andreas Goser on 8 Dec 2017
The best answer I am aware of is posted here.


Community Treasure Hunt

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

Start Hunting!