How To:
Get the Fraction's Parts

Mobius Logo

(Tested in Mobius 09/18/2018)

Suppose you have a fraction and want to access the numerator and denominator. Mobius does not seem to have such a function.

Before going any further, note that you need to have an actual fraction. In particular defing a value like :

$X=5/7

will NOT allow you to extract the numerator and denominator, because Mobius immediately converts this to a decimal number. You need to define your fraction using frac or by a Maple call:

$X=frac(5,7);
or
$X=maple("5/7");

Maple DOES have functions to do this for you, they are denom and numer. To get the denominator and numerator of $X from Maple use the calls:

$DenX = maple("denom($X)");
$NumX=maple("numer($X)");

or if you want just one Maple call:

$Temp=maple("denom($X),numer($X)");
$ DenX=switch(0,$Temp);
$NumX=switch(1,$Temp);

You can save on Maple calls as follows. Suppose $X is our rational. The following code will get the denominator and numerator for you:

$DenX=sqrt(1/($X/$X));
$NumX=$DenX*$X;

Yes, that is ($X/$X)! You MUST use the brackets as shown, it does not work otherwise.

Why does this work? The fraction IS TREATED AS AN OPERATION STATEMENT, NOT A QUANTITY. Suppose then you have $X = 2/3 . Then

($X/$X) is evaluated as ((2/3)/2)/3 = 1/9 !

So to Mobius $X/$X is not 1 when $X is a fraction .

Notes:
  1. Both these methods work for integers, returning a denominator of 1 of course.
  2. For negative numbers the denominator is returned as a positive, the numerator as a negative.

How-To Index

Main Index

Updated: 09/18/2018 SMS/MUO