File: rational.msk

package info (click to toggle)
gap 4r4p12-2
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 29,584 kB
  • ctags: 7,113
  • sloc: ansic: 98,786; sh: 3,299; perl: 2,263; makefile: 498; asm: 63; awk: 6
file content (103 lines) | stat: -rw-r--r-- 2,971 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%A  rational.msk                GAP documentation            Martin Schoenert
%%
%A  @(#)$Id: rational.msk,v 1.8 2003/09/05 07:54:50 gap Exp $
%%
%Y  (C) 1998 School Math and Comp. Sci., University of St.  Andrews, Scotland
%Y  Copyright (C) 2002 The GAP Group
%%
\Chapter{Rational Numbers}

The *rationals* form a very important field.  On the one hand it is the
quotient field of the integers (see chapter~"Integers").
On the other hand it is the prime field of the fields of characteristic zero
(see chapter~"Abelian Number 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.

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

\Declaration{Rationals}
\beginexample
gap> Size( Rationals ); 2/3 in Rationals;
infinity
true
\endexample


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Section{Elementary Operations for Rationals}

\Declaration{IsRat}
\index{test!for a rational}
\beginexample
gap> IsRat( 2/3 );
true
gap> IsRat( 17/-13 );
true
gap> IsRat( 11 );
true
gap> IsRat( IsRat );  # `IsRat' is a function, not a rational
false
\endexample

\Declaration{IsPosRat}
\Declaration{IsNegRat}

\Declaration{NumeratorRat}
\index{numerator!of a rational}
\beginexample
gap> NumeratorRat( 2/3 );
2
gap> NumeratorRat( 66/123 );  # numerator and denominator are made relatively prime
22
gap> NumeratorRat( 17/-13 );  # the numerator holds the sign of the rational
-17
gap> NumeratorRat( 11 );      # integers are rationals with denominator 1
11
\endexample


\Declaration{DenominatorRat}
\index{denominator!of a rational}
\beginexample
gap> DenominatorRat( 2/3 );
3
gap> DenominatorRat( 66/123 );  # numerator and denominator are made relatively prime
41
gap> DenominatorRat( 17/-13 );  # the denominator holds the sign of the rational
13
gap> DenominatorRat( 11 );      # integers are rationals with denominator 1
1
\endexample

\Declaration{Rat}

\>Random( Rationals )!{for rationals}

`Random' for rationals returns pseudo random rationals which are the
quotient of two random integers. See the description of `Random' for integers
("Random!for integers") for details. (Also see~"Random".)