return the numerator and denimentor

조회 수: 2 (최근 30일)
Fatma Alnabrisi
Fatma Alnabrisi 2019년 12월 9일
댓글: Fatma Alnabrisi 2019년 12월 9일
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

채택된 답변

John D'Errico
John D'Errico 2019년 12월 9일
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개)

Matt J
Matt J 2019년 12월 9일
편집: Matt J 2019년 12월 9일
If x is a symbolic variable, then yes
>> x=sym(1/2);
>> [n,d]=numden(x)
n =
1
d =
2

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by