# Double Precision Real Numbers#

EXAMPLES:

We create the real double vector space of dimension $$3$$:

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


Notice that this space is unique:

sage: V is RDF^3
True
sage: V is FreeModule(RDF, 3)
True
sage: V is VectorSpace(RDF, 3)
True


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

sage: V = RDF^10000

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: RDF.NaN()
NaN

abs()#

Returns the absolute value of self.

EXAMPLES:

sage: RDF(1e10).abs()
10000000000.0
sage: RDF(-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: a = RDF(1.5)
sage: b = RDF(2.3)
sage: a.agm(b)
1.8786484558146697


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

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

algdep(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: r = sqrt(RDF(2)); r
1.4142135623730951
sage: r.algebraic_dependency(5)
x^2 - 2

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: r = sqrt(RDF(2)); r
1.4142135623730951
sage: r.algebraic_dependency(5)
x^2 - 2

as_integer_ratio()#

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)

ceil()#

Return the ceiling of self.

EXAMPLES:

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

ceiling()#

Return the ceiling of self.

EXAMPLES:

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

conjugate()#

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

EXAMPLES:

sage: RDF(4).conjugate()
4.0

cube_root()#

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

floor()#

Return the floor of self.

EXAMPLES:

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

frac()#

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

imag()#

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

EXAMPLES:

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

integer_part()#

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

is_NaN()#

Check if self is NaN.

EXAMPLES:

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

is_infinity()#

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

is_integer()#

Return True if this number is a integer

EXAMPLES:

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

is_negative_infinity()#

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

is_positive_infinity()#

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

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 non-negative.

EXAMPLES:

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

multiplicative_order()#

Returns $$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

nan()#

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN

prec()#

Return the precision of this number in bits.

Always returns 53.

EXAMPLES:

sage: RDF(0).prec()
53

real()#

Return self - we are already real.

EXAMPLES:

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

round()#

Given real number $$x$$, rounds up if fractional part is greater than $$0.5$$, rounds down if fractional part is less than $$0.5$$.

EXAMPLES:

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

sign()#

Returns -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

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: 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


The mantissa is always a nonnegative number:

sage: RDF(-1).sign_mantissa_exponent()
(-1, 4503599627370496, -52)

sqrt(extend=True, all=False)#

The square root function.

INPUT:

• extend – bool (default: True); if True, return a square root in a complex field if necessary if self is negative; otherwise raise a ValueError.

• all – bool (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

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

sage: r = RDF(-2.0)
sage: r.sqrt()
1.4142135623730951*I

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

str()#

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'


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

trunc()#

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

ulp()#

Returns 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)
sage: a.ulp()
4.440892098500626e-16
sage: b = a + a.ulp()


Adding or subtracting an ulp always gives a different number:

sage: a + a.ulp() == a
False
sage: a - a.ulp() == a
False
sage: b + b.ulp() == b
False
sage: 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: 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


The ulp is always positive:

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


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

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


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


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

sage.rings.real_double.RealDoubleField()#

Return the unique instance of the real double field.

EXAMPLES:

sage: 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: RR == RDF
False
sage: RDF == RealDoubleField()    # RDF is the shorthand
True

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


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


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: 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

NaN()#

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN

algebraic_closure()#

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

EXAMPLES:

sage: RDF.algebraic_closure()
Complex Double Field

characteristic()#

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

EXAMPLES:

sage: RDF.characteristic()
0

complex_field()#

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

EXAMPLES:

sage: RDF.complex_field()
Complex Double Field

construction()#

Returns 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

euler_constant()#

Return Euler’s gamma constant to double precision.

EXAMPLES:

sage: RDF.euler_constant()
0.5772156649015329

factorial(n)#

Return the factorial of the integer $$n$$ as a real number.

EXAMPLES:

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

gen(n=0)#

Return the generator of the real double field.

EXAMPLES:

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

is_exact()#

Returns False, because doubles are not exact.

EXAMPLES:

sage: RDF.is_exact()
False

log2()#

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

EXAMPLES:

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

name()#

The name of self.

EXAMPLES:

sage: RDF.name()
'RealDoubleField'

nan()#

Return Not-a-Number NaN.

EXAMPLES:

sage: RDF.NaN()
NaN

ngens()#

Return the number of generators which is always 1.

EXAMPLES:

sage: RDF.ngens()
1

pi()#

Returns $$\pi$$ to double-precision.

EXAMPLES:

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

prec()#

Return the precision of this real double field in bits.

Always returns 53.

EXAMPLES:

sage: RDF.precision()
53

precision()#

Return the precision of this real double field in bits.

Always returns 53.

EXAMPLES:

sage: 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: 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

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: RDF.to_prec(52)
Real Field with 52 bits of precision
sage: RDF.to_prec(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: 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

class sage.rings.real_double.ToRDF#

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

sage.rings.real_double.is_RealDoubleElement(x)#

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))
True
sage: is_RealDoubleElement(RIF(3))
False

sage.rings.real_double.is_RealDoubleField(x)#

Returns True if x is the field of real double precision numbers.

This function is deprecated. Use isinstance() with RealDoubleField instead.

EXAMPLES:

sage: from sage.rings.real_double import is_RealDoubleField
sage: is_RealDoubleField(RDF)
doctest:warning...
DeprecationWarning: is_RealDoubleField is deprecated;