Numerical Multiplication in MATLAB

조회 수: 20 (최근 30일)
BM
BM 2019년 10월 24일
댓글: Animesh Rastogi 2021년 10월 12일
Using MATLAB as a simple calculator, we get the results
2 * 0.155 * 100 = 31,
2 * 0.165 * 100 = 33,
2 * 0.135 * 100 = 27,
2 * 0.125 * 100 = 25
but the moment I compute the following
2 * 0.145 * 100 = 29.0000
I didn't have any variables in the memory, nor any settings enabled. These were on a blank canvas, but this particular value of 0.145 yields 29.0000 with four decimal places. I am a little unsure of why this value of 0.145 yields an answer with decimals, whereas the other calculations yield integers. Does anyone have an answer?

채택된 답변

John D'Errico
John D'Errico 2019년 10월 24일
편집: John D'Errico 2019년 10월 24일
So what is your problem? If you think the result should always be an exact integer, that just means you don't appreciate floating point arithmetic.
Perhaps you wonder why the last of those computations does give an exact integer. That happens because 0.125 is EXACTLY representable as a binary number, thus 2^-3. We could write it in the form of
0.00100000000000000000000000...
in binary bits.
So when you multiply it by 2*100, you turn the result into the exact integer 25.
The other results are NOT exactly representable in binary. For example, the decimal number 0.145 looks like
0.0010010100011110101110000101000111101011100001010001111...
as a repeating binary form, where each 1 in that expansion represents a negative power of 2.
This is no different from the fact that you cannot write the fraction 1/3 as a terminating decimal number.
That means you cannot expect all of those results to always be exact integers.
sprintf('%0.55f',0.145)
ans =
'0.1449999999999999900079927783735911361873149871826171875'
sprintf('%0.55f',2*0.145*100)
ans =
'28.9999999999999964472863211994990706443786621093750000000'
29 == 2*0.145*100
ans =
logical
0
In a binary form, we might write what MATLAB generates for 2*0.145*100 using this expansion:
11100.111111111111111111111111111111111111111111111111
Whereas we know that 29 is:
dec2bin(29)
ans =
'11101'
So the result is off by one bit down at the least significant bit of the number.
All of this is due, not to MATLAB, but to the IEEE representation of floating point numbers, used by most computing languages.
  댓글 수: 2
BM
BM 2019년 10월 24일
Hi John,
Thanks for your answer. This part of the code serves as a numerical check for my program. I guess I had been staring at analytical calculations so long that when this checking error was thrown up in the numerical calculation, its reconciliation through binary representation had completely escaped me at that moment.
Animesh Rastogi
Animesh Rastogi 2021년 10월 12일
0.155 is also not represented in binary exactly. Then why is it returning exact integer after multiplication?

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by