Skip to content

Rational

Rational is the head for the atomic rational type.

Attributes[Rational] := {Protected}

Simple examples

Rationals are created from Times when a rational form is encountered:

In[1]:= Head[(5)/(6)]
Out[1]= Rational
Which is equivalent to typing them in directly:
In[2]:= Head[(5)/(6)]
Out[2]= Rational
Or being even more explicit:
In[3]:= Head[Rational[5, 6]]
Out[3]= Rational
Rationals simplify on evaluation:
In[4]:= Rational[10, 6]
Out[4]= (5)/(3)
Which might include evaluating to an Integer:
In[5]:= Head[Rational[-100, 10]]
Out[5]= Integer
Rationals of non-Integer types are not allowed:
In[6]:= Rational[0, n]
Out[6]= Rational[0, n]

Further examples

Undefined rationals are handled accordingly:

In[1]:= Rational[0, 0]
Out[1]= Indeterminate
In[2]:= Rational[1, 0]
Out[2]= ComplexInfinity
Rational numbers have some special handling for pattern matching:
In[3]:= test = Rational[2, 3]
Out[3]= (2)/(3)
In[4]:= MatchQ[test, (2)/(3)]
Out[4]= True
In[5]:= MatchQ[test, Rational[a_Integer, b_Integer]]
Out[5]= True
In[6]:= ((2)/(3)) /. ((Rational[a_Integer, b_Integer] -> {a, b}))
Out[6]= {2, 3}
In[7]:= ((2)/(3)) /. (((a_Integer)/(b_Integer) -> {a, b}))
Out[7]= (2)/(3)