12.6 Rational Numbers

Module: sage.rings.rational

Author Log:

Module-level Functions

gmp_randrange( )

make_rational( s)

mathml( x)

Output x formated for inclusion in a MathML document.

rational_reconstruction( integer.Integer a, integer.Integer m)

Find the rational reconstruction of a mod m, if it exists.

INPUT:
    a -- Integer
    m -- Integer
OUTPUT:
    x -- rings.rational.Rational

Class: Rational

class Rational

Functions: additive_order,$  $ ceil,$  $ copy,$  $ denom,$  $ denominator,$  $ factor,$  $ floor,$  $ gcd,$  $ height,$  $ is_one,$  $ is_zero,$  $ lcm,$  $ mod_ui,$  $ multiplicative_order,$  $ numer,$  $ numerator,$  $ parent,$  $ set_si,$  $ set_str,$  $ sqrt,$  $ square_root,$  $ str,$  $ valuation

additive_order( self)

Return the additive order of self.

sage: QQ(0).additive_order()
1
sage: QQ(1).additive_order()
Infinity

ceil( self)

self.ceil(): Return the ceiling of this rational number.

If this rational number is an integer, this returns this number, otherwise it returns the floor of this number +1.

sage: n = 5/3; n.ceil()
2
sage: n = -17/19; n.ceil()
0
sage: n = -7/2; n.ceil()
-3
sage: n = 7/2; n.ceil()
4
sage: n = 10/2; n.ceil()
5

denom( self)

self.denom(): Return the denominator of this rational number.

sage: x = 5/13
sage: x.denom()
13
sage: x = -9/3
sage: x.denom()
1

denominator( self)

self.denominator(): Return the denominator of this rational number.

sage: x = -5/11
sage: x.denominator()
11
sage: x = 9/3
sage: x.denominator()
1

floor( self)

self.floor(): Return the floor of this rational number.

sage: n = 5/3; n.floor()
1
sage: n = -17/19; n.floor()
-1
sage: n = -7/2; n.floor()
-4
sage: n = 7/2; n.floor()
3
sage: n = 10/2; n.floor()
5

gcd( self, Rational other)

Return the least common multiple of self and other.

Our hopefully interesting notion of GCD for rational numbers is illustrated in the examples below.

sage: gcd(2/3,1/5)
1/15
sage: gcd(2/3,7/5)
1/15
sage: gcd(1/3,1/6)
1/6
sage: gcd(6/7,9/7)
3/7

height( self)

The max absolute value of the numerator and denominator of self, as an Integer.

sage: a = 2/3
sage: a.height()
3
sage: a = 34/3
sage: a.height()
34
sage: a = -97/4
sage: a.height()
97

Author: Naqi Jaffery (2006-03-05): examples

lcm( self, Rational other)

Return the least common multiple of self and other.

Our hopefully interesting notion of LCM for rational numbers is illustrated in the examples below.

sage: lcm(2/3,1/5)
2
sage: lcm(2/3,7/5)
14
sage: lcm(1/3,1/5)
1
sage: lcm(1/3,1/6)
1/3

multiplicative_order( self)

Return the multiplicative order of self, if self is a unit, or raise codeArithmeticError otherwise.

sage: QQ(1).multiplicative_order()
1
sage: QQ('1/-1').multiplicative_order()
2
sage: QQ(0).multiplicative_order()
Traceback (most recent call last):
...
ArithmeticError: no power of 0 is a unit
sage: QQ('2/3').multiplicative_order()
Traceback (most recent call last):
...
ArithmeticError: no power of 2/3 is a unit

numer( self)

Return the numerator of this rational number.

sage: x = -5/11
sage: x.numer()
-5

numerator( self)

Return the numerator of this rational number.

sage: x = 5/11
sage: x.numerator()
5

sage: x = 9/3
sage: x.numerator()
3

sqrt( self, bits=None)

Returns the positive square root of self as a real number to the given number of bits of precision if self is nonnegative, and raises a ValueError exception otherwise.

INPUT:
    bits -- number of bits of precision.
            If bits is not specified, the number of
            bits of precision is at least twice the
            number of bits of self (the precision
            is always at least 53 bits if not specified).
OUTPUT:
    integer, real number, or complex number.

sage: x = 23/2
sage: x.sqrt()
3.3911649915626341
sage: x = 32/5
sage: x.sqrt()
2.5298221281347035
sage: x = 16/9
sage: x.sqrt()
4/3
sage: x.sqrt(53)
1.3333333333333333            
sage: x = 9837/2
sage: x.sqrt()
70.132018365365752
sage: x = 645373/45
sage: x.sqrt()
119.75651223303984
sage: x = -12/5
sage: x.sqrt()
1.5491933384829668*I

Author: Naqi Jaffery (2006-03-05): examples

square_root( self)

Return the positive rational square root of self, or raises a ValueError if self is not a perfect square.

sage: x = 125/5
sage: x.square_root()
5
sage: x = 64/4
sage: x.square_root()
4
sage: x = 1000/10
sage: x.square_root()
10
sage: x = 81/3
sage: x.square_root()
Traceback (most recent call last):
...
ValueError: self (=27) is not a perfect square

Author: Naqi Jaffery (2006-03-05): examples

Special Functions: __abs__,$  $ __add_,$  $ __add__,$  $ __cmp__,$  $ __div_,$  $ __div__,$  $ __eq__,$  $ __float__,$  $ __ge__,$  $ __getitem__,$  $ __gt__,$  $ __int__,$  $ __invert__,$  $ __le__,$  $ __long__,$  $ __lshift__,$  $ __lt__,$  $ __mod__,$  $ __mul_,$  $ __mul__,$  $ __ne__,$  $ __neg__,$  $ __pos__,$  $ __pow__,$  $ __radd__,$  $ __rdiv__,$  $ __reduce__,$  $ __repr__,$  $ __rlshift__,$  $ __rmod__,$  $ __rmul__,$  $ __rpow__,$  $ __rrshift__,$  $ __rshift__,$  $ __rsub__,$  $ __set_value,$  $ __sub_,$  $ __sub__,$  $ _gcd,$  $ _im_gens_,$  $ _interface_init_,$  $ _latex_,$  $ _lcm,$  $ _lshift,$  $ _mathml_,$  $ _mpfr_,$  $ _pari_,$  $ _reduce_set,$  $ _rshift

__set_value( self, x)

sage: a = long(901824309821093821093812093810928309183091832091)
sage: b = QQ(a); b
901824309821093821093812093810928309183091832091
sage: QQ(b)
901824309821093821093812093810928309183091832091
sage: QQ(int(93820984323))
93820984323
sage: QQ(ZZ(901824309821093821093812093810928309183091832091))
901824309821093821093812093810928309183091832091
sage: QQ('-930482/9320842317')
-930482/9320842317
sage: QQ((-930482, 9320842317))
-930482/9320842317
sage: QQ([9320842317])
9320842317
sage: QQ(pari(39029384023840928309482842098430284398243982394))
39029384023840928309482842098430284398243982394
sage: QQ('sage')
Traceback (most recent call last):
...
TypeError: unable to convert sage to a rational

sage: QQ(RR(3929329/32))
3929329/32
sage: QQ(RR(1/7)) - 1/7
-1/126100789566373888
sage: QQ(23.2)
6530219459687219/281474976710656
sage: 6530219459687219.0/281474976710656
23.1999999999999993

_gcd( self, Rational other)

Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).

_interface_init_( self)

sage: kash(3/1).Type()              # optional
elt-fld^rat
sage: magma(3/1).Type()             # optional
FldRatElt

_lcm( self, Rational other)

Returns the least common multiple, in the rational numbers, of self and other. This function returns either 0 or 1 (as a rational number).

_lshift( self, unsigned long int exp)

Return $ self/2^exp$

_rshift( self, unsigned long int exp)

Return $ self/2^exp$

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