error (rounding or user) in simple math op

I just noticed odd behavior in R2012b that really seems like an error though perhaps I'm making a foolish error?
I enter the following >> 2.7+1.8*[1,2,3,4,5,6,7,8,9]
I expect 4.5 6.3 8.1 9.9 11.7 13.5 15.3 17.1 18.9
I get: ans =
Columns 1 through 5
4.500000000000000 6.300000000000001 8.100000000000001 9.900000000000000 11.699999999999999
Columns 6 through 9
13.500000000000000 15.300000000000001 17.100000000000001 18.899999999999999
This seems odd to me for obvious reasons. Even something simple like
>> 1.8*9
ans =
16.199999999999999
how could this be happening?
Thank you
(p.s. its 8.0.0.783 R2012b)

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 8일
편집: Azzi Abdelmalek 2013년 1월 8일

0 개 추천

Use
format short
The problem is simply caused by the way, real data are stored in memory

추가 답변 (1개)

José-Luis
José-Luis 2013년 1월 8일

1 개 추천

댓글 수: 5

Jason
Jason 2013년 1월 8일
I expect 0 not to always be 0 but this is a bit much:
1.1*9999 ans = 1.099890000000000e+04
The result is off by 0.1
Jason
Jason 2013년 1월 8일
doh, never mind.
José-Luis
José-Luis 2013년 1월 8일
편집: José-Luis 2013년 1월 8일
That's just a matter of how you are displaying the number. Please try format long as Azzi suggests. The number should then be displayed with all its precision.
José-Luis
José-Luis 2013년 1월 9일
Please accept an answer if it helped you.
Jan
Jan 2013년 1월 9일
편집: Jan 2013년 1월 9일
This could be more helpful here:
format long g
But what is wrong with "1.1*9999 ans = 1.099890000000000e+04"? 1.1*9999 is 10998.9 or 1.09989e4. I cannot see why this should be "off by 0.1".

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

태그

Community Treasure Hunt

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

Start Hunting!

Translated by