4.3.2.1 RealNumber Objects

class RealNumber
Abstract real number base class.
RealNumber( cls)

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

algebraic_dependence,$  $ arg,$  $ imag,$  $ log,$  $ order,$  $ pari_object,$  $ prec,$  $ real

These methods are defined as follows:

algebraic_dependence( n, [flag=False], [make_integral=0])

Try to find a polynomial of degree at most n having this real number x as an approximate root.

The polynomial that is obtained is not necessarily "correct", and it's not even guaranteed to be irreducible. One can check the closeness either by evaluation or by computing the roots of the polynomial given by algdep.

INPUT:
    n -- positive integer
    flag -- 0 (default): Use a variant of the LLL algorithm
               due to Hastard, Lagarias, and Schnorr (STACS
               1986).  If the precision is too low, the
               routine may enter an infinite loop.
            >0: Use a standard LLL algorithm, where the
               flag is a precision, which should be between
               0.5 and 1.0 times the number of decimal digits
               to which x was computed.
OUTPUT:
    A polynomial over the reals.  If make_integral is True,
    instead try to return a polynomial over the integers.

IMPLEMENTATION: Call PARI's algdep function. Note that the default real precision of PARI is used, not the precision of this real number.

EXAMPLE: We compute the minimal polynomial of sqrt(2):

sage: import real_field, math
sage: R = real_field.RealField()
sage: a = R(math.sqrt(2))
sage: a.algebraic_dependence(2)
x^2 - 2

Here we compute the real number 123/456, which is not in lowest terms, and find the minimal polynomial, which is a polynomial of degree 1.

sage: a = R('123/456')
sage: a
0.269736842105263157895
sage: a.algebraic_dependence(1)
152.0*x - 41.0
sage: a.algebraic_dependence(1, make_integral=True)
152*x - 41
sage: 41/152.0
0.26973684210526316

arg( )

imag( )

log( )

Return the logarithm of self.

Note: Currently the computation is done by converting the number to a float and using the standard library function. Thus there is potential for precision loss!

order( )

pari_object( )
PARI real associated to this real number

prec( )

real( )

Instances of class RealNumber also have the following special methods:

__abs__( )

__add__( right)

Return self + right.

sage: RealNumber_mpf('2/3') + RealNumber_decimal(0.333)
0.999666666666666666667

__cmp__( other)

Compare self and other.

In comparisons numeric types, e.g., int, long, float, and those derived from Element, are converted to a RealNumber (if it is on the left), to the precision of the number on the left.

    sage: R = RealNumber_mpf
    sage: R('2/3') < 1
    True
    sage: R('1/3') > 0.1
    True
    sage: R('1/3') > RealNumber_decimal(0.1)
    True
    sage: R('1/3') == R('0.3333333333333333')
    False
    
Strings are not numeric, so they are not converted.
    sage: R(1) == '1'
    False

__div__( right)

Return the quotient self / right.

sage: R = RealField_mpf()
sage: R(1) / R(3)
0.333333333333333333333

__float__( )

Return the coercion of self to a Python float.

sage: R = RealField_mpf()

sage: pi = R.pi(200); pi
3.1415926535897932384626433832795028841971693993751058209749445
9230782
sage: float(pi)
3.1415926535897931

__int__( )

Return the coercion of self to a Python int. This is the integer part of self.

    sage: R = RealField_mpf()
    
    >> pi = R.pi(200); pi
    3.141592653589793238462643383279502884197169399375105820974
94459230782
    >> int(pi)
    3
    
If the real number is too large to fit in a Python int, it is 
coerced to a Python long (note the L below):
    sage: int(R(141592653589793238462643383279502))
    141592653589793238462643383279502L
    
Note that int is not "rounding".  It simply takes the integer
part
of the decimal expansion, including the sign:
    sage: int(R(-0.8))
    0
    sage: int(R(0.8))
    0
    sage: int(R(-2.3))
    -2

__invert__( )

Return the inverse of self.

    sage: R = RealField_mpf()
    
    >> pi = R.pi(); pi
    3.14159265358979323846
    >> ~pi    # the additive inverse
    0.318309886183790671538
    
Division by zero raises a ....
    sage: ~R(0)
    Traceback (most recent call last):
    ...
    ZeroDivisionError: inverse of 0 not defined

__long__( )

Return the coercion of self to a Python long.

    sage: R = RealField_mpf()
    
    >> pi = R.pi(200); pi
    3.141592653589793238462643383279502884197169399375105820974
94459230782
    >> long(pi)
    3L
    
Note that int is not "rounding".  It simply takes the integer
part
of the decimal expansion, including the sign:
    sage: long(R(-0.8))
    0L
    sage: long(R(0.8))
    0L
    sage: long(R(-2.3))
    -2L

__mul__( right)

Return the product of self and right.

If right is not a RealNumber, an attempt is made to coerce self and right into a common parent.

sage: R = RealField_mpf()
sage: pi = R.pi()
sage: A = pi * 25; A
78.5398163397448309615
sage: isinstance(A, real_number.RealNumber_mpf)
True

In this example, a string is automatically coerced to a real number.

    sage: pi * '2.01'
    6.31460123371548440927
    
Of course two strings don't coerce to RealNumber's:
    sage: '3.14' * '2.01'
    Traceback (most recent call last):
    ...
    TypeError: can't multiply sequence by non-int

__neg__( )

Return the negative of self.

sage: x = RealNumber_mpf('1.23456')
sage: -x
-1.23456

__pos__( )

__pow__( right)

__radd__( left)

__rdiv__( left)

__rmul__( left)

__rsub__( left)

__sub__( right)

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