How to get quotient value after division without round off?

조회 수: 60 (최근 30일)
Khushboo Singla
Khushboo Singla 2021년 6월 19일
답변: Steven Lord 2021년 6월 19일
I want to get quotient (only integer value) after division of two numbers without rounding off.
n1 = I1(i,j) +1;
for k1= 0:7
l1(8-k1) = rem(n1,2);
n1 = fix(n1/2);
end
when n1 = 157 in first line :
then in fourth line after dividing by 2 , n1 is taking 79 (157/2 = 78.5) value
but i want n1 to take 78 value.
  댓글 수: 2
Khushboo Singla
Khushboo Singla 2021년 6월 19일
There is one more doubt as::
In Command Window
when i write fix(157/2) gives answer as 78
but when i write fix(n1/2) gives answer as 79
and according to my code i want to write fix(n1/2) and wwant to get answer as 78 i.e quotient without round off !!
dpb
dpb 2021년 6월 19일
>> n1=157;
>> fix(n1/2)
ans =
78
>>
You've done something else you've not told us about first...like an intermediate store with a rounding operation, maybe.
Even in your code;
>> n1=157;for k1= 0:7
l1(8-k1) = rem(n1,2);
n1 = fix(n1/2)
end
n1 =
78
n1 =
39
n1 =
19
n1 =
9
n1 =
4
n1 =
2
n1 =
1
n1 =
0
>>

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

채택된 답변

Mouhamed Niasse
Mouhamed Niasse 2021년 6월 19일
Hello,
I tried to execute your portion of code but, just as expected, n1 has the value 78 not 79 for me.
Try using the function 'floor' instead of 'fix', they normally give the same output for positive input values.
  댓글 수: 1
Khushboo Singla
Khushboo Singla 2021년 6월 19일
Thankyou so much. Though same problem persisted in my code BUT i got another solution
as I subtracted remainder(from third line) from the number(n1) everytime so that it will give only interger value after division as shown:
n = 157;
for k1= 0:7
l1(8-k1) = rem(n1,2);
n1 = fix((n1-l1(8-k1))/2)
end

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

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 6월 19일
If one or both of the numbers are stored as an integer type you could use the idivide function.
idivide(int16(157),2)
ans = int16 78

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by