Weird answer for function when dividing two fractions

5 vues (au cours des 30 derniers jours)
Katy Soto
Katy Soto le 4 Avr 2019
Modifié(e) : Robert U le 5 Avr 2019
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

Réponse acceptée

Robert U
Robert U le 4 Avr 2019
Modifié(e) : Robert U le 5 Avr 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

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by