Weird answer for function when dividing two fractions
조회 수: 4 (최근 30일)
이전 댓글 표시
function c = c_div(r, s) % Divides vectors
% Compute c = r / s, where r, s and c are rational numbers defined as
% 2-vectors
c(1) = r(1) * s(2); % numerator
c(2) = r(2) * s(1); % denominator
c = c / gcd(c(1), c(2)); % using gcd to leave the c in its simplest form
end
I want to divide:
r = [4501700 1000]
s = [128 202042]
So I use myc_div(r, s) and the answer is:
ans =
1.0e+09 *
4.547662357000000 0.000000640000000
What does this mean as the answer should be 7105722.433
댓글 수: 0
채택된 답변
Robert U
2019년 4월 4일
편집: Robert U
2019년 4월 5일
Hi Katy Soto,
type the following to command line to prevent Matlab from shortening output:
format long g
Then output of your function is
c_div(r, s)
ans =
4547662357 640
This is the rational number in the same format as the input.
ans(1)/ans(2)
ans =
7105722.4328125
Kind regards,
Robert
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!