The **rationals** form a very important field. On the one hand it is the
quotient field of the integers (see Integers). On the other hand it is
the prime field of the fields of characteristic zero (see Subfields of
Cyclotomic Fields).

The former comment suggests the representation actually used. A rational
is represented as a pair of integers, called **numerator** and
**denominator**. Numerator and denominator are **reduced**, i.e., their
greatest common divisor is 1. If the denominator is 1, the rational is
in fact an integer and is represented as such. The numerator holds the
sign of the rational, thus the denominator is always positive.

Because the underlying integer arithmetic can compute with arbitrary size integers, the rational arithmetic is always exact, even for rationals whose numerators and denominators have thousands of digits.

gap> 2/3; 2/3 gap> 66/123; 22/41 # numerator and denominator are made relatively prime gap> 17/-13; -17/13 # the numerator carries the sign gap> 121/11; 11 # rationals with denominator 1 (after cancelling) are integers

The first sections of this chapter describe the functions that test whether an object is a rational (see IsRat), and select the numerator and denominator of a rational (see Numerator, Denominator).

The next sections describe the rational operations (see Comparisons of Rationals, and Operations for Rationals).

The **GAP3** object `Rationals`

is the field domain of all rationals. All
set theoretic functions are applicable to this domain (see chapter
Domains and Set Functions for Rationals). Since `Rationals`

is a
field all field functions are also applicable to this domain and its
elements (see chapter Fields and Field Functions for Rationals).

All external functions are defined in the file `"LIBNAME/rational.g"`

.

- IsRat
- Numerator
- Denominator
- Floor
- Mod1
- Comparisons of Rationals
- Operations for Rationals
- Set Functions for Rationals
- Field Functions for Rationals

`IsRat( `

`obj` )

`IsRat`

returns `true`

if `obj`, which can be an arbitrary object, is a
rational and `false`

otherwise. Integers are rationals with denominator
1, thus `IsRat`

returns `true`

for integers. `IsRat`

will signal an
error if `obj` is an unbound variable or a procedure call.

```
gap> IsRat( 2/3 );
true
gap> IsRat( 17/-13 );
true
gap> IsRat( 11 );
true
gap> IsRat( IsRat );
false #
````IsRat`

is a function, not a rational

`Numerator( `

`rat` )

`Numerator`

returns the numerator of the rational `rat`. Because the
numerator holds the sign of the rational it may be any integer. Integers
are rationals with denominator 1, thus `Numerator`

is the identity
function for integers.

gap> Numerator( 2/3 ); 2 gap> Numerator( 66/123 ); 22 # numerator and denominator are made relatively prime gap> Numerator( 17/-13 ); -17 # the numerator holds the sign of the rational gap> Numerator( 11 ); 11 # integers are rationals with denominator 1

`Denominator`

(see Denominator) is the counterpart to `Numerator`

.

`Denominator( `

`rat` )

`Denominator`

returns the denominator of the rational `rat`. Because the
numerator holds the sign of the rational the denominator is always a
positive integer. Integers are rationals with the denominator 1, thus
`Denominator`

returns 1 for integers.

gap> Denominator( 2/3 ); 3 gap> Denominator( 66/123 ); 41 # numerator and denominator are made relatively prime gap> Denominator( 17/-13 ); 13 # the denominator holds the sign of the rational gap> Denominator( 11 ); 1 # integers are rationals with denominator 1

`Numerator`

(see Numerator) is the counterpart to `Denominator`

.

`Floor(`

`r`)

This function returns the largest integer smaller or equal to `r`.

gap> Floor(-2/3); -1 gap> Floor(2/3); 0

`Mod1(`

`r`)

The argument should be a rational or a list. If `r` is a rational, it
returns `(Numerator(`

. If `r`) mod Denominator(`r`))/Denominator(r)`r` is a
list, it returns `List(`

. This function is very useful for working
in `r`,Mod1)*ℚ/ℤ*.

gap> Mod1([-2/3,-1,7/4,3]); [ 1/3, 0, 3/4, 0 ]

`q1` = `q2`

`q1` <> `q2`

The equality operator `=`

evaluates to `true`

if the two rationals `q1`
and `q2` are equal and to `false`

otherwise. The inequality operator
`<>`

evaluates to `true`

if the two rationals `q1` and `q2` are not
equal and to `false`

otherwise.

gap> 2/3 = -4/-6; true gap> 66/123 <> 22/41; false gap> 17/13 = 11; false

`q1` < `q2`

`q1` <= `q2`

`q1` > `q2`

`q1` >= `q2`

The operators `<`

, `<=`

, `>`

, and `=>`

evaluate to `true`

if the
rational `q1` is less than, less than or equal to, greater than, and
greater than or equal to the rational `q2` and to `false`

otherwise.

One rational *q _{1} = n_{1}/d_{1}* is less than another

gap> 2/3 < 22/41; false gap> -17/13 < 11; true

`q1` + `q2`

`q1` - `q2`

`q1` * `q2`

`q1` / `q2`

The operators `+`

, `-`

, `*`

and `/`

evaluate to the sum, difference,
product, and quotient of the two rationals `q1` and `q2`. For the
quotient `/`

`q2` must of course be nonzero, otherwise an error is
signalled. Either operand may also be an integer `i`, which is
interpreted as a rational with denominator 1. The result of those
operations is always reduced. If, after the reduction, the denominator
is 1, the rational is in fact an integer, and is represented as such.

gap> 2/3 + 4/5; 22/15 gap> 7/6 * 2/3; 7/9 # note how the result is cancelled gap> 67/6 - 1/6; 11 # the result is an integer

`q` ^ `i`

The powering operator `^`

returns the `i`-th power of the rational `q`.
`i` must be an integer. If the exponent `i` is zero,

is
defined as 1; if `q`^`i``i` is positive,

is defined as the `q`^`i``i`-fold
product

; finally, if `q`*`q`*..*`q``i` is negative,

is
defined as `q`^`i``(1/`

. In this case `q`)^-`i``q` must of course be nonzero.

gap> (2/3) ^ 3; 8/27 gap> (-17/13) ^ -1; -13/17 # note how the sign switched gap> (1/2) ^ -2; 4

As was already mentioned in the introduction of this chapter the **GAP3**
object `Rationals`

is the domain of all rationals. All set theoretic
functions, e.g., `Intersection`

and `Size`

, are applicable to this
domain.

```
gap> Intersection( Rationals, [ E(4)^0, E(4)^1, E(4)^2, E(4)^3 ] );
[ -1, 1 ] #
````E(4)`

is the complex square root of -1
gap> Size( Rationals );
"infinity"

This does not seem to be very useful.

As was already mentioned in the introduction of this chapter the **GAP3**
object `Rationals`

is the field of all rationals. All field functions,
e.g., `Norm`

and `MinPol`

are applicable to this domain and its elements.
However, since the field of rationals is the prime field, all those
functions are trivial. Therefore, `Conjugates( Rationals, `

returns
`q` )`[ `

, `q` ]`Norm( Rationals, `

and `q` )`Trace( Rationals, `

return
`q` )`q`, and `CharPol( Rationals, `

and `q` )`MinPol( Rationals, `

both
return `q` )`[ -`

.
`q`, 1 ]

gap3-jm

24 Jun 2022