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

107 views (last 30 days)
I want to input this fractions i.e. 1/2+1/3.. How can i get answer in same format i.e. in fractions?

Answers (2)

dpb
dpb on 17 Sep 2015
>> format rat
>> 1/2+1/3
ans =
5/6
>> format short
>> 1/2+1/3
ans =
0.8333
>>
  2 Comments
Steven Lord
Steven Lord on 2 Apr 2024
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

Sign in to comment.


Ianah Nicole
Ianah Nicole on 2 May 2025
1/2

Tags

Products

Community Treasure Hunt

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

Start Hunting!