Why isn't three divided by two 1.5?

>> x=int16(3) % set x equal to the integer 3
x =
3
>> y=int16(2) % set y equal to the integer 2
y =
2
>> x/y % divide x by y
ans =
2

답변 (2개)

Joseph Cheng
Joseph Cheng 2015년 10월 5일

2 개 추천

because it's rounding due to your setting x and y as integers and not double or single precision.
Walter Roberson
Walter Roberson 2015년 10월 5일

0 개 추천

Division like that on integer data types is defined as being equivalent to converting the data to double, doing the operation, and then converting back to the integer data type. The conversion of double to integer data type is defined as one that rounds. 1.5 rounds to 2 so the result is int16(2)

카테고리

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

태그

질문:

2015년 10월 5일

답변:

2015년 10월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by