Long Division

조회 수: 16 (최근 30일)
Ben Davis
Ben Davis 2012년 3월 7일
편집: Walter Roberson 2019년 9월 10일
Hi im trying to write a function, that is basically long division, so i will get a value and then also a remainder. Could anybody give me some starting advice please?
function [q,r]=Divide(x,y)
Thats what my first line of my function shall be, where q is the integer value and r the remainder.
Thanks!
  댓글 수: 2
Jan
Jan 2012년 3월 7일
This is a double post. You got two answers to your former question already: http://www.mathworks.com/matlabcentral/answers/31403-writing-a-function-for-division
Ben Davis
Ben Davis 2012년 3월 7일
Hello, the answer you gave me is good but i need to somehow get it to work for when x is very large, so then q doesnt approximate:
For example i need my function to be able to perform commands like this:
[q,r]=Divide(2*ones(1,43),7)
But at the moment i cant get it too :\
got any tips? :)
Because for some reason when i call that on the command line i end up with q as =0 and r as 43 zero's in a line.
When the answer should be :
q = Columns 1 through 22
3 1 7 4 6 0 3 1 7 4 6 0 3 1 7 4 6 0 3 1 7 4
Columns 23 through 42
6 0 3 1 7 4 6 0 3 1 7 4 6 0 3 1 7 4 6 0
r = 2

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

답변 (2개)

Walter Roberson
Walter Roberson 2012년 3월 7일
When you do division by hand, on a piece of paper, what you see is a vector of digits in the numerator and a vector of digits in the denominator. Humans do not usually perceive a number such as 2893 as a single unit: we see it as "two eight nine three". "long division" deals with the situation.
So, if someone gave you pencil and paper and two vectors of digits (numerator and denominator), and asked you to do the division, how would you proceed?
  댓글 수: 9
Walter Roberson
Walter Roberson 2012년 3월 7일
If you post what you have so far, someone might point out problems.
Ben Davis
Ben Davis 2012년 3월 8일
편집: Walter Roberson 2019년 9월 10일
I tried to follow your steps and program it in that order. I have missed out the sections:
'Compare the denominator vector to the leading digits of the numerator vector. Is the denominator greater than those leading digits? If it is then the result-digit for that position is 0, and add an extra 0 to the end of the numerator vector to achieve the effect of multiplying by 10, and return to the beginning of the loop.'
So ,so far i have this:
function [q,r]=Fivide(x,y)
f=length(x)-length(y);
g=zeros(1,f);
x=[g,x];
y=[g,y];
q=x/y;
q=floor(q);
r=x-(q*y);
p=length(q)
for a=p:-1:1;
if x==0;
r=0;
end
if length(y)>length(x)
q=x;
end
while x(1)==0;
y(1)==0;
x(1)=[];
y(1)=[];
end
end
end
I have attempted to what i think many work for the function other than the section that i have missed out, i know i have most likely made many mistakes. But i really am trying my best :\ any feedback would be great.

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


Daniel Shub
Daniel Shub 2012년 3월 7일
In your first comment to Walter, you wrote "and the process is repeated until an answer is obtained." That sounds a lot like a "while" loop to me. Think you could express what you do in terms of a while loop ...
Maybe: While the remainder is less than the denominator DO ...
Then from there think about how to write the do in pseudo code. Finally convert it to MATLAB

카테고리

Help CenterFile Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by