12.9 Real Numbers

Module: sage.rings.real_field

Author Log:

Module-level Functions

__reduce__RealField( prec, sci_not, rnd)

__reduce__RealNumber( parent, x, [base=10])

is_RealField( x)

is_RealNumber( x)

mpfr_prec_max( )

mpfr_prec_min( )

Return the mpfr variable MPFR_PREC_MIN.

Class: RealField

class RealField
RealField(prec, sci_not, rnd):

INPUT: prec - (integer) precision; default = 53 prec is the number of bits used to represent the mantissa of a floating-point number. The precision can be any integer between mpfr_prec_min() and mpfr_prec_max(). In the current implementation, mpfr_prec_min() is equal to 2.

sci_not - (default: True) whether or not to display using scientific notation

rnd - (string) the rounding mode RNDN - round to nearest RNDZ - round towards zero RNDU - round towards plus infinity RNDD - round towards minus infinity

sage: RealField(10)
Real Field with 10 bits of precision
sage: RealField()
Real Field with 53 bits of precision
sage: RealField(100000)
Real Field with 100000 bits of precision

NOTE: The default precision is 53, since according to the GMP manual: "mpfr should be able to exactly reproduce all computations with double-precision machine floating-point numbers (double type in C), except the default exponent range is much wider and subnormal numbers are not implemented."

Functions: catalan_constant,$  $ characteristic,$  $ euler_constant,$  $ factorial,$  $ gen,$  $ gens,$  $ is_atomic_repr,$  $ is_finite,$  $ log2,$  $ name,$  $ ngens,$  $ pi,$  $ precision,$  $ rounding_mode,$  $ scientific_notation,$  $ zeta

catalan_constant( self)

Returns Catalan's constant to the precision of this field.

sage: RealField(100).catalan_constant()
0.91596559417721901505460351493252

characteristic( self)

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

sage: RealField(10).characteristic()
0

euler_constant( self)

Returns Euler's gamma constant to the precision of this field.

sage: RealField(100).euler_constant()
0.57721566490153286060651209008234

factorial( self, int n)

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

is_atomic_repr( self)

Returns True, to signify that elements of this field print without sums, so parenthesis aren't required, e.g., in coefficients of polynomials.

sage: RealField(10).is_atomic_repr()
True

is_finite( self)

Returns False, since the field of real numbers is not finite.

sage: RealField(10).is_finite()
False

log2( self)

Returns log(2) to the precision of this field.

sage: R=RealField(100)
sage: R.log2() 
0.69314718055994530941723212145798
sage: R(2).log()
0.69314718055994530941723212145798

pi( self)

Returns pi to the precision of this field.

sage: R = RealField(100)
sage: R.pi()
3.1415926535897932384626433832793
sage: R.pi().sqrt()/2
0.88622692545275801364908374167063

scientific_notation( self, status=None)

Set or return the scientific notation printing flag. If this flag is True then real numbers with this space as parent print using scientific notation.

INPUT:
    status -- (bool --) optional flag

zeta( self, n=2)

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

sage: R = RealField()
sage: R.zeta()
-1.0000000000000000
sage: R.zeta(1)
1.0000000000000000
sage: R.zeta(5)
Traceback (most recent call last):
...
ValueError: No 5th root of unity in self

Special Functions: __call__,$  $ __cmp__,$  $ __reduce__,$  $ _coerce_,$  $ _is_valid_homomorphism_,$  $ _latex_,$  $ _repr_

__reduce__( self)

sage: R = RealField(sci_not=1, prec=200, rnd='RNDU')
sage: loads(dumps(R)) == R
True

Class: RealNumber

class RealNumber
A real number.

Functions: acos,$  $ acosh,$  $ agm,$  $ asin,$  $ asinh,$  $ atan,$  $ atanh,$  $ ceil,$  $ ceiling,$  $ copy,$  $ cos,$  $ cosh,$  $ cube_root,$  $ erf,$  $ exp,$  $ exp10,$  $ exp2,$  $ floor,$  $ frac,$  $ gamma,$  $ integer_part,$  $ log,$  $ log10,$  $ log2,$  $ multiplicative_order,$  $ parent,$  $ prec,$  $ real,$  $ round,$  $ sign,$  $ sin,$  $ sincos,$  $ sinh,$  $ sqrt,$  $ str,$  $ tan,$  $ tanh,$  $ trunc,$  $ zeta

acos( self)

Returns the inverse cosine of this number

sage: q = R.pi()/3
sage: i = q.cos()
sage: i.acos() == q
True

acosh( self)

Returns the hyperbolic inverse cosine of this number

sage: q = R.pi()/2 
sage: i = q.cosh() ; i
2.5091784786580567
sage: i.acosh() == q
True

agm( self, 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 codeNaN.

asin( self)

Returns the inverse sine of this number

sage: q = R.pi()/5
sage: i = q.sin()
sage: i.asin() == q
True

asinh( self)

Returns the hyperbolic inverse sine of this number

sage: q = R.pi()/7 
sage: i = q.sinh() ; i
0.46401763049299094
sage: i.asinh() == q
True

atan( self)

Returns the inverse tangent of this number

sage: q = R.pi()/5
sage: i = q.tan()
sage: i.atan() == q
True

atanh( self)

Returns the hyperbolic inverse tangent of this number

sage: q = R.pi()/7 
sage: i = q.tanh() ; i
0.42091124104853489
sage: i.atanh() == q
True

ceil( self)

Returns the ceiling of this number

OUTPUT: integer

sage: (2.99).ceil()
3
sage: (2.00).ceil()
2
sage: (2.01).ceil()
3

cos( self)

Returns the cosine of this number

sage: t=R.pi()/2
sage: t.cos()
0.000000000000000061232339957367660

cosh( self)

Returns the hyperbolic cosine of this number

sage: q = R.pi()/12
sage: q.cosh()
1.0344656400955106

cube_root( self)

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

sage: r = 125.0; r.cube_root()
5.0000000000000000
sage: r = -119.0
sage: r.cube_root()^3 - r       # illustrates precision loss
-0.000000000000014210854715202004

erf( self)

Returns the value of the error function on self.

sage: R = RealField()
sage: R(6).erf()
1.0000000000000000

exp( self)

Returns $ e^\code{self}$

sage: r = 0.0
sage: r.exp()
1.0000000000000000

sage: r = 32.3
sage: a = r.exp(); a
106588847274864.47
sage: a.log()
32.299999999999997

sage: r = -32.3
sage: r.exp()
0.0000000000000093818445884986851

exp10( self)

Returns $ 10^\code{self}$

sage: r = 0.0
sage: r.exp10()
1.0000000000000000

sage: r = 32.0
sage: r.exp10()
100000000000000010000000000000000

sage: r = -32.3
sage: r.exp10()
0.0000000000000000000000000000000050118723362727556

exp2( self)

Returns $ 2^code{self}$

sage: r = 0.0
sage: r.exp2()
1.0000000000000000

sage: r = 32.0
sage: r.exp2()
4294967296.0000000

sage: r = -32.3
sage: r.exp2()
0.00000000018911724825302072

floor( self)

Returns the floor of this number

sage: R = RealField()
sage: (2.99).floor()
2
sage: (2.00).floor()
2
sage: floor(RR(-5/2))
-3

frac( self)

frac returns a real number > -1 and < 1. it satisfies the relation: x = x.trunc() + x.frac()

sage: (2.99).frac()
0.99000000000000021
sage: (2.50).frac()
0.50000000000000000
sage: (-2.79).frac()
-0.79000000000000004

gamma( self)

The Euler gamma function. Return gamma of self.

sage: R = RealField()                                                
sage: R(6).gamma()
120.00000000000000
sage: R(1.5).gamma()
0.88622692545275805

integer_part( self)

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

log( self)

sage: R = RealField()
sage: R(2).log()
0.69314718055994529

log10( self)

Returns log to the base 10 of self

sage: r = 16.0; r.log10()
1.2041199826559248
sage: r.log() / log(10)
1.2041199826559246

sage: r = 39.9; r.log10()
1.6009728956867482

sage: r = 0.0
sage: r.log10()
-infinity

sage: r = -1.0
sage: r.log10()
NaN

log2( self)

Returns log to the base 2 of self

sage: r = 16.0
sage: r.log2()
4.0000000000000000

sage: r = 31.9; r.log2()
4.9954845188775066

sage: r = 0.0
sage: r.log2()
-infinity

parent( self)

sage: R = RealField()
sage: a = R('1.2456')
sage: a.parent()
Real Field with 53 bits of precision

real( self)

Return the real part of self.

(Since self is a real number, this simply returns self.)

round( self)

Rounds self to the nearest real number. There are 4 rounding modes. They are

RNDN - round to nearest:

sage: R = RealField(10,False,'RNDN')
sage: R(22.454)
22.469
sage: R(22.491)
22.500

RNDZ - round towards zero:

sage: R = RealField(10,False,'RNDZ')
sage: R(22.454)
22.437
sage: R(22.491)
22.468

RNDU - round towards plus infinity:

sage: R10 = RealField(10,False,'RNDU')
sage: R10(22.454)
22.469
sage: R10(22.491)
22.500

RNDU - round towards minus infinity:

sage: R10 = RealField(10,False,'RNDD')
sage: R10(22.454)
22.437
sage: R10(22.491)
22.468

sin( self)

Returns the sine of this number

sage: R = RealField(100)
sage: R(2).sin()
0.90929742682568169539601986591150

sincos( self)

Returns a pair consisting of the sine and cosine.

sage: R = RealField()
sage: t = R.pi()/6
sage: t.sincos()
(0.49999999999999994, 0.86602540378443871)

sinh( self)

Returns the hyperbolic sine of this number

sage: q = R.pi()/12
sage: q.sinh()
0.26480022760227073

sqrt( self)

Return the square root of self.

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

sage: r = 4344
sage: r.sqrt()
65.909028213136324
sage: r.sqrt()^2 == r
True

sage: r = -2.0
sage: r.sqrt()
NaN

tan( self)

Returns the tangent of this number

sage: q = R.pi()/3
sage: q.tan()
1.7320508075688767
sage: q = R.pi()/6
sage: q.tan()
0.57735026918962573

tanh( self)

Returns the hyperbolic tangent of this number

sage: q = R.pi()/11
sage: q.tanh()
0.27807942929585028

trunc( self)

Truncates this number

sage: (2.99).trunc()
2.0000000000000000
sage: (-0.00).trunc()
-0.00000000000000000
sage: (0.00).trunc()
0.00000000000000000

zeta( self, n=2)

Return the Riemann zeta function evaluated at this real number.

Note: PARI is vastly more efficient at computing the Riemann zeta function. See the example below for how to use it.

sage: R = RealField()                                                
sage: R(2).zeta()
1.6449340668482264
sage: R.pi()^2/6
1.6449340668482264
sage: R(-2).zeta()
0.00000000000000000
sage: R(1).zeta()
+infinity

Computing zeta using PARI is much more efficient in difficult cases. Here's how to compute zeta with at least a given precision:

sage: z = pari.new_with_bits_prec(2, 53).zeta(); z
1.644934066848226436472415167              # 32-bit
1.6449340668482264364724151666460251892    # 64-bit

Note that the number of bits of precision in the constructor only effects the internel precision of the pari number, not the number of digits that gets displayed. To increase that you must use pari.set_real_precision.

sage: type(z)
<type 'gen.gen'>
sage: R(z)
1.6449340668482264

Special Functions: __abs__,$  $ __cmp__,$  $ __complex__,$  $ __div_,$  $ __div__,$  $ __eq__,$  $ __float__,$  $ __ge__,$  $ __gt__,$  $ __int__,$  $ __invert__,$  $ __le__,$  $ __long__,$  $ __lshift__,$  $ __lt__,$  $ __ne__,$  $ __neg__,$  $ __pos__,$  $ __pow,$  $ __pow__,$  $ __rdiv__,$  $ __reduce__,$  $ __repr__,$  $ __rlshift__,$  $ __rpow__,$  $ __rrshift__,$  $ __rshift__,$  $ _add_,$  $ _im_gens_,$  $ _interface_init_,$  $ _latex_,$  $ _lshift_,$  $ _mul_,$  $ _rshift_,$  $ _sub_

__reduce__( self)

sage: R = RealField(sci_not=1, prec=200, rnd='RNDU')
sage: b = R('393.39203845902384098234098230948209384028340')
sage: loads(dumps(b)) == b
True
sage: b = R(1)/R(0); b
+infinity
sage: loads(dumps(b)) == b
True
sage: b = R(-1)/R(0); b
-infinity
sage: loads(dumps(b)) == b
True
sage: b = R(-1).sqrt(); b
NaN
sage: loads(dumps(b)) == b
True

_add_( RealNumber self, RealNumber other)

Add two real numbers with the same parent.

sage: R = RealField()
sage: R(-1.5) + R(2.5)
1.0000000000000000

_interface_init_( self)

Return string representation of self in base 10 with no scientific notation.

This is most likely to make sense in other computer algebra systems (this function is the default for exporting to other computer algebra systems).

sage: n = 1.3939494594
sage: n._interface_init_()
'1.3939494593999999'

_mul_( RealNumber self, RealNumber other)

Multiply two real numbers with the same parent.

sage: R = RealField()
sage: R(-1.5) * R(2.5)
-3.7500000000000000

If two elements have different precision, arithmetic operations are performed after coercing to the lower precision.

sage: R10 = RealField(10)
sage: R100 = RealField(100)
sage: a = R10('393.3902834028345')
sage: b = R100('393.3902834028345')
sage: a
393.50
sage: b
393.39028340283450000000000000011
sage: a*b
154880
sage: b*a
154880
sage: parent(b*a)
Real Field with 10 bits of precision

_sub_( RealNumber self, RealNumber other)

Subtract two real numbers with the same parent.

sage: R = RealField()
sage: R(-1.5) - R(2.5)
-4.0000000000000000

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