# Representation of Rational Numbers

Following up on this question I would like to understand the way MMA represents rational numbers.

First of all, as Carl Woll suggested, x/y is not a simple rational number. Instead it is a much more convoluted sequence of operators. Indeed, as the documentation for Rational suggests, numbers entered in the form n/m only become Rational numbers on evaluation, and further, the unevaluated form is expressed in terms of Times and Power:

ClearAll[x,y]
x/y // FullForm


Times[x,Power[y,-1]]

Naively, we would expect it to be a rational number, but, following the a.m. logic, it's not:

Head[x/y]


Times

Accordingly, this evaluates to False

Rational[x,y]===x/y


Fine, there are not the same, because they are constructed differently. I am not sure I understand why this particular representation was chosen, so the question number one would be: why not simply /?

Moving forward, I would like to question the following sequence, which I don't understand:

FindInstance[Rational[x, y] =!= x/y, {x, y}, Integers]


{{x->33,y->16}}

a = Rational[33, 16]
b = 33/16


33/16

33/16

Rational

Rational

So, they are both rational, and they are the same

a===b


True

So, the question number two is what did FindInstance mean?

The plot thickens if we use the following command:

FindInstance[Rational[x, y] =!= x/y, {x, y}, Reals]


{{x->33/10,y->8/5}}

Let's substitute:

a = Rational[33/10, 8/5]
b = (33/10)/(8/5)


Rational[33/10,8/5]

33/16

Here are our 33 and 16 back! Well, let's see what they are closer:

Head[a]


Rational

Rational

However, not only they are not the same, they are not equal!

a===b
a==b


False

False

Well, in this case, we sort of expected this, as we explicitly asked for cases where they are not the same. But, they are! What happened to the idea that MMA should simplify 33/10 over 8/5 to 33 over 16? Even inside Rational. In the end, as Rational documentation suggests, this is just a representation of n/m, therefore, it should be simplified according to the elementary rules. But we can't even evaluate it:

Evaluate@a


Rational[33/10, 8/5]

So, the question number three is: what's going on here?

• Consider that Rational[33/10, 8/5] // NumericQ gives False. Commented Jan 24, 2017 at 2:04
• @m_goldberg Right, and I thought it is a strange solution, because Rational is defined as integer/integer, but FindInstance sort of suggested it. Anyways, even if so, outside of the fact that there should be no solution, I think it should at least be able to simplify it to the case of integer/integer when the arguments are rationals themselves. Commented Jan 24, 2017 at 2:20

Rational is for exact numeric values, not symbolic expressions, i.e. its arguments must be integers. Rational[x,y] is syntactically valid input but no more meaningful than foo[x,y].

FindInstance[foo[x, y] =!= x/y, {x, y}, Integers]

{{x -> 33, y -> 16}}


FindInstance["meaningless" =!= x/y, {x, y}, Integers] gives the same output. Why values 33 and 16 are chosen as a starting point I do not know.

In evaluated expressions Mathematica does not use Divide or Subtract, replacing these with "equivalents" using Times. I suppose this was chosen to reduce the number of rules needed for transformations but there are apparent consequences, e.g. see my own question:

• Setting RandomSeed to something different will of course change the result: FindInstance[foo[x, y] =!= x/y, {x, y}, Integers, RandomSeed -> 9] Commented Jan 24, 2017 at 3:30
• @J.M. That is great! Thank you. Commented Jan 24, 2017 at 3:35
• @Mr. Wizard Brilliant explanation. Thanks! I knew there is something there in that madness. Commented Jan 24, 2017 at 3:37

I am not sure why

FindInstance[Rational[x, y] =!= x/y, {x, y}, Reals]


returned

{{x -> 33/10, y -> 8/5}}

but that result certainly satisfies your query. But the result is not equivalent to asserting Rational[33/10, 8/5] is an answer to your query because Rational[33/10, 8/5] is semantic nonsense.

Further, I would argue that your query is ill-posed. You should have written

FindInstance[Rational[x, y] != x/y, {x, y}, Integers]


Then you would have gotten the message

Perhaps this would have been more meaningful to you.

• Right, I thought Rational can deal with any reasonable inputs, but apparently it is not designed for this. Commented Jan 24, 2017 at 3:39