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

86 visualizaciones (últimos 30 días)
I want to input this fractions i.e. 1/2+1/3.. How can i get answer in same format i.e. in fractions?

Respuestas (2)

dpb
dpb el 17 de Sept. de 2015
>> format rat
>> 1/2+1/3
ans =
5/6
>> format short
>> 1/2+1/3
ans =
0.8333
>>
  2 comentarios
Steven Lord
Steven Lord el 2 de Abr. de 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

Iniciar sesión para comentar.


Ianah Nicole
Ianah Nicole el 2 de Mayo de 2025
1/2

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by