2.2.1.1 RationalField Objects

class RationalField
The class RationalField represents the field $ \mathbf{Q}$ of rational numbers.
RationalField( )

We create the rational numbers $ \mathbf{Q}$ , and call a few functions:

sage: Q = RationalField(); Q
Rational Field
sage: Q.characteristic()
0
sage: Q.is_field()
True
sage: Q.zeta()
-1

We next illustrate arithmetic in $ \mathbf{Q}$ .

sage: Q('49/7')
7
sage: type(Q('49/7'))
<type '_rational.Rational'>
sage: a = Q('19/374'); b = frac(17, 371); print a, b
19/374 17/371
sage: a + b
13407/138754
sage: b + a
13407/138754
sage: a * b
19/8162
sage: b * a
19/8162
sage: a - b
691/138754
sage: b - a
-691/138754
sage: a / b
7049/6358
sage: b / a
6358/7049
sage: b < a
True
sage: a < b
False

Next finally illustrate arithmetic with automatic coercion. The types that coerce into the rational field include str, int, long, Integer.

sage: a + '17/371'
13407/138754
sage: a * 374
19
sage: 374 * a
19
sage: a/19
1/374
sage: a + 1
393/374

Instances of class RationalField have the following methods (in addition to inherited methods and special methods):

characteristic,$  $ gen,$  $ is_atomic_repr,$  $ is_field,$  $ name,$  $ ngens,$  $ random,$  $ zeta

These methods are defined as follows:

characteristic( )

gen( )

is_atomic_repr( )

is_field( )

name( )

ngens( )

random( [num_bound=1], [den_bound=1])

zeta( )

Instances of class RationalField also have the following special methods:

__call__( x)

__cmp__( other)

__contains__( x)

See About this document... for information on suggesting changes.