Weird answer for function when dividing two fractions

2 views (last 30 days)
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

Accepted Answer

Robert U
Robert U on 4 Apr 2019
Edited: Robert U on 5 Apr 2019
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!