Tuesday, March 9, 2010

Fractions with numerator and denominator

I received a comment here.

The commentator asked me how to show a fraction as same as it was inserted.

For example:

x = 1/2;

disp(x)
1
-
2


So, I made a simple form for it.

-->function [x] = frac(n, d)
--> x = (n*%s)/(d*%s);
-->endfunction;

-->x1 = frac(1, 2);

-->disp(x1);

1
-
2

-->x2 = frac(2, 3);

-->disp(x2);

2
-
3

-->disp(x1 + x2);

7
-
6

-->disp(x1*x2);

2
-
6

-->x1 + 1
ans =

3
-
2

-->x1 + x2 - 2
ans =

- 5
-
6



PS.: The element %s is used in polynomial definitions.

Is it all right now?

1 comment:

rattikan saelim said...

Thank you, it's very helpful so far.