I want to input this fractions i.e. 1/2+1/3.. How can i get answer in same format i.e. in fractions?

73 ビュー (過去 30 日間)
I want to input this fractions i.e. 1/2+1/3.. How can i get answer in same format i.e. in fractions?

回答 (2 件)

dpb
dpb 2015 年 9 月 17 日
>> format rat
>> 1/2+1/3
ans =
5/6
>> format short
>> 1/2+1/3
ans =
0.8333
>>
  2 件のコメント
Steven Lord
Steven Lord 2024 年 4 月 2 日
Note that format rat may not give you the answer you expect once the numerator and denominator get too large.
N = 1e8;
format rat
X = (N-2^20)/N
X =
1038/1049
format longg
X
X =
0.98951424
y = 1038/1049
y =
0.989513822688275
Close, but not quite right.
If you wanted more control you could use the rat function to compute the numerator and denominator (perhaps with a tolerance) or use Symbolic Math Toolbox.
NS = sym(N);
XS = (NS-2^20)/NS
XS = 
[numeratorS, denominatorS] = numden(XS)
numeratorS = 
386529
denominatorS = 
390625
[numerator1, denominator1] = rat(X) % Default tolerance
numerator1 =
1038
denominator1 =
1049
[numerator2, denominator2] = rat(X, 0) % Tolerance of 0
numerator2 =
386529
denominator2 =
390625

サインインしてコメントする。


Ianah Nicole
Ianah Nicole 2025 年 5 月 2 日
1/2

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by