I got a strange answer when I use Matlab to calculate the quotient of two decimal places
2 views (last 30 days)
Show older comments
远东 谢 on 13 Mar 2023
Commented: Dyuman Joshi on 13 Mar 2023
I tried to calculate 2.47/0.01,and I got the answer 247.0000. Then I used "ceil" to the answer, and to my surprise, I got the answer 248. This situation doesn't exist when I calculate 24.7/0.1 and 0.247/0.001.
Rik on 13 Mar 2023
Welcome to the world of floats and finite precision.
The reason is that some numbers can't be represented exactly by the double data type. The result you showed is displayed with only zeros, but apparently contains something in the decimals that aren't displayed. That is the reason you see .000 in the first place, as Matlab will show integers as integer.
The solution is to test against a tolerance. The eps function may be of use.
Dyuman Joshi on 13 Mar 2023
More Answers (0)
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!