Weird answer for function when dividing two fractions
2 ビュー (過去 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!