Double precision floating point real numbers

EXAMPLES:

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

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

Notice that this space is unique:

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
>>> 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
V is RDF^3                                                                    # needs sage.modules
V is FreeModule(RDF, 3)                                                       # needs sage.modules
V is VectorSpace(RDF, 3)                                                      # needs sage.modules

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

sage: V = RDF^10000                                                                 # needs sage.modules
>>> from sage.all import *
>>> V = RDF**Integer(10000)                                                                 # needs sage.modules
V = RDF^10000                                                                 # needs sage.modules
class sage.rings.real_double.RealDoubleElement[source]

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()[source]

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN
>>> from sage.all import *
>>> RDF.NaN()
NaN
RDF.NaN()
abs()[source]

Return the absolute value of self.

EXAMPLES:

sage: RDF(1e10).abs()
10000000000.0
sage: RDF(-1e10).abs()
10000000000.0
>>> from sage.all import *
>>> RDF(RealNumber('1e10')).abs()
10000000000.0
>>> RDF(-RealNumber('1e10')).abs()
10000000000.0
RDF(1e10).abs()
RDF(-1e10).abs()
agm(other)[source]

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: a = RDF(1.5)
sage: b = RDF(2.3)
sage: a.agm(b)
1.8786484558146697
>>> from sage.all import *
>>> a = RDF(RealNumber('1.5'))
>>> b = RDF(RealNumber('2.3'))
>>> a.agm(b)
1.8786484558146697
a = RDF(1.5)
b = RDF(2.3)
a.agm(b)

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

sage: sqrt(a*b) < a.agm(b) < (a+b)/2
True
>>> from sage.all import *
>>> sqrt(a*b) < a.agm(b) < (a+b)/Integer(2)
True
sqrt(a*b) < a.agm(b) < (a+b)/2
algdep(n)[source]

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: r = sqrt(RDF(2)); r
1.4142135623730951
sage: r.algebraic_dependency(5)                                             # needs sage.libs.pari
x^2 - 2
>>> from sage.all import *
>>> r = sqrt(RDF(Integer(2))); r
1.4142135623730951
>>> r.algebraic_dependency(Integer(5))                                             # needs sage.libs.pari
x^2 - 2
r = sqrt(RDF(2)); r
r.algebraic_dependency(5)                                             # needs sage.libs.pari
algebraic_dependency(n)[source]

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: r = sqrt(RDF(2)); r
1.4142135623730951
sage: r.algebraic_dependency(5)                                             # needs sage.libs.pari
x^2 - 2
>>> from sage.all import *
>>> r = sqrt(RDF(Integer(2))); r
1.4142135623730951
>>> r.algebraic_dependency(Integer(5))                                             # needs sage.libs.pari
x^2 - 2
r = sqrt(RDF(2)); r
r.algebraic_dependency(5)                                             # needs sage.libs.pari
as_integer_ratio()[source]

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

EXAMPLES:

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)
>>> 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)
RDF(0).as_integer_ratio()
RDF(1/3).as_integer_ratio()
RDF(37/16).as_integer_ratio()
RDF(3^60).as_integer_ratio()
ceil()[source]

Return the ceiling of self.

EXAMPLES:

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

Return the ceiling of self.

EXAMPLES:

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

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

EXAMPLES:

sage: RDF(4).conjugate()
4.0
>>> from sage.all import *
>>> RDF(Integer(4)).conjugate()
4.0
RDF(4).conjugate()
cube_root()[source]

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

EXAMPLES:

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
>>> 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
r = RDF(125.0); r.cube_root()
r = RDF(-119.0)
r.cube_root()^3 - r  # rel tol 1
floor()[source]

Return the floor of self.

EXAMPLES:

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

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

EXAMPLES:

sage: RDF(2.99).frac()
0.9900000000000002
sage: RDF(2.50).frac()
0.5
sage: RDF(-2.79).frac()
-0.79
>>> 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
RDF(2.99).frac()
RDF(2.50).frac()
RDF(-2.79).frac()
imag()[source]

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

EXAMPLES:

sage: a = RDF(3)
sage: a.imag()
0.0
>>> from sage.all import *
>>> a = RDF(Integer(3))
>>> a.imag()
0.0
a = RDF(3)
a.imag()
integer_part()[source]

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

EXAMPLES:

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
>>> 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
r = RDF('-1.6')
a = r.integer_part(); a
type(a)
r = RDF(0.0/0.0)
a = r.integer_part()
is_NaN()[source]

Check if self is NaN.

EXAMPLES:

sage: RDF(1).is_NaN()
False
sage: a = RDF(0)/RDF(0)
sage: a.is_NaN()
True
>>> from sage.all import *
>>> RDF(Integer(1)).is_NaN()
False
>>> a = RDF(Integer(0))/RDF(Integer(0))
>>> a.is_NaN()
True
RDF(1).is_NaN()
a = RDF(0)/RDF(0)
a.is_NaN()
is_infinity()[source]

Check if self is \(\infty\).

EXAMPLES:

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

Return True if this number is a integer.

EXAMPLES:

sage: RDF(3.5).is_integer()
False
sage: RDF(3).is_integer()
True
>>> from sage.all import *
>>> RDF(RealNumber('3.5')).is_integer()
False
>>> RDF(Integer(3)).is_integer()
True
RDF(3.5).is_integer()
RDF(3).is_integer()
is_negative_infinity()[source]

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

EXAMPLES:

sage: a = RDF(2)/RDF(0)
sage: a.is_negative_infinity()
False
sage: a = RDF(-3)/RDF(0)
sage: a.is_negative_infinity()
True
>>> 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
a = RDF(2)/RDF(0)
a.is_negative_infinity()
a = RDF(-3)/RDF(0)
a.is_negative_infinity()
is_positive_infinity()[source]

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

EXAMPLES:

sage: a = RDF(1)/RDF(0)
sage: a.is_positive_infinity()
True
sage: a = RDF(-1)/RDF(0)
sage: a.is_positive_infinity()
False
>>> 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
a = RDF(1)/RDF(0)
a.is_positive_infinity()
a = RDF(-1)/RDF(0)
a.is_positive_infinity()
is_square()[source]

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: RDF(3.5).is_square()
True
sage: RDF(0).is_square()
True
sage: RDF(-4).is_square()
False
>>> from sage.all import *
>>> RDF(RealNumber('3.5')).is_square()
True
>>> RDF(Integer(0)).is_square()
True
>>> RDF(-Integer(4)).is_square()
False
RDF(3.5).is_square()
RDF(0).is_square()
RDF(-4).is_square()
multiplicative_order()[source]

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

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

EXAMPLES:

sage: RDF(1).multiplicative_order()
1
sage: RDF(-1).multiplicative_order()
2
sage: RDF(3).multiplicative_order()
+Infinity
>>> from sage.all import *
>>> RDF(Integer(1)).multiplicative_order()
1
>>> RDF(-Integer(1)).multiplicative_order()
2
>>> RDF(Integer(3)).multiplicative_order()
+Infinity
RDF(1).multiplicative_order()
RDF(-1).multiplicative_order()
RDF(3).multiplicative_order()
nan()[source]

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN
>>> from sage.all import *
>>> RDF.NaN()
NaN
RDF.NaN()
prec()[source]

Return the precision of this number in bits.

Always returns 53.

EXAMPLES:

sage: RDF(0).prec()
53
>>> from sage.all import *
>>> RDF(Integer(0)).prec()
53
RDF(0).prec()
real()[source]

Return self - we are already real.

EXAMPLES:

sage: a = RDF(3)
sage: a.real()
3.0
>>> from sage.all import *
>>> a = RDF(Integer(3))
>>> a.real()
3.0
a = RDF(3)
a.real()
round()[source]

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: 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
>>> 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
RDF(0.49).round()
a=RDF(0.51).round(); a
RDF(0.5).round()
RDF(1.5).round()
sign()[source]

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

EXAMPLES:

sage: RDF(-1.5).sign()
-1
sage: RDF(0).sign()
0
sage: RDF(2.5).sign()
1
>>> from sage.all import *
>>> RDF(-RealNumber('1.5')).sign()
-1
>>> RDF(Integer(0)).sign()
0
>>> RDF(RealNumber('2.5')).sign()
1
RDF(-1.5).sign()
RDF(0).sign()
RDF(2.5).sign()
sign_mantissa_exponent()[source]

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: # 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
>>> 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
# needs sage.symbolic
a = RDF(exp(1.0)); a
sign, mantissa, exponent = RDF(exp(1.0)).sign_mantissa_exponent()
sign, mantissa, exponent
sign*mantissa*(2**exponent) == a

The mantissa is always a nonnegative number:

sage: RDF(-1).sign_mantissa_exponent()                                      # needs sage.rings.real_mpfr
(-1, 4503599627370496, -52)
>>> from sage.all import *
>>> RDF(-Integer(1)).sign_mantissa_exponent()                                      # needs sage.rings.real_mpfr
(-1, 4503599627370496, -52)
RDF(-1).sign_mantissa_exponent()                                      # needs sage.rings.real_mpfr
sqrt(extend=True, all=False)[source]

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: r = RDF(4.0)
sage: r.sqrt()
2.0
sage: r.sqrt()^2 == r
True
>>> from sage.all import *
>>> r = RDF(RealNumber('4.0'))
>>> r.sqrt()
2.0
>>> r.sqrt()**Integer(2) == r
True
r = RDF(4.0)
r.sqrt()
r.sqrt()^2 == r
sage: r = RDF(4344)
sage: r.sqrt()
65.90902821313632
sage: r.sqrt()^2 - r
0.0
>>> from sage.all import *
>>> r = RDF(Integer(4344))
>>> r.sqrt()
65.90902821313632
>>> r.sqrt()**Integer(2) - r
0.0
r = RDF(4344)
r.sqrt()
r.sqrt()^2 - r
>>> from sage.all import *
>>> r = RDF(Integer(4344))
>>> r.sqrt()
65.90902821313632
>>> r.sqrt()**Integer(2) - r
0.0
r = RDF(4344)
r.sqrt()
r.sqrt()^2 - r
sage: r = RDF(-2.0)
sage: r.sqrt()                                                              # needs sage.rings.complex_double
1.4142135623730951*I
>>> from sage.all import *
>>> r = RDF(-RealNumber('2.0'))
>>> r.sqrt()                                                              # needs sage.rings.complex_double
1.4142135623730951*I
r = RDF(-2.0)
r.sqrt()                                                              # needs sage.rings.complex_double
>>> from sage.all import *
>>> r = RDF(-RealNumber('2.0'))
>>> r.sqrt()                                                              # needs sage.rings.complex_double
1.4142135623730951*I
r = RDF(-2.0)
r.sqrt()                                                              # needs sage.rings.complex_double
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]
>>> 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]
RDF(2).sqrt(all=True)
RDF(0).sqrt(all=True)
RDF(-2).sqrt(all=True)                                                # needs sage.rings.complex_double
>>> 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]
RDF(2).sqrt(all=True)
RDF(0).sqrt(all=True)
RDF(-2).sqrt(all=True)                                                # needs sage.rings.complex_double
str()[source]

Return the informal string representation of self.

EXAMPLES:

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'
>>> 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'
a = RDF('4.5'); a.str()
a = RDF('49203480923840.2923904823048'); a.str()
a = RDF(1)/RDF(0); a.str()
a = -RDF(1)/RDF(0); a.str()
a = RDF(0)/RDF(0); a.str()

We verify consistency with RR (mpfr reals):

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
>>> 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
str(RR(RDF(1)/RDF(0))) == str(RDF(1)/RDF(0))
str(RR(-RDF(1)/RDF(0))) == str(-RDF(1)/RDF(0))
str(RR(RDF(0)/RDF(0))) == str(RDF(0)/RDF(0))
trunc()[source]

Truncates this number (returns integer part).

EXAMPLES:

sage: RDF(2.99).trunc()
2
sage: RDF(-2.00).trunc()
-2
sage: RDF(0.00).trunc()
0
>>> from sage.all import *
>>> RDF(RealNumber('2.99')).trunc()
2
>>> RDF(-RealNumber('2.00')).trunc()
-2
>>> RDF(RealNumber('0.00')).trunc()
0
RDF(2.99).trunc()
RDF(-2.00).trunc()
RDF(0.00).trunc()
ulp()[source]

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: a = RDF(pi)                                                           # needs sage.symbolic
sage: a.ulp()                                                               # needs sage.symbolic
4.440892098500626e-16
sage: b = a + a.ulp()                                                       # needs sage.symbolic
>>> 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
a = RDF(pi)                                                           # needs sage.symbolic
a.ulp()                                                               # needs sage.symbolic
b = a + a.ulp()                                                       # needs sage.symbolic

Adding or subtracting an ulp always gives a different number:

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
>>> 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
# needs sage.symbolic
a + a.ulp() == a
a - a.ulp() == a
b + b.ulp() == b
b - b.ulp() == b

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: # 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
>>> 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
# needs sage.symbolic
a - a.ulp()/3 == a
a + a.ulp()/3 == a
b - b.ulp()/3 == b
b + b.ulp()/3 == b
c = RDF(1)
c - c.ulp()/3 == c
c.ulp()
(c - c.ulp()).ulp()

The ulp is always positive:

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

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

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

Some special values:

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
>>> 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
a = RDF(1)/RDF(0); a
a.ulp()
(-a).ulp()
a = RDF('nan')
a.ulp() is a

The ulp method works correctly with small numbers:

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
>>> 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
u = RDF(0).ulp()
u.ulp() == u
x = u * (2^52-1)  # largest denormal number
x.ulp() == u
x = u * 2^52  # smallest normal number
x.ulp() == u
sage.rings.real_double.RealDoubleField()[source]

Return the unique instance of the real double field.

EXAMPLES:

sage: RealDoubleField() is RealDoubleField()
True
>>> from sage.all import *
>>> RealDoubleField() is RealDoubleField()
True
RealDoubleField() is RealDoubleField()
class sage.rings.real_double.RealDoubleField_class[source]

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: RR == RDF                                                                 # needs sage.rings.real_mpfr
False
sage: RDF == RealDoubleField()    # RDF is the shorthand
True
>>> from sage.all import *
>>> RR == RDF                                                                 # needs sage.rings.real_mpfr
False
>>> RDF == RealDoubleField()    # RDF is the shorthand
True
RR == RDF                                                                 # needs sage.rings.real_mpfr
RDF == RealDoubleField()    # RDF is the shorthand
sage: RDF(1)
1.0
sage: RDF(2/3)
0.6666666666666666
>>> from sage.all import *
>>> RDF(Integer(1))
1.0
>>> RDF(Integer(2)/Integer(3))
0.6666666666666666
RDF(1)
RDF(2/3)
>>> from sage.all import *
>>> RDF(Integer(1))
1.0
>>> RDF(Integer(2)/Integer(3))
0.6666666666666666
RDF(1)
RDF(2/3)

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

sage: RDF(QQ['x'].0)
Traceback (most recent call last):
...
TypeError: cannot convert nonconstant polynomial
sage: RDF(QQ['x'](3))
3.0
>>> 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
RDF(QQ['x'].0)
RDF(QQ['x'](3))

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: # 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
>>> 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
# needs sage.rings.real_mpfr
a = RealField(200)(2).sqrt(); a
b = RDF(a); b
a.parent()(b)
a.parent()(b) == b
b == RR(a)
NaN()[source]

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN
>>> from sage.all import *
>>> RDF.NaN()
NaN
RDF.NaN()
algebraic_closure()[source]

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

EXAMPLES:

sage: RDF.algebraic_closure()                                               # needs sage.rings.complex_double
Complex Double Field
>>> from sage.all import *
>>> RDF.algebraic_closure()                                               # needs sage.rings.complex_double
Complex Double Field
RDF.algebraic_closure()                                               # needs sage.rings.complex_double
characteristic()[source]

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

EXAMPLES:

sage: RDF.characteristic()
0
>>> from sage.all import *
>>> RDF.characteristic()
0
RDF.characteristic()
complex_field()[source]

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

EXAMPLES:

sage: RDF.complex_field()                                                   # needs sage.rings.complex_double
Complex Double Field
>>> from sage.all import *
>>> RDF.complex_field()                                                   # needs sage.rings.complex_double
Complex Double Field
RDF.complex_field()                                                   # needs sage.rings.complex_double
construction()[source]

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: c, S = RDF.construction(); S
Rational Field
sage: RDF == c(S)
True
>>> from sage.all import *
>>> c, S = RDF.construction(); S
Rational Field
>>> RDF == c(S)
True
c, S = RDF.construction(); S
RDF == c(S)
euler_constant()[source]

Return Euler’s gamma constant to double precision.

EXAMPLES:

sage: RDF.euler_constant()
0.5772156649015329
>>> from sage.all import *
>>> RDF.euler_constant()
0.5772156649015329
RDF.euler_constant()
factorial(n)[source]

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

EXAMPLES:

sage: RDF.factorial(100)
9.332621544394415e+157
>>> from sage.all import *
>>> RDF.factorial(Integer(100))
9.332621544394415e+157
RDF.factorial(100)
gen(n=0)[source]

Return the generator of the real double field.

EXAMPLES:

sage: RDF.0
1.0
sage: RDF.gens()
(1.0,)
>>> from sage.all import *
>>> RDF.gen(0)
1.0
>>> RDF.gens()
(1.0,)
RDF.0
RDF.gens()
is_exact()[source]

Return False, because doubles are not exact.

EXAMPLES:

sage: RDF.is_exact()
False
>>> from sage.all import *
>>> RDF.is_exact()
False
RDF.is_exact()
log2()[source]

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

EXAMPLES:

sage: RDF.log2()
0.6931471805599453
sage: RDF(2).log()
0.6931471805599453
>>> from sage.all import *
>>> RDF.log2()
0.6931471805599453
>>> RDF(Integer(2)).log()
0.6931471805599453
RDF.log2()
RDF(2).log()
name()[source]

The name of self.

EXAMPLES:

sage: RDF.name()
'RealDoubleField'
>>> from sage.all import *
>>> RDF.name()
'RealDoubleField'
RDF.name()
nan()[source]

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN
>>> from sage.all import *
>>> RDF.NaN()
NaN
RDF.NaN()
ngens()[source]

Return the number of generators which is always 1.

EXAMPLES:

sage: RDF.ngens()
1
>>> from sage.all import *
>>> RDF.ngens()
1
RDF.ngens()
pi()[source]

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

EXAMPLES:

sage: RDF.pi()
3.141592653589793
sage: RDF.pi().sqrt()/2
0.8862269254527579
>>> from sage.all import *
>>> RDF.pi()
3.141592653589793
>>> RDF.pi().sqrt()/Integer(2)
0.8862269254527579
RDF.pi()
RDF.pi().sqrt()/2
prec()[source]

Return the precision of this real double field in bits.

Always returns 53.

EXAMPLES:

sage: RDF.precision()
53
>>> from sage.all import *
>>> RDF.precision()
53
RDF.precision()
precision()[source]

Return the precision of this real double field in bits.

Always returns 53.

EXAMPLES:

sage: RDF.precision()
53
>>> from sage.all import *
>>> RDF.precision()
53
RDF.precision()
random_element(min=-1, max=1)[source]

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

EXAMPLES:

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
>>> 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
RDF.random_element().parent() is RDF
-1 <= RDF.random_element() <= 1
100 <= RDF.random_element(min=100, max=110) <= 110
to_prec(prec)[source]

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: RDF.to_prec(52)                                                       # needs sage.rings.real_mpfr
Real Field with 52 bits of precision
sage: RDF.to_prec(53)
Real Double Field
>>> 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
RDF.to_prec(52)                                                       # needs sage.rings.real_mpfr
RDF.to_prec(53)
zeta(n=2)[source]

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

EXAMPLES:

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
>>> 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
RDF.zeta()
RDF.zeta(1)
RDF.zeta(5)
class sage.rings.real_double.ToRDF[source]

Bases: Morphism

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

EXAMPLES:

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
>>> 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
f = RDF.coerce_map_from(ZZ); f
f(4)
f = RDF.coerce_map_from(QQ); f
f(1/2)
f = RDF.coerce_map_from(int); f
f(3r)
f = RDF.coerce_map_from(float); f
f(3.5)
sage.rings.real_double.is_RealDoubleElement(x)[source]

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

EXAMPLES:

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
>>> 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
from sage.rings.real_double import is_RealDoubleElement
is_RealDoubleElement(RDF(3))
is_RealDoubleElement(RIF(3))                                              # needs sage.rings.real_interval_field