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
Head[a]
Head[b]
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]
Head[b]
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?
Rational[33/10, 8/5] // NumericQ
givesFalse
. $\endgroup$