12.5 Field $ \mathbf{Q}$ of Rational Numbers

Module: sage.rings.rational_field

The class RationalField represents the field $ \mathbf{Q}$ of (arbitrary precision) rational numbers. Each rational number is an instance of the class Rational.

Module-level Functions

factor( x)

frac( n, d)

is_RationalField( x)

Class: RationalField

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

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 = Q('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

Functions: characteristic,$  $ complex_embedding,$  $ gen,$  $ gens,$  $ is_atomic_repr,$  $ is_field,$  $ is_finite,$  $ is_prime_field,$  $ ngens,$  $ number_field,$  $ order,$  $ random_element,$  $ zeta

characteristic( self)

Return 0, since the rational field has characteristic 0.

sage: c = QQ.characteristic(); c
0
sage: parent(c)
Integer Ring

is_field( self)

Return True, since the rational field is a field.

is_finite( self)

Return False, since the rational field is not finite.

order( self)

sage: QQ.order()
Infinity

random_element( self, [num_bound=1], [den_bound=1])

sage: QQ.random_element(10,10)
-5/3

Special Functions: __call__,$  $ __cmp__,$  $ __iter__,$  $ __repr__,$  $ _coerce_,$  $ _gap_init_,$  $ _is_valid_homomorphism_,$  $ _latex_,$  $ _magma_init_

__call__( self, x)

Coerce x into the field of rational numbers.

_gap_init_( self)

sage: gap(QQ)                     
Rationals

_magma_init_( self)

sage: magma(QQ)                       # optional
Rational Field

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