MATLAB Answers

return the numerator and denimentor

1 view (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 9 Dec 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

  1 Comment

Sign in to comment.

More Answers (1)

Matt J
Matt J on 9 Dec 2019
Edited: Matt J on 9 Dec 2019
If x is a symbolic variable, then yes
>> x=sym(1/2);
>> [n,d]=numden(x)
n =
1
d =
2

  1 Comment

Sign in to comment.

Sign in to answer this question.