How is the division of two numbers carried out in Matlab?

조회 수: 130 (최근 30일)
PE
PE 2018년 3월 23일
댓글: PE 2018년 3월 23일
The behavior is as expected when both the denominator and the numerator are in 'double' precision. If either of the two are integer, as in the example below, the rounding error yields unexpected division results.
double(475904) / double(512) = 929.5
int32(475904) / int32(512) = 930
int32(475904) / double(512) = 930
double(475904) / int32(512) = 930
  댓글 수: 4
PE
PE 2018년 3월 23일
Thanks. Here are two not so obvious learnings in Matlab- 1) Data operations that involve both integers result in an integer data type. 2) The division of two integers is rounded to nearest integer by default. If at all one would have to do this operation, idivide( int32(475904), int32(512) ) would be used.

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

답변 (1개)

John D'Errico
John D'Errico 2018년 3월 23일
편집: John D'Errico 2018년 3월 23일
Think about what the class of the result is. If you don't know how to use the class function to check that, then learn to read the output of the whos command. (In fact, in newer MATLAB releases, just displaying a variable at the command line tells you the class, if it is not a double.)
Then consider if the result is stored in an integer variable, then why would you expect a non-integer result? That is, an int32 cannot represent non-integers. So MATLAB stores only the integer part, essentially rounding to the nearest integer.
double(4) / int32(3)
ans =
int32
1
double(5) / int32(3)
ans =
int32
2
  댓글 수: 1
PE
PE 2018년 3월 23일
Thanks! This wasn't intuitive given the experience of other languages. But it seems it is clearly documented in Matlab Fundamentals. Thanks for the reference link.

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

카테고리

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