Double precision floating point real numbers - Fixed and Arbitrary Precision Numerical Fields

Skip to content
Back to top
View this page
Edit this page

# Double precision floating point real numbers

EXAMPLES:

We create the real double vector space of dimension \(3\):

Sage

    sage: V = RDF^3; V                                                                  # needs sage.modules
    Vector space of dimension 3 over Real Double Field

Python

    >>> from sage.all import *
    >>> V = RDF**Integer(3); V                                                                  # needs sage.modules
    Vector space of dimension 3 over Real Double Field

Notice that this space is unique:

Sage

    sage: V is RDF^3                                                                    # needs sage.modules
    True
    sage: V is FreeModule(RDF, 3)                                                       # needs sage.modules
    True
    sage: V is VectorSpace(RDF, 3)                                                      # needs sage.modules
    True

[Python]

    >>> from sage.all import *
    >>> V is RDF**Integer(3)                                                                    # needs sage.modules
    True
    >>> V is FreeModule(RDF, Integer(3))                                                       # needs sage.modules
    True
    >>> V is VectorSpace(RDF, Integer(3))                                                      # needs sage.modules
    True

Also, you can instantly create a space of large dimension:

Sage

    sage: V = RDF^10000                                                                 # needs sage.modules

Python

    >>> from sage.all import *
    >>> V = RDF**Integer(10000)                                                                 # needs sage.modules

class sage.rings.real_double.RealDoubleElement

  Bases: `FieldElement`

  An approximation to a real number using double precision floating point numbers. Answers derived from calculations with such approximations may differ from what they would be if those calculations were performed with true real numbers. This is due to the rounding errors inherent to finite precision calculations.

  NaN()

    Return Not-a-Number `NaN`.

    EXAMPLES:

    Sage

        sage: RDF.NaN()
        NaN

    Python

        >>> from sage.all import *
        >>> RDF.NaN()
        NaN

  abs()

    Return the absolute value of `self`.

    EXAMPLES:

    Sage

        sage: RDF(1e10).abs()
        10000000000.0
        sage: RDF(-1e10).abs()
        10000000000.0

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('1e10')).abs()
        10000000000.0
        >>> RDF(-RealNumber('1e10')).abs()
        10000000000.0

  agm(other)

    Return the arithmetic-geometric mean of `self` and `other`. The arithmetic-geometric mean is the common limit of the sequences \(u_n\) and \(v_n\), where \(u_0\) is `self`, \(v_0\) is other, \(u_{n+1}\) is the arithmetic mean of \(u_n\) and \(v_n\), and \(v_{n+1}\) is the geometric mean of \(u_n\) and \(v_n\). If any operand is negative, the return value is `NaN`.

    EXAMPLES:

    Sage

        sage: a = RDF(1.5)
        sage: b = RDF(2.3)
        sage: a.agm(b)
        1.8786484558146697

    Python

        >>> from sage.all import *
        >>> a = RDF(RealNumber('1.5'))
        >>> b = RDF(RealNumber('2.3'))
        >>> a.agm(b)
        1.8786484558146697

    The arithmetic-geometric mean always lies between the geometric and arithmetic mean:

    Sage

        sage: sqrt(a*b) < a.agm(b) < (a+b)/2
        True

    [Python]

        >>> from sage.all import *
        >>> sqrt(a*b) < a.agm(b) < (a+b)/Integer(2)
        True

  algdep(n)

    alias of `algebraic_dependency()`.

  algebraic_dependency(n)

    Return a polynomial of degree at most \(n\) which is approximately satisfied by this number.

    Note

    The resulting polynomial need not be irreducible, and indeed usually won’t be if this number is a good approximation to an algebraic number of degree less than \(n\).

    ALGORITHM:

    Uses the PARI C-library pari:algdep command.

    EXAMPLES:

    Sage

        sage: r = sqrt(RDF(2)); r
        1.4142135623730951
        sage: r.algebraic_dependency(5)                                             # needs sage.libs.pari
        x^2 - 2

    Python

        >>> from sage.all import *
        >>> r = sqrt(RDF(Integer(2))); r
        1.4142135623730951
        >>> r.algebraic_dependency(Integer(5))                                             # needs sage.libs.pari
        x^2 - 2

  as_integer_ratio()

    Return a coprime pair of integers `(a, b)` such that `self` equals `a / b` exactly.

    EXAMPLES:

    Sage

        sage: RDF(0).as_integer_ratio()
        (0, 1)
        sage: RDF(1/3).as_integer_ratio()
        (6004799503160661, 18014398509481984)
        sage: RDF(37/16).as_integer_ratio()
        (37, 16)
        sage: RDF(3^60).as_integer_ratio()
        (42391158275216203520420085760, 1)

    Python

        >>> from sage.all import *
        >>> RDF(Integer(0)).as_integer_ratio()
        (0, 1)
        >>> RDF(Integer(1)/Integer(3)).as_integer_ratio()
        (6004799503160661, 18014398509481984)
        >>> RDF(Integer(37)/Integer(16)).as_integer_ratio()
        (37, 16)
        >>> RDF(Integer(3)**Integer(60)).as_integer_ratio()
        (42391158275216203520420085760, 1)

  ceil()

    Return the ceiling of `self`.

    EXAMPLES:

    Sage

        sage: RDF(2.99).ceil()
        3
        sage: RDF(2.00).ceil()
        2
        sage: RDF(-5/2).ceil()
        -2

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('2.99')).ceil()
        3
        >>> RDF(RealNumber('2.00')).ceil()
        2
        >>> RDF(-Integer(5)/Integer(2)).ceil()
        -2

  ceiling()

    alias of `ceil()`.

  conjugate()

    Return the complex conjugate of this real number, which is the real number itself.

    EXAMPLES:

    Sage

        sage: RDF(4).conjugate()
        4.0

    Python

        >>> from sage.all import *
        >>> RDF(Integer(4)).conjugate()
        4.0

  cube_root()

    Return the cubic root (defined over the real numbers) of `self`.

    EXAMPLES:

    Sage

        sage: r = RDF(125.0); r.cube_root()
        5.000000000000001
        sage: r = RDF(-119.0)
        sage: r.cube_root()^3 - r  # rel tol 1
        -1.4210854715202004e-14

    Python

        >>> from sage.all import *
        >>> r = RDF(RealNumber('125.0')); r.cube_root()
        5.000000000000001
        >>> r = RDF(-RealNumber('119.0'))
        >>> r.cube_root()**Integer(3) - r  # rel tol 1
        -1.4210854715202004e-14

  floor()

    Return the floor of `self`.

    EXAMPLES:

    Sage

        sage: RDF(2.99).floor()
        2
        sage: RDF(2.00).floor()
        2
        sage: RDF(-5/2).floor()
        -3

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('2.99')).floor()
        2
        >>> RDF(RealNumber('2.00')).floor()
        2
        >>> RDF(-Integer(5)/Integer(2)).floor()
        -3

  frac()

    Return a real number in \((-1, 1)\). It satisfies the relation: `x = x.trunc() + x.frac()`

    EXAMPLES:

    Sage

        sage: RDF(2.99).frac()
        0.9900000000000002
        sage: RDF(2.50).frac()
        0.5
        sage: RDF(-2.79).frac()
        -0.79

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('2.99')).frac()
        0.9900000000000002
        >>> RDF(RealNumber('2.50')).frac()
        0.5
        >>> RDF(-RealNumber('2.79')).frac()
        -0.79

  imag()

    Return the imaginary part of this number, which is zero.

    EXAMPLES:

    Sage

        sage: a = RDF(3)
        sage: a.imag()
        0.0

    Python

        >>> from sage.all import *
        >>> a = RDF(Integer(3))
        >>> a.imag()
        0.0

  integer_part()

    If in decimal this number is written `n.defg`, returns `n`.

    EXAMPLES:

    Sage

        sage: r = RDF('-1.6')
        sage: a = r.integer_part(); a
        -1
        sage: type(a)
        <class 'sage.rings.integer.Integer'>
        sage: r = RDF(0.0/0.0)
        sage: a = r.integer_part()
        Traceback (most recent call last):
        ...
        TypeError: Attempt to get integer part of NaN

    Python

        >>> from sage.all import *
        >>> r = RDF('-1.6')
        >>> a = r.integer_part(); a
        -1
        >>> type(a)
        <class 'sage.rings.integer.Integer'>
        >>> r = RDF(RealNumber('0.0')/RealNumber('0.0'))
        >>> a = r.integer_part()
        Traceback (most recent call last):
        ...
        TypeError: Attempt to get integer part of NaN

  is_NaN()

    Check if `self` is `NaN`.

    EXAMPLES:

    Sage

        sage: RDF(1).is_NaN()
        False
        sage: a = RDF(0)/RDF(0)
        sage: a.is_NaN()
        True

    Python

        >>> from sage.all import *
        >>> RDF(Integer(1)).is_NaN()
        False
        >>> a = RDF(Integer(0))/RDF(Integer(0))
        >>> a.is_NaN()
        True

  is_infinity()

    Check if `self` is \(\infty\).

    EXAMPLES:

    Sage

        sage: a = RDF(2); b = RDF(0)
        sage: (a/b).is_infinity()
        True
        sage: (b/a).is_infinity()
        False

    Python

        >>> from sage.all import *
        >>> a = RDF(Integer(2)); b = RDF(Integer(0))
        >>> (a/b).is_infinity()
        True
        >>> (b/a).is_infinity()
        False

  is_integer()

    Return `True` if this number is a integer.

    EXAMPLES:

    Sage

        sage: RDF(3.5).is_integer()
        False
        sage: RDF(3).is_integer()
        True

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('3.5')).is_integer()
        False
        >>> RDF(Integer(3)).is_integer()
        True

  is_negative_infinity()

    Check if `self` is \(-\infty\).

    EXAMPLES:

    Sage

        sage: a = RDF(2)/RDF(0)
        sage: a.is_negative_infinity()
        False
        sage: a = RDF(-3)/RDF(0)
        sage: a.is_negative_infinity()
        True

    Python

        >>> from sage.all import *
        >>> a = RDF(Integer(2))/RDF(Integer(0))
        >>> a.is_negative_infinity()
        False
        >>> a = RDF(-Integer(3))/RDF(Integer(0))
        >>> a.is_negative_infinity()
        True

  is_positive_infinity()

    Check if `self` is \(+\infty\).

    EXAMPLES:

    Sage

        sage: a = RDF(1)/RDF(0)
        sage: a.is_positive_infinity()
        True
        sage: a = RDF(-1)/RDF(0)
        sage: a.is_positive_infinity()
        False

    Python

        >>> from sage.all import *
        >>> a = RDF(Integer(1))/RDF(Integer(0))
        >>> a.is_positive_infinity()
        True
        >>> a = RDF(-Integer(1))/RDF(Integer(0))
        >>> a.is_positive_infinity()
        False

  is_square()

    Return whether or not this number is a square in this field. For the real numbers, this is `True` if and only if `self` is nonnegative.

    EXAMPLES:

    Sage

        sage: RDF(3.5).is_square()
        True
        sage: RDF(0).is_square()
        True
        sage: RDF(-4).is_square()
        False

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('3.5')).is_square()
        True
        >>> RDF(Integer(0)).is_square()
        True
        >>> RDF(-Integer(4)).is_square()
        False

  multiplicative_order()

    Return \(n\) such that `self^n == 1`.

    Only \(\pm 1\) have finite multiplicative order.

    EXAMPLES:

    Sage

        sage: RDF(1).multiplicative_order()
        1
        sage: RDF(-1).multiplicative_order()
        2
        sage: RDF(3).multiplicative_order()
        +Infinity

    Python

        >>> from sage.all import *
        >>> RDF(Integer(1)).multiplicative_order()
        1
        >>> RDF(-Integer(1)).multiplicative_order()
        2
        >>> RDF(Integer(3)).multiplicative_order()
        +Infinity

  nan()

    alias of `NaN()`.

  prec()

    Return the precision of this number in bits.

    Always returns 53.

    EXAMPLES:

    Sage

        sage: RDF(0).prec()
        53

    Python

        >>> from sage.all import *
        >>> RDF(Integer(0)).prec()
        53

  real()

    Return `self` - we are already real.

    EXAMPLES:

    Sage

        sage: a = RDF(3)
        sage: a.real()
        3.0

    Python

        >>> from sage.all import *
        >>> a = RDF(Integer(3))
        >>> a.real()
        3.0

  round()

    Round `self` to the nearest integer.

    This uses the convention of rounding half to even (i.e., if the fractional part of `self` is \(0.5\), then it is rounded to the nearest even integer).

    EXAMPLES:

    Sage

        sage: RDF(0.49).round()
        0
        sage: a=RDF(0.51).round(); a
        1
        sage: RDF(0.5).round()
        0
        sage: RDF(1.5).round()
        2

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('0.49')).round()
        0
        >>> a=RDF(RealNumber('0.51')).round(); a
        1
        >>> RDF(RealNumber('0.5')).round()
        0
        >>> RDF(RealNumber('1.5')).round()
        2

  sign()

    Return -1, 0, or 1 if `self` is negative, zero, or positive; respectively.

    EXAMPLES:

    Sage

        sage: RDF(-1.5).sign()
        -1
        sage: RDF(0).sign()
        0
        sage: RDF(2.5).sign()
        1

    Python

        >>> from sage.all import *
        >>> RDF(-RealNumber('1.5')).sign()
        -1
        >>> RDF(Integer(0)).sign()
        0
        >>> RDF(RealNumber('2.5')).sign()
        1

  sign_mantissa_exponent()

    Return the sign, mantissa, and exponent of `self`.

    In Sage (as in MPFR), floating-point numbers of precision \(p\) are of the form \(s m 2^{e-p}\), where \(s \in \{-1, 1\}\), \(2^{p-1} \leq m < 2^p\), and \(-2^{30} + 1 \leq e \leq 2^{30} - 1\); plus the special values `+0`, `-0`, `+infinity`, `-infinity`, and `NaN` (which stands for Not-a-Number).

    This function returns \(s\), \(m\), and \(e-p\). For the special values:

    - `+0` returns `(1, 0, 0)`

    - `-0` returns `(-1, 0, 0)`

    - the return values for `+infinity`, `-infinity`, and `NaN` are not specified.

    EXAMPLES:

    Sage

        sage: # needs sage.symbolic
        sage: a = RDF(exp(1.0)); a
        2.718281828459045
        sage: sign, mantissa, exponent = RDF(exp(1.0)).sign_mantissa_exponent()
        sage: sign, mantissa, exponent
        (1, 6121026514868073, -51)
        sage: sign*mantissa*(2**exponent) == a
        True

    Python

        >>> from sage.all import *
        >>> # needs sage.symbolic
        >>> a = RDF(exp(RealNumber('1.0'))); a
        2.718281828459045
        >>> sign, mantissa, exponent = RDF(exp(RealNumber('1.0'))).sign_mantissa_exponent()
        >>> sign, mantissa, exponent
        (1, 6121026514868073, -51)
        >>> sign*mantissa*(Integer(2)**exponent) == a
        True

    The mantissa is always a nonnegative number:

    Sage

        sage: RDF(-1).sign_mantissa_exponent()                                      # needs sage.rings.real_mpfr
        (-1, 4503599627370496, -52)

    [Python]

        >>> from sage.all import *
        >>> RDF(-Integer(1)).sign_mantissa_exponent()                                      # needs sage.rings.real_mpfr
        (-1, 4503599627370496, -52)

  sqrt(extend=True, all=False)

    The square root function.

    INPUT:

    - `extend` – boolean (default: `True`); if `True`, return a square root in a complex field if necessary if `self` is negative. Otherwise raise a `ValueError`.

    - `all` – boolean (default: `False`); if `True`, return a list of all square roots

    EXAMPLES:

    Sage

        sage: r = RDF(4.0)
        sage: r.sqrt()
        2.0
        sage: r.sqrt()^2 == r
        True

    Python

        >>> from sage.all import *
        >>> r = RDF(RealNumber('4.0'))
        >>> r.sqrt()
        2.0
        >>> r.sqrt()**Integer(2) == r
        True

    Sage

        sage: r = RDF(4344)
        sage: r.sqrt()
        65.90902821313632
        sage: r.sqrt()^2 - r
        0.0

    [Python]

        >>> from sage.all import *
        >>> r = RDF(Integer(4344))
        >>> r.sqrt()
        65.90902821313632
        >>> r.sqrt()**Integer(2) - r
        0.0

    Sage

        sage: r = RDF(-2.0)
        sage: r.sqrt()                                                              # needs sage.rings.complex_double
        1.4142135623730951*I

    Python

        >>> from sage.all import *
        >>> r = RDF(-RealNumber('2.0'))
        >>> r.sqrt()                                                              # needs sage.rings.complex_double
        1.4142135623730951*I

    Sage

        sage: RDF(2).sqrt(all=True)
        [1.4142135623730951, -1.4142135623730951]
        sage: RDF(0).sqrt(all=True)
        [0.0]
        sage: RDF(-2).sqrt(all=True)                                                # needs sage.rings.complex_double
        [1.4142135623730951*I, -1.4142135623730951*I]

    [Python]

        >>> from sage.all import *
        >>> RDF(Integer(2)).sqrt(all=True)
        [1.4142135623730951, -1.4142135623730951]
        >>> RDF(Integer(0)).sqrt(all=True)
        [0.0]
        >>> RDF(-Integer(2)).sqrt(all=True)                                                # needs sage.rings.complex_double
        [1.4142135623730951*I, -1.4142135623730951*I]

  str()

    Return the informal string representation of `self`.

    EXAMPLES:

    Sage

        sage: a = RDF('4.5'); a.str()
        '4.5'
        sage: a = RDF('49203480923840.2923904823048'); a.str()
        '49203480923840.29'
        sage: a = RDF(1)/RDF(0); a.str()
        '+infinity'
        sage: a = -RDF(1)/RDF(0); a.str()
        '-infinity'
        sage: a = RDF(0)/RDF(0); a.str()
        'NaN'

    Python

        >>> from sage.all import *
        >>> a = RDF('4.5'); a.str()
        '4.5'
        >>> a = RDF('49203480923840.2923904823048'); a.str()
        '49203480923840.29'
        >>> a = RDF(Integer(1))/RDF(Integer(0)); a.str()
        '+infinity'
        >>> a = -RDF(Integer(1))/RDF(Integer(0)); a.str()
        '-infinity'
        >>> a = RDF(Integer(0))/RDF(Integer(0)); a.str()
        'NaN'

    We verify consistency with `RR` (mpfr reals):

    Sage

        sage: str(RR(RDF(1)/RDF(0))) == str(RDF(1)/RDF(0))
        True
        sage: str(RR(-RDF(1)/RDF(0))) == str(-RDF(1)/RDF(0))
        True
        sage: str(RR(RDF(0)/RDF(0))) == str(RDF(0)/RDF(0))
        True

    [Python]

        >>> from sage.all import *
        >>> str(RR(RDF(Integer(1))/RDF(Integer(0)))) == str(RDF(Integer(1))/RDF(Integer(0)))
        True
        >>> str(RR(-RDF(Integer(1))/RDF(Integer(0)))) == str(-RDF(Integer(1))/RDF(Integer(0)))
        True
        >>> str(RR(RDF(Integer(0))/RDF(Integer(0)))) == str(RDF(Integer(0))/RDF(Integer(0)))
        True

  trunc()

    Truncates this number (returns integer part).

    EXAMPLES:

    Sage

        sage: RDF(2.99).trunc()
        2
        sage: RDF(-2.00).trunc()
        -2
        sage: RDF(0.00).trunc()
        0

    Python

        >>> from sage.all import *
        >>> RDF(RealNumber('2.99')).trunc()
        2
        >>> RDF(-RealNumber('2.00')).trunc()
        -2
        >>> RDF(RealNumber('0.00')).trunc()
        0

  ulp()

    Return the unit of least precision of `self`, which is the weight of the least significant bit of `self`. This is always a strictly positive number. It is also the gap between this number and the closest number with larger absolute value that can be represented.

    EXAMPLES:

    Sage

        sage: a = RDF(pi)                                                           # needs sage.symbolic
        sage: a.ulp()                                                               # needs sage.symbolic
        4.440892098500626e-16
        sage: b = a + a.ulp()                                                       # needs sage.symbolic

    Python

        >>> from sage.all import *
        >>> a = RDF(pi)                                                           # needs sage.symbolic
        >>> a.ulp()                                                               # needs sage.symbolic
        4.440892098500626e-16
        >>> b = a + a.ulp()                                                       # needs sage.symbolic

    Adding or subtracting an ulp always gives a different number:

    Sage

        sage: # needs sage.symbolic
        sage: a + a.ulp() == a
        False
        sage: a - a.ulp() == a
        False
        sage: b + b.ulp() == b
        False
        sage: b - b.ulp() == b
        False

    [Python]

        >>> from sage.all import *
        >>> # needs sage.symbolic
        >>> a + a.ulp() == a
        False
        >>> a - a.ulp() == a
        False
        >>> b + b.ulp() == b
        False
        >>> b - b.ulp() == b
        False

    Since the default rounding mode is round-to-nearest, adding or subtracting something less than half an ulp always gives the same number, unless the result has a smaller ulp. The latter can only happen if the input number is (up to sign) exactly a power of 2:

    Sage

        sage: # needs sage.symbolic
        sage: a - a.ulp()/3 == a
        True
        sage: a + a.ulp()/3 == a
        True
        sage: b - b.ulp()/3 == b
        True
        sage: b + b.ulp()/3 == b
        True

        sage: c = RDF(1)
        sage: c - c.ulp()/3 == c
        False
        sage: c.ulp()
        2.220446049250313e-16
        sage: (c - c.ulp()).ulp()
        1.1102230246251565e-16

    Python

        >>> from sage.all import *
        >>> # needs sage.symbolic
        >>> a - a.ulp()/Integer(3) == a
        True
        >>> a + a.ulp()/Integer(3) == a
        True
        >>> b - b.ulp()/Integer(3) == b
        True
        >>> b + b.ulp()/Integer(3) == b
        True

        >>> c = RDF(Integer(1))
        >>> c - c.ulp()/Integer(3) == c
        False
        >>> c.ulp()
        2.220446049250313e-16
        >>> (c - c.ulp()).ulp()
        1.1102230246251565e-16

    The ulp is always positive:

    Sage

        sage: RDF(-1).ulp()
        2.220446049250313e-16

    [Python]

        >>> from sage.all import *
        >>> RDF(-Integer(1)).ulp()
        2.220446049250313e-16

    The ulp of zero is the smallest positive number in RDF:

    Sage

        sage: RDF(0).ulp()
        5e-324
        sage: RDF(0).ulp()/2
        0.0

    Python

        >>> from sage.all import *
        >>> RDF(Integer(0)).ulp()
        5e-324
        >>> RDF(Integer(0)).ulp()/Integer(2)
        0.0

    Some special values:

    Sage

        sage: a = RDF(1)/RDF(0); a
        +infinity
        sage: a.ulp()
        +infinity
        sage: (-a).ulp()
        +infinity
        sage: a = RDF('nan')
        sage: a.ulp() is a
        True

    [Python]

        >>> from sage.all import *
        >>> a = RDF(Integer(1))/RDF(Integer(0)); a
        +infinity
        >>> a.ulp()
        +infinity
        >>> (-a).ulp()
        +infinity
        >>> a = RDF('nan')
        >>> a.ulp() is a
        True

    The ulp method works correctly with small numbers:

    Sage

        sage: u = RDF(0).ulp()
        sage: u.ulp() == u
        True
        sage: x = u * (2^52-1)  # largest denormal number
        sage: x.ulp() == u
        True
        sage: x = u * 2^52  # smallest normal number
        sage: x.ulp() == u
        True

    Python

        >>> from sage.all import *
        >>> u = RDF(Integer(0)).ulp()
        >>> u.ulp() == u
        True
        >>> x = u * (Integer(2)**Integer(52)-Integer(1))  # largest denormal number
        >>> x.ulp() == u
        True
        >>> x = u * Integer(2)**Integer(52)  # smallest normal number
        >>> x.ulp() == u
        True

sage.rings.real_double.RealDoubleField()

  Return the unique instance of the `real double field`.

  EXAMPLES:

  Sage

      sage: RealDoubleField() is RealDoubleField()
      True

  Python

      >>> from sage.all import *
      >>> RealDoubleField() is RealDoubleField()
      True

class sage.rings.real_double.RealDoubleField_class

  Bases: `RealDoubleField`

  An approximation to the field of real numbers using double precision floating point numbers. Answers derived from calculations in this approximation may differ from what they would be if those calculations were performed in the true field of real numbers. This is due to the rounding errors inherent to finite precision calculations.

  EXAMPLES:

  Sage

      sage: RR == RDF                                                                 # needs sage.rings.real_mpfr
      False
      sage: RDF == RealDoubleField()    # RDF is the shorthand
      True

  Python

      >>> from sage.all import *
      >>> RR == RDF                                                                 # needs sage.rings.real_mpfr
      False
      >>> RDF == RealDoubleField()    # RDF is the shorthand
      True

  Sage

      sage: RDF(1)
      1.0
      sage: RDF(2/3)
      0.6666666666666666

  [Python]

      >>> from sage.all import *
      >>> RDF(Integer(1))
      1.0
      >>> RDF(Integer(2)/Integer(3))
      0.6666666666666666

  A `TypeError` is raised if the coercion doesn’t make sense:

  Sage

      sage: RDF(QQ['x'].0)
      Traceback (most recent call last):
      ...
      TypeError: cannot convert nonconstant polynomial
      sage: RDF(QQ['x'](3))
      3.0

  Python

      >>> from sage.all import *
      >>> RDF(QQ['x'].gen(0))
      Traceback (most recent call last):
      ...
      TypeError: cannot convert nonconstant polynomial
      >>> RDF(QQ['x'](Integer(3)))
      3.0

  One can convert back and forth between double precision real numbers and higher-precision ones, though of course there may be loss of precision:

  Sage

      sage: # needs sage.rings.real_mpfr
      sage: a = RealField(200)(2).sqrt(); a
      1.4142135623730950488016887242096980785696718753769480731767
      sage: b = RDF(a); b
      1.4142135623730951
      sage: a.parent()(b)
      1.4142135623730951454746218587388284504413604736328125000000
      sage: a.parent()(b) == b
      True
      sage: b == RR(a)
      True

  [Python]

      >>> from sage.all import *
      >>> # needs sage.rings.real_mpfr
      >>> a = RealField(Integer(200))(Integer(2)).sqrt(); a
      1.4142135623730950488016887242096980785696718753769480731767
      >>> b = RDF(a); b
      1.4142135623730951
      >>> a.parent()(b)
      1.4142135623730951454746218587388284504413604736328125000000
      >>> a.parent()(b) == b
      True
      >>> b == RR(a)
      True

  NaN()

    Return Not-a-Number `NaN`.

    EXAMPLES:

    Sage

        sage: RDF.NaN()
        NaN

    Python

        >>> from sage.all import *
        >>> RDF.NaN()
        NaN

  algebraic_closure()

    Return the algebraic closure of `self`, i.e., the complex double field.

    EXAMPLES:

    Sage

        sage: RDF.algebraic_closure()                                               # needs sage.rings.complex_double
        Complex Double Field

    Python

        >>> from sage.all import *
        >>> RDF.algebraic_closure()                                               # needs sage.rings.complex_double
        Complex Double Field

  characteristic()

    Return 0, since the field of real numbers has characteristic 0.

    EXAMPLES:

    Sage

        sage: RDF.characteristic()
        0

    Python

        >>> from sage.all import *
        >>> RDF.characteristic()
        0

  complex_field()

    Return the complex field with the same precision as `self`, i.e., the complex double field.

    EXAMPLES:

    Sage

        sage: RDF.complex_field()                                                   # needs sage.rings.complex_double
        Complex Double Field

    Python

        >>> from sage.all import *
        >>> RDF.complex_field()                                                   # needs sage.rings.complex_double
        Complex Double Field

  construction()

    Return the functorial construction of `self`, namely, completion of the rational numbers with respect to the prime at \(\infty\).

    Also preserves other information that makes this field unique (i.e. the Real Double Field).

    EXAMPLES:

    Sage

        sage: c, S = RDF.construction(); S
        Rational Field
        sage: RDF == c(S)
        True

    Python

        >>> from sage.all import *
        >>> c, S = RDF.construction(); S
        Rational Field
        >>> RDF == c(S)
        True

  euler_constant()

    Return Euler’s gamma constant to double precision.

    EXAMPLES:

    Sage

        sage: RDF.euler_constant()
        0.5772156649015329

    Python

        >>> from sage.all import *
        >>> RDF.euler_constant()
        0.5772156649015329

  factorial(n)

    Return the factorial of the integer \(n\) as a real number.

    EXAMPLES:

    Sage

        sage: RDF.factorial(100)
        9.332621544394415e+157

    Python

        >>> from sage.all import *
        >>> RDF.factorial(Integer(100))
        9.332621544394415e+157

  gen(n=0)

    Return the generator of the real double field.

    EXAMPLES:

    Sage

        sage: RDF.0
        1.0
        sage: RDF.gens()
        (1.0,)

    Python

        >>> from sage.all import *
        >>> RDF.gen(0)
        1.0
        >>> RDF.gens()
        (1.0,)

  is_exact()

    Return `False`, because doubles are not exact.

    EXAMPLES:

    Sage

        sage: RDF.is_exact()
        False

    Python

        >>> from sage.all import *
        >>> RDF.is_exact()
        False

  log2()

    Return \(\log(2)\) to the precision of this field.

    EXAMPLES:

    Sage

        sage: RDF.log2()
        0.6931471805599453
        sage: RDF(2).log()
        0.6931471805599453

    Python

        >>> from sage.all import *
        >>> RDF.log2()
        0.6931471805599453
        >>> RDF(Integer(2)).log()
        0.6931471805599453

  name()

    The name of `self`.

    EXAMPLES:

    Sage

        sage: RDF.name()
        'RealDoubleField'

    Python

        >>> from sage.all import *
        >>> RDF.name()
        'RealDoubleField'

  nan()

    alias of `NaN()`.

  ngens()

    Return the number of generators which is always 1.

    EXAMPLES:

    Sage

        sage: RDF.ngens()
        1

    Python

        >>> from sage.all import *
        >>> RDF.ngens()
        1

  pi()

    Return \(\pi\) to double-precision.

    EXAMPLES:

    Sage

        sage: RDF.pi()
        3.141592653589793
        sage: RDF.pi().sqrt()/2
        0.8862269254527579

    Python

        >>> from sage.all import *
        >>> RDF.pi()
        3.141592653589793
        >>> RDF.pi().sqrt()/Integer(2)
        0.8862269254527579

  prec()

    alias of `precision()`.

  precision()

    Return the precision of this real double field in bits.

    Always returns 53.

    EXAMPLES:

    Sage

        sage: RDF.precision()
        53

    Python

        >>> from sage.all import *
        >>> RDF.precision()
        53

  random_element(min=-1, max=1)

    Return a random element of this real double field in the interval `[min, max]`.

    EXAMPLES:

    Sage

        sage: RDF.random_element().parent() is RDF
        True
        sage: -1 <= RDF.random_element() <= 1
        True
        sage: 100 <= RDF.random_element(min=100, max=110) <= 110
        True

    Python

        >>> from sage.all import *
        >>> RDF.random_element().parent() is RDF
        True
        >>> -Integer(1) <= RDF.random_element() <= Integer(1)
        True
        >>> Integer(100) <= RDF.random_element(min=Integer(100), max=Integer(110)) <= Integer(110)
        True

  to_prec(prec)

    Return the real field to the specified precision. As doubles have fixed precision, this will only return a real double field if `prec` is exactly 53.

    EXAMPLES:

    Sage

        sage: RDF.to_prec(52)                                                       # needs sage.rings.real_mpfr
        Real Field with 52 bits of precision
        sage: RDF.to_prec(53)
        Real Double Field

    Python

        >>> from sage.all import *
        >>> RDF.to_prec(Integer(52))                                                       # needs sage.rings.real_mpfr
        Real Field with 52 bits of precision
        >>> RDF.to_prec(Integer(53))
        Real Double Field

  zeta(n=2)

    Return an \(n\)-th root of unity in the real field, if one exists, or raise a `ValueError` otherwise.

    EXAMPLES:

    Sage

        sage: RDF.zeta()
        -1.0
        sage: RDF.zeta(1)
        1.0
        sage: RDF.zeta(5)
        Traceback (most recent call last):
        ...
        ValueError: No 5th root of unity in self

    Python

        >>> from sage.all import *
        >>> RDF.zeta()
        -1.0
        >>> RDF.zeta(Integer(1))
        1.0
        >>> RDF.zeta(Integer(5))
        Traceback (most recent call last):
        ...
        ValueError: No 5th root of unity in self

class sage.rings.real_double.ToRDF

  Bases: `Morphism`

  Fast morphism from anything with a `__float__` method to an `RDF` element.

  EXAMPLES:

  Sage

      sage: f = RDF.coerce_map_from(ZZ); f
      Native morphism:
        From: Integer Ring
        To:   Real Double Field
      sage: f(4)
      4.0
      sage: f = RDF.coerce_map_from(QQ); f
      Native morphism:
        From: Rational Field
        To:   Real Double Field
      sage: f(1/2)
      0.5
      sage: f = RDF.coerce_map_from(int); f
      Native morphism:
        From: Set of Python objects of class 'int'
        To:   Real Double Field
      sage: f(3r)
      3.0
      sage: f = RDF.coerce_map_from(float); f
      Native morphism:
        From: Set of Python objects of class 'float'
        To:   Real Double Field
      sage: f(3.5)
      3.5

  Python

      >>> from sage.all import *
      >>> f = RDF.coerce_map_from(ZZ); f
      Native morphism:
        From: Integer Ring
        To:   Real Double Field
      >>> f(Integer(4))
      4.0
      >>> f = RDF.coerce_map_from(QQ); f
      Native morphism:
        From: Rational Field
        To:   Real Double Field
      >>> f(Integer(1)/Integer(2))
      0.5
      >>> f = RDF.coerce_map_from(int); f
      Native morphism:
        From: Set of Python objects of class 'int'
        To:   Real Double Field
      >>> f(3)
      3.0
      >>> f = RDF.coerce_map_from(float); f
      Native morphism:
        From: Set of Python objects of class 'float'
        To:   Real Double Field
      >>> f(RealNumber('3.5'))
      3.5

sage.rings.real_double.is_RealDoubleElement(x)

  Check if `x` is an element of the real double field.

  EXAMPLES:

  Sage

      sage: from sage.rings.real_double import is_RealDoubleElement
      sage: is_RealDoubleElement(RDF(3))
      doctest:warning...
      DeprecationWarning: The function is_RealDoubleElement is deprecated;
      use 'isinstance(..., RealDoubleElement)' instead.
      See https://github.com/sagemath/sage/issues/38128 for details.
      True
      sage: is_RealDoubleElement(RIF(3))                                              # needs sage.rings.real_interval_field
      False

  Python

      >>> from sage.all import *
      >>> from sage.rings.real_double import is_RealDoubleElement
      >>> is_RealDoubleElement(RDF(Integer(3)))
      doctest:warning...
      DeprecationWarning: The function is_RealDoubleElement is deprecated;
      use 'isinstance(..., RealDoubleElement)' instead.
      See https://github.com/sagemath/sage/issues/38128 for details.
      True
      >>> is_RealDoubleElement(RIF(Integer(3)))                                              # needs sage.rings.real_interval_field
      False