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???
  3 Comments
Jan
Jan on 8 Dec 2017
Why???
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
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.
  2 Comments
James Tursa
James Tursa on 8 Dec 2017
Just to complete the thread, the exact conversions are:
>> num2strexact(0.2835)
ans =
0.28349999999999997424282582869636826217174530029296875
>> num2strexact(0.2835*10000)
ans =
2.83499999999999954525264911353588104248046875e3

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.

Tags

Community Treasure Hunt

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

Start Hunting!