필터 지우기
필터 지우기

I got a strange answer when I use Matlab to calculate the quotient of two decimal places

조회 수: 1 (최근 30일)
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
Rik 2023년 3월 13일
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.
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 3월 13일
Adding to Rik's point
y = 2.47/0.01
y = 247.0000
num2str(y, 32)
ans = '247.00000000000002842170943040401'
@远东 谢 you can see that the result of operation is not exactly 247 as one would expect.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by