# Rational number

In mathematics, a rational number is a number that can be expressed as the quotient or fraction ⁠${\tfrac {p}{q}}$⁠ of two integers, a numerator p and a non-zero denominator q. For example, ⁠${\tfrac {3}{7}}$⁠ is a rational number, as is every integer (for example, $-5={\tfrac {-5}{1}}$). The set of all rational numbers, also referred to as "the rationals", the field of rationals or the field of rational numbers is usually denoted by boldface Q, or blackboard bold ⁠$\mathbb {Q} .$⁠
A rational number is a real number. The real numbers that are rational are those whose decimal expansion either terminates after a finite number of digits (example: 3/4 = 0.75), or eventually begins to repeat the same finite sequence of digits over and over (example: 9/44 = 0.20454545...). This statement is true not only in base 10, but also in every other integer base, such as the binary and hexadecimal ones (see Repeating decimal § Extension to other bases).
A real number that is not rational is called irrational. Irrational numbers include the square root of 2 (⁠${\sqrt {2}}$⁠), π, e, and the golden ratio (φ). Since the set of rational numbers is countable, and the set of real numbers is uncountable, almost all real numbers are irrational.
Rational numbers can be formally defined as equivalence classes of pairs of integers (p, q) with q ≠ 0, using the equivalence relation defined as follows:
    $(p_{1},q_{1})\sim (p_{2},q_{2})\iff p_{1}q_{2}=p_{2}q_{1}.$
The fraction ⁠${\tfrac {p}{q}}$⁠ then denotes the equivalence class of (p, q).
Rational numbers together with addition and multiplication form a field which contains the integers, and is contained in any field containing the integers. In other words, the field of rational numbers is a prime field, and a field has characteristic zero if and only if it contains the rational numbers as a subfield. Finite extensions of ⁠$\mathbb {Q}$⁠ are called algebraic number fields, and the algebraic closure of ⁠$\mathbb {Q}$⁠ is the field of algebraic numbers.
In mathematical analysis, the rational numbers form a dense subset of the real numbers. The real numbers can be constructed from the rational numbers by completion, using Cauchy sequences, Dedekind cuts, or infinite decimals (see Construction of the real numbers).

## Terminology

In mathematics, "rational" is often used as a noun abbreviating "rational number". The adjective rational sometimes means that the coefficients are rational numbers. For example, a rational point is a point with rational coordinates (i.e., a point whose coordinates are rational numbers); a rational matrix is a matrix of rational numbers; a rational polynomial may be a polynomial with rational coefficients, although the term "polynomial over the rationals" is generally preferred, to avoid confusion between "rational expression" and "rational function" (a polynomial is a rational expression and defines a rational function, even if its coefficients are not rational numbers). However, a rational curve is not a curve defined over the rationals, but a curve which can be parameterized by rational functions.

### Etymology

Although nowadays rational numbers are defined in terms of ratios, the term rational is not a derivation of ratio. On the contrary, it is ratio that is derived from rational: the first use of ratio with its modern meaning was attested in English about 1660, while the use of rational for qualifying numbers appeared almost a century earlier, in 1570. This meaning of rational came from the mathematical meaning of irrational, which was first used in 1551, and it was used in "translations of Euclid (following his peculiar use of ἄλογος)".
This unusual history originated in the fact that ancient Greeks "avoided heresy by forbidding themselves from thinking of those [irrational] lengths as numbers". So such lengths were irrational, in the sense of illogical, that is "not to be spoken about" (ἄλογος in Greek).

## Arithmetic
### Irreducible fraction

Every rational number may be expressed in a unique way as an irreducible fraction ⁠${\tfrac {a}{b}},$⁠ where a and b are coprime integers and b > 0. This is often called the canonical form of the rational number.
Starting from a rational number ⁠${\tfrac {a}{b}},$⁠ its canonical form may be obtained by dividing a and b by their greatest common divisor, and, if b < 0, changing the sign of the resulting numerator and denominator.

### Embedding of integers

Any integer n can be expressed as the rational number ⁠${\tfrac {n}{1}},$⁠ which is its canonical form as a rational number.

### Equality

    ${\frac {a}{b}}={\frac {c}{d}}$ if and only if $ad=bc$
If both fractions are in canonical form, then:
    ${\frac {a}{b}}={\frac {c}{d}}$ if and only if $a=c$ and $b=d$

### Ordering

If both denominators are positive (particularly if both fractions are in canonical form):
    ${\frac {a}{b}}<{\frac {c}{d}}$ if and only if $ad<bc.$
On the other hand, if either denominator is negative, then each fraction with a negative denominator must first be converted into an equivalent form with a positive denominator—by changing the signs of both its numerator and denominator.

### Addition

Two fractions are added as follows:
    ${\frac {a}{b}}+{\frac {c}{d}}={\frac {ad+bc}{bd}}.$
If both fractions are in canonical form, the result is in canonical form if and only if b, d are coprime integers.

### Subtraction

    ${\frac {a}{b}}-{\frac {c}{d}}={\frac {ad-bc}{bd}}.$
If both fractions are in canonical form, the result is in canonical form if and only if b, d are coprime integers.

### Multiplication

The rule for multiplication is:
    ${\frac {a}{b}}\cdot {\frac {c}{d}}={\frac {ac}{bd}}.$
where the result may be a reducible fraction—even if both original fractions are in canonical form.

### Inverse

Every rational number ⁠${\tfrac {a}{b}}$⁠ has an additive inverse, often called its opposite,
    $-\left({\frac {a}{b}}\right)={\frac {-a}{b}}.$
If ⁠${\tfrac {a}{b}}$⁠ is in canonical form, the same is true for its opposite.
A nonzero rational number ⁠${\tfrac {a}{b}}$⁠ has a multiplicative inverse, also called its reciprocal,
    $\left({\frac {a}{b}}\right)^{-1}={\frac {b}{a}}.$
If ⁠${\tfrac {a}{b}}$⁠ is in canonical form, then the canonical form of its reciprocal is either ⁠${\tfrac {b}{a}}$⁠ or ⁠${\tfrac {-b}{-a}},$⁠ depending on the sign of a.

### Division

If b, c, d are nonzero, the division rule is
    ${\frac {\,{\dfrac {a}{b}}\,}{\dfrac {c}{d}}}={\frac {ad}{bc}}.$
Thus, dividing ⁠${\tfrac {a}{b}}$⁠ by ⁠${\tfrac {c}{d}}$⁠ is equivalent to multiplying ⁠${\tfrac {a}{b}}$⁠ by the reciprocal of ⁠${\tfrac {c}{d}}:$⁠
    ${\frac {ad}{bc}}={\frac {a}{b}}\cdot {\frac {d}{c}}.$

### Exponentiation to integer power

If n is a non-negative integer, then
    $\left({\frac {a}{b}}\right)^{n}={\frac {a^{n}}{b^{n}}}.$
The result is in canonical form if the same is true for ⁠${\tfrac {a}{b}}.$⁠ In particular,
    $\left({\frac {a}{b}}\right)^{0}=1.$
If a ≠ 0, then
    $\left({\frac {a}{b}}\right)^{-n}={\frac {b^{n}}{a^{n}}}.$
If ⁠${\tfrac {a}{b}}$⁠ is in canonical form, the canonical form of the result is ⁠${\tfrac {b^{n}}{a^{n}}}$⁠ if a > 0 or n is even. Otherwise, the canonical form of the result is ⁠${\tfrac {-b^{n}}{-a^{n}}}.$⁠

## Continued fraction representation

A finite continued fraction is an expression such as
    $a_{0}+{\cfrac {1}{a_{1}+{\cfrac {1}{a_{2}+{\cfrac {1}{\ddots +{\cfrac {1}{a_{n}}}}}}}}},$
where a$_n$ are integers. Every rational number ⁠${\tfrac {a}{b}}$⁠ can be represented as a finite continued fraction, whose coefficients a$_n$ can be determined by applying the Euclidean algorithm to (a, b).

## Other representations

* common fraction: ⁠${\tfrac {8}{3}}$⁠
* mixed numeral: ⁠$2{\tfrac {2}{3}}$⁠
* repeating decimal using a vinculum: $2.{\overline {6}}$
* repeating decimal using parentheses: $2.(6)$
* continued fraction using traditional typography: $2+{\tfrac {1}{1+{\tfrac {1}{2}}}}$
* continued fraction in abbreviated notation: $[2;1,2]$
* Egyptian fraction: $2+{\tfrac {1}{2}}+{\tfrac {1}{6}}$
* prime power decomposition: $2^{3}\times 3^{-1}$
* quote notation: $3'6$
are different ways to represent the same rational value.

## Formal construction

The rational numbers may be built as equivalence classes of ordered pairs of integers.
More precisely, let ⁠$(\mathbb {Z} \times (\mathbb {Z} \setminus \{0\}))$⁠ be the set of the pairs (m, n) of integers such n ≠ 0. An equivalence relation is defined on this set by
    $(m_{1},n_{1})\sim (m_{2},n_{2})\iff m_{1}n_{2}=m_{2}n_{1}.$
Addition and multiplication can be defined by the following rules:
    $(m_{1},n_{1})+(m_{2},n_{2})\equiv (m_{1}n_{2}+n_{1}m_{2},n_{1}n_{2}),$
    $(m_{1},n_{1})\times (m_{2},n_{2})\equiv (m_{1}m_{2},n_{1}n_{2}).$
This equivalence relation is a congruence relation, which means that it is compatible with the addition and multiplication defined above; the set of rational numbers ⁠$\mathbb {Q}$⁠ is the defined as the quotient set by this equivalence relation, ⁠$(\mathbb {Z} \times (\mathbb {Z} \backslash \{0\}))/\sim ,$⁠ equipped with the addition and the multiplication induced by the above operations. (This construction can be carried out with any integral domain and produces its field of fractions.)
The equivalence class of a pair (m, n) is denoted ⁠${\tfrac {m}{n}}.$⁠
Two pairs (m$_1$, n$_1$) and (m$_2$, n$_2$) belong to the same equivalence class (that is are equivalent) if and only if
    $m_{1}n_{2}=m_{2}n_{1}.$
This means that
    ${\frac {m_{1}}{n_{1}}}={\frac {m_{2}}{n_{2}}}$
if and only if
    $m_{1}n_{2}=m_{2}n_{1}.$
Every equivalence class ⁠${\tfrac {m}{n}}$⁠ may be represented by infinitely many pairs, since
    $\cdots ={\frac {-2m}{-2n}}={\frac {-m}{-n}}={\frac {m}{n}}={\frac {2m}{2n}}=\cdots .$
Each equivalence class contains a unique canonical representative element. The canonical representative is the unique pair (m, n) in the equivalence class such that m and n are coprime, and n > 0. It is called the representation in lowest terms of the rational number.
The integers may be considered to be rational numbers identifying the integer n with the rational number ⁠${\tfrac {n}{1}}.$⁠
A total order may be defined on the rational numbers, that extends the natural order of the integers. One has
    ${\frac {m_{1}}{n_{1}}}\leq {\frac {m_{2}}{n_{2}}}$
If
    ${\begin{aligned}&(n_{1}n_{2}>0\quad {\text{and}}\quad m_{1}n_{2}\leq n_{1}m_{2})\\&\qquad {\text{or}}\\&(n_{1}n_{2}<0\quad {\text{and}}\quad m_{1}n_{2}\geq n_{1}m_{2}).\end{aligned}}$

## Properties

The set ⁠$\mathbb {Q}$⁠ of all rational numbers, together with the addition and multiplication operations shown above, forms a field.
⁠$\mathbb {Q}$⁠ has no field automorphism other than the identity. (A field automorphism must fix 0 and 1; as it must fix the sum and the difference of two fixed elements, it must fix every integer; as it must fix the quotient of two fixed elements, it must fix every rational number, and is thus the identity.)
⁠$\mathbb {Q}$⁠ is a prime field, which is a field that has no subfield other than itself. The rationals are the smallest field with characteristic zero. Every field of characteristic zero contains a unique subfield isomorphic to ⁠$\mathbb {Q} .$⁠
With the order defined above, ⁠$\mathbb {Q}$⁠ is an ordered field that has no subfield other than itself, and is the smallest ordered field, in the sense that every ordered field contains a unique subfield isomorphic to ⁠$\mathbb {Q} .$⁠
⁠$\mathbb {Q}$⁠ is the field of fractions of the integers ⁠$\mathbb {Z} .$⁠ The algebraic closure of ⁠$\mathbb {Q} ,$⁠ i.e. the field of roots of rational polynomials, is the field of algebraic numbers.
The rationals are a densely ordered set: between any two rationals, there sits another one, and, therefore, infinitely many other ones. For example, for any two fractions such that
    ${\frac {a}{b}}<{\frac {c}{d}}$
(where $b,d$ are positive), we have
    ${\frac {a}{b}}<{\frac {a+c}{b+d}}<{\frac {c}{d}}.$
Any totally ordered set which is countable, dense (in the above sense), and has no least or greatest element is order isomorphic to the rational numbers.

### Countability

The set of positive rational numbers is countable, as is illustrated in the figure.
More pecisely, one can sort the fractions by increasing values of the sum of the numerator and the denominator, and, for equal sums, by increasing numerator or denominator. This produces a sequence of fractions, from which one can remove the reducible fractions (in red on the figure), for getting a sequence that contains each rational number exactly once. This establishes a bijection between the rational numbers and the natural numbers, which maps each rational number to its rank in the sequence.
A similar method can be used for numbering all rational numbers (positive and negative).
As the set of all rational numbers is countable, and the set of all real numbers (as well as the set of irrational numbers) is uncountable, the set of rational numbers is a null set, that is, almost all real numbers are irrational, in the sense of Lebesgue measure.

## Real numbers and topological properties

The rationals are a dense subset of the real numbers; every real number has rational numbers arbitrarily close to it. A related property is that rational numbers are the only numbers with finite expansions as regular continued fractions.
In the usual topology of the real numbers, the rationals are neither an open set nor a closed set.
By virtue of their order, the rationals carry an order topology. The rational numbers, as a subspace of the real numbers, also carry a subspace topology. The rational numbers form a metric space by using the absolute difference metric $d(x,y)=|x-y|,$ and this yields a third topology on ⁠$\mathbb {Q} .$⁠ All three topologies coincide and turn the rationals into a topological field. The rational numbers are an important example of a space which is not locally compact. The rationals are characterized topologically as the unique countable metrizable space without isolated points. The space is also totally disconnected. The rational numbers do not form a complete metric space, and the real numbers are the completion of ⁠$\mathbb {Q}$⁠ under the metric $d(x,y)=|x-y|$ above.

## p-adic numbers

In addition to the absolute value metric mentioned above, there are other metrics which turn ⁠$\mathbb {Q}$⁠ into a topological field:
Let p be a prime number and for any non-zero integer a, let $|a|_{p}=p^{-n},$ where p$^n$ is the highest power of p dividing a.
In addition set $|0|_{p}=0.$ For any rational number ⁠${\frac {a}{b}},$⁠ we set
    $\left|{\frac {a}{b}}\right|_{p}={\frac {|a|_{p}}{|b|_{p}}}.$
Then
    $d_{p}(x,y)=|x-y|_{p}$
defines a metric on ⁠$\mathbb {Q} .$⁠
The metric space ⁠$(\mathbb {Q} ,d_{p})$⁠ is not complete, and its completion is the p-adic number field ⁠$\mathbb {Q} _{p}.$⁠ Ostrowski's theorem states that any non-trivial absolute value on the rational numbers ⁠$\mathbb {Q}$⁠ is equivalent to either the usual real absolute value or a p-adic absolute value.