Weird answer for function when dividing two fractions

조회 수: 4 (최근 30일)
Katy Soto
Katy Soto 2019년 4월 4일
편집: Robert U 2019년 4월 5일
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

채택된 답변

Robert U
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by