CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

Path: gap4r8 / doc / ref / chap17.txt
Views: 418346
1
2
17 Rational Numbers
3
4
The rationals form a very important field. On the one hand it is the
5
quotient field of the integers (see chapter 14). On the other hand it is the
6
prime field of the fields of characteristic zero (see chapter 60).
7
8
The former comment suggests the representation actually used. A rational is
9
represented as a pair of integers, called numerator and denominator.
10
Numerator and denominator are reduced, i.e., their greatest common divisor
11
is 1. If the denominator is 1, the rational is in fact an integer and is
12
represented as such. The numerator holds the sign of the rational, thus the
13
denominator is always positive.
14
15
Because the underlying integer arithmetic can compute with arbitrary size
16
integers, the rational arithmetic is always exact, even for rationals whose
17
numerators and denominators have thousands of digits.
18
19
 Example 
20
gap> 2/3;
21
2/3
22
gap> 66/123; # numerator and denominator are made relatively prime
23
22/41
24
gap> 17/-13; # the numerator carries the sign;
25
-17/13
26
gap> 121/11; # rationals with denominator 1 (when canceled) are integers
27
11
28

29
30
31
17.1 Rationals: Global Variables
32
33
17.1-1 Rationals
34
35
Rationals global variable
36
IsRationals( obj )  filter
37
38
Rationals is the field â„š of rational integers, as a set of cyclotomic
39
numbers, see Chapter 18 for basic operations, Functions for the field
40
Rationals can be found in the chapters 58 and 60.
41
42
IsRationals returns true for a prime field that consists of cyclotomic
43
numbers –for example the GAP object Rationals– and false for all other GAP
44
objects.
45
46
 Example 
47
gap> Size( Rationals ); 2/3 in Rationals;
48
infinity
49
true
50

51
52
53
17.2 Elementary Operations for Rationals
54
55
17.2-1 IsRat
56
57
IsRat( obj )  Category
58
59
Every rational number lies in the category IsRat, which is a subcategory of
60
IsCyc (18.1-3).
61
62
 Example 
63
gap> IsRat( 2/3 );
64
true
65
gap> IsRat( 17/-13 );
66
true
67
gap> IsRat( 11 );
68
true
69
gap> IsRat( IsRat ); # `IsRat' is a function, not a rational
70
false
71

72
73
17.2-2 IsPosRat
74
75
IsPosRat( obj )  Category
76
77
Every positive rational number lies in the category IsPosRat.
78
79
17.2-3 IsNegRat
80
81
IsNegRat( obj )  Category
82
83
Every negative rational number lies in the category IsNegRat.
84
85
17.2-4 NumeratorRat
86
87
NumeratorRat( rat )  function
88
89
NumeratorRat returns the numerator of the rational rat. Because the
90
numerator holds the sign of the rational it may be any integer. Integers are
91
rationals with denominator 1, thus NumeratorRat is the identity function for
92
integers.
93
94
 Example 
95
gap> NumeratorRat( 2/3 );
96
2
97
gap> # numerator and denominator are made relatively prime:
98
gap> NumeratorRat( 66/123 );
99
22
100
gap> NumeratorRat( 17/-13 ); # numerator holds the sign of the rational
101
-17
102
gap> NumeratorRat( 11 ); # integers are rationals with denominator 1
103
11
104

105
106
17.2-5 DenominatorRat
107
108
DenominatorRat( rat )  function
109
110
DenominatorRat returns the denominator of the rational rat. Because the
111
numerator holds the sign of the rational the denominator is always a
112
positive integer. Integers are rationals with the denominator 1, thus
113
DenominatorRat returns 1 for integers.
114
115
 Example 
116
gap> DenominatorRat( 2/3 );
117
3
118
gap> # numerator and denominator are made relatively prime:
119
gap> DenominatorRat( 66/123 );
120
41
121
gap> # the denominator holds the sign of the rational:
122
gap> DenominatorRat( 17/-13 );
123
13
124
gap> DenominatorRat( 11 ); # integers are rationals with denominator 1
125
1
126

127
128
17.2-6 Rat
129
130
Rat( elm )  attribute
131
132
Rat returns a rational number rat whose meaning depends on the type of elm.
133
134
If elm is a string consisting of digits '0', '1', ..., '9' and '-' (at the
135
first position), '/' and the decimal dot '.' then rat is the rational
136
described by this string. The operation String (27.7-6) can be used to
137
compute a string for rational numbers, in fact for all cyclotomics.
138
139
 Example 
140
gap> Rat( "1/2" ); Rat( "35/14" ); Rat( "35/-27" ); Rat( "3.14159" );
141
1/2
142
5/2
143
-35/27
144
314159/100000
145

146
147
17.2-7 Random
148
149
Random( Rationals )  operation
150
151
Random for rationals returns pseudo random rationals which are the quotient
152
of two random integers. See the description of Random (14.2-12) for details.
153
(Also see Random (30.7-1).)
154
155
156