13.2.1.3 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 + Q('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,$  $ is_finite,$  $ ngens,$  $ number_field,$  $ random_element,$  $ zeta

Further documentation:

characteristic( )

Return 0, since the rational field has characteristic 0.

is_field( )

Return True, since the rational field is a field.

is_finite( )

Return False, since the rational field is not finite.

Instances of class RationalField also have the following special methods:

__call__,$  $ __cmp__,$  $ __contains__,$  $ __reduce__,$  $ __repr__,$  $ _coerce_,$  $ _latex_

Further documentation:

__call__( x)

Coerce x into the field of rational numbers.

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