return the numerator and denimentor

2 vues (au cours des 30 derniers jours)
Fatma Alnabrisi
Fatma Alnabrisi le 9 Déc 2019
Commenté : Fatma Alnabrisi le 9 Déc 2019
Hi everyone
Is there such a way to return the numerator and the denomenator of a given rational number
for example x = 1/2
I want to get n=1 , d=2

Réponse acceptée

John D'Errico
John D'Errico le 9 Déc 2019
Rat gives rational results for a floating point number, sometimes they are only approximate, of course.
[n,d] = rat(1/2)
n =
1
d =
2
[n,d] = rat(pi)
n =
355
d =
113

Plus de réponses (1)

Matt J
Matt J le 9 Déc 2019
Modifié(e) : Matt J le 9 Déc 2019
If x is a symbolic variable, then yes
>> x=sym(1/2);
>> [n,d]=numden(x)
n =
1
d =
2

Community Treasure Hunt

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

Start Hunting!

Translated by