6.1 Mathematical constants

Module: sage.misc.constants

The following standard mathematical constants are defined in SAGE, along with support for coercing them into GAP, GP/PARI, KASH, Maxima, Mathematica, Maple, Octave, and Singular:

sage: pi
pi
sage: e             # base of the natural logarithm
e
sage: NaN           # Not a number
NaN
sage: golden_ratio
golden_ratio
sage: log2          # natural logarithm of the real number 2
log2
sage: euler_gamma   # Euler's gamma constant
euler_gamma
sage: catalan       # the Catalon constant
K
sage: khinchin      # Khinchin's constant
khinchin

Suppose for coercion into the various systems means that if, e.g., you want to create $ \pi$ in Maxima and Singular, you don't have to figure out the special notation for each system. You just type the following:

sage: pi.str()
'3.1415926535897931'
sage: maxima(pi)
%pi
sage: singular(pi)
3.1415926535897931
sage: gap(pi)
"3.1415926535897931"
sage: gp(pi)
3.141592653589793238462643383     # 32-bit
3.1415926535897932384626433832795028842   # 64-bit
sage: pari(pi)
3.141592653589793238462643383     # 32-bit
3.1415926535897932384626433832795028842   # 64-bit
sage: kash(pi)                    # optional
3.14159265358979323846264338328
sage: mathematica(pi)             # optional
Pi
sage: maple(pi)                   # optional
Pi
sage: octave(pi)                  # optional
3.14159

Arithmetic operations with constants also yield constants, which can be coerced into other systems or evaluated.

sage: a = pi + e*4/5; a
(pi + ((e*4)/5))
sage: maxima(a)
%pi + 4*%e/5
sage: a.str(15)      # 15 *bits* of precision
'5.31616'
sage: gp(a)
5.316218116357029426750873360            # 32-bit
5.3162181163570294267508733603616328824  # 64-bit
sage: mathematica(a)                     # optional
(4*E)/5 + Pi

Decimal expansions of constants

We can obtain floating point approximations to each of these constants by coercing into the real field with given precision. For example, to 200 decimal places we have the following:

sage: R = RealField(200); R
Real Field with 200 bits of precision

sage: R(pi)
3.1415926535897932384626433832795028841971693993751058209749445

sage: R(e)
2.7182818284590452353602874713526624977572470936999595749669679

sage: R(NaN)
NaN

sage: R(golden_ratio)
1.6180339887498948482045868343656381177203091798057628621354484

sage: R(log2)
0.69314718055994530941723212145817656807550013436025525412067998

sage: R(euler_gamma)
0.57721566490153286060651209008240243104215933593992359880576723

sage: R(catalan)
0.91596559417721901505460351493238411077414937428167213426649835

sage: R(khinchin)
2.6854520010653064453097148354817956938203822939944629530511514

Arithmetic with constants

sage: pp = pi+pi; pp
(pi + pi)
sage: R(pp)
6.2831853071795864769252867665590057683943387987502116419498890

sage: s = (1 + e^pi);s
(1 + (e^pi))
sage: R(s)
24.140692632779269005729086367948547380266106242600211993445043
sage: R(s-1)
23.140692632779269005729086367948547380266106242600211993445043

sage: l = (1-log2)/(1+log2);l
((1 - log2)/(1 + log2))
sage: R(l)
0.18123221829928249948761381864650311423330609774776013488055837

sage: pim = maxima(pi)
sage: maxima.eval('fpprec : 100')
'100'
sage: pim.bfloat()
3.1415926535897932384626433832795028841971693993751058209749445923078164062
86208998628034825342117068B0

Author Log:

Class: Brun

class Brun
Brun's constant is the sum of reciprocals of odd twin primes.

It is not known to very high precision; calculating the number using twin primes up to $ 10^16$ (Sebah 2002) gives the number $ 1.9021605831040$ .

sage: float(brun)
       1.902160583104
       sage: R = RealField(41); R
       Real Field with 41 bits of precision
       sage: R(brun)
       1.9021605831040
Brun( self)

Functions: floor

Special Functions: __float__,$  $ _mpfr_,$  $ _repr_

__float__( self)

sage: float(brun)
1.902160583104

_mpfr_( self, R)

sage: RealField(53)(brun)
Traceback (most recent call last):
...
NotImplementedError: Brun's constant only available up to 41 bits
sage: RealField(41)(brun)
1.9021605831040

Class: Catalan

class Catalan
A number appaering in combinatorics defined as the Dirichlet beta function evaluated at the number 2.

Catalan( self)

Functions: floor

Special Functions: _latex_,$  $ _mpfr_,$  $ _repr_

Class: Constant

class Constant

Special Functions: __call__,$  $ _interface_is_cached_

_interface_is_cached_( self)

Return False, since coercion of functions to interfaces is not cached.

We do not cache coercions of functions to interfaces, since the precision of the interface may change.

sage: gp(pi)
3.141592653589793238462643383              # 32-bit
3.1415926535897932384626433832795028842    # 64-bit
sage: old_prec = gp.set_precision(100)
sage: gp(pi)
3.1415926535897932384626433832795028841971693993751058209749445923078164062
86208998628034825342117068
sage: _ = gp.set_precision(old_prec)
sage: gp(pi)
3.141592653589793238462643383              # 32-bit
3.1415926535897932384626433832795028842    # 64-bit

Class: Constant_gen

class Constant_gen

Special Functions: __call__

Class: E

class E
The base of the natural logarithm.

sage: R(e)
2.7182818284590451
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(e)
2.7182818284590452353602874713526624977572470936999595749669679
sage: em = 1 + e^(1-e); em
(1 + (e^(1 - e)))
sage: R(em)
1.1793740787340171819619895873183164984596816017589156131573701
sage: maxima(e).float()
2.718281828459045
sage: t = mathematica(e)               # optional
sage: t                                # optional
E
sage: float(t)                         # optional
2.7182818284590451
E( self)

Functions: floor

Special Functions: __float__,$  $ _latex_,$  $ _mpfr_,$  $ _repr_

Class: EulerGamma

class EulerGamma
The limiting difference between the harmonic series and the natural logarithm.

       sage: R = RealField()
       sage: R(euler_gamma)
0.57721566490153287
       sage: R = RealField(200); R
       Real Field with 200 bits of precision
       sage: R(euler_gamma)
       0.57721566490153286060651209008240243104215933593992359880576723
       sage: eg = euler_gamma + euler_gamma;eg
       (euler_gamma + euler_gamma)
       sage: R(eg)
       1.1544313298030657212130241801648048620843186718798471976115345
EulerGamma( self)

Functions: floor

Special Functions: _latex_,$  $ _mpfr_,$  $ _repr_

Class: GoldenRatio

class GoldenRatio
The number (1+sqrt(5))/2

sage: gr = golden_ratio
sage: R(gr)
1.6180339887498949
sage: R = RealField(200)
sage: R(gr)
1.6180339887498948482045868343656381177203091798057628621354484
sage: grm = maxima(golden_ratio);grm
(sqrt(5) + 1)/2
sage: grm + grm
sqrt(5) + 1
sage: float(grm + grm)
3.2360679774997898
GoldenRatio( self)

Special Functions: __float__,$  $ _latex_,$  $ _mpfr_,$  $ _repr_

Class: Khinchin

class Khinchin
The geometric mean of the continued fraction expansion of any (almost any) real number.

sage: float(khinchin)
2.6854520010653062
sage: khinchin.str(100)
'2.6854520010653064453097148354831'
sage: m = mathematica(khinchin); m             # optional
Khinchin
sage: m.N(200)                                 # optional
2.6854520010653064453097148354817956938203822939944629530511523455572188595
371520028011411749318476979951534659052880900828976777164109630517925334832
596683818523154213321194996260393285220448194096180686641664289          #
32-bit
2.6854520010653064453097148354817956938203822939944629530511523455572188595
371520028011411749318476979951534659052880900828976777164109630517925334832
596683818523154213321194996260393285220448194096180686641664289308477880620
36073705  # 64-bit
Khinchin( self)

Functions: floor

Special Functions: __float__,$  $ _mpfr_,$  $ _repr_

Class: Log2

class Log2
The natural logarithm of the real number 2.

sage: log2
log2
sage: float(log2)
0.69314718055994529
sage: R(log2)
0.69314718055994529
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(log2)
0.69314718055994530941723212145817656807550013436025525412067998
sage: l = (1-log2)/(1+log2);l
((1 - log2)/(1 + log2))
sage: R(l)
0.18123221829928249948761381864650311423330609774776013488055837
sage: maxima(log2)
log(2)
sage: maxima(log2).float()
.6931471805599453
sage: gp(log2)
0.6931471805599453094172321215             # 32-bit
0.69314718055994530941723212145817656808   # 64-bit
Log2( self)

Functions: floor

Special Functions: __float__,$  $ _latex_,$  $ _mpfr_,$  $ _repr_

Class: Merten

class Merten
The Merten constant is related to the Twin Primes constant and appears in Merten's second theorem.

sage: float(merten)
0.26149721284764277
sage: R=RealField(200);R
Real Field with 200 bits of precision
sage: R(merten)
0.26149721284764278375542683860869585905156664826119920619206426
Merten( self)

Functions: floor

Special Functions: __float__,$  $ _mpfr_,$  $ _repr_

__float__( self)

sage: float(merten)
0.26149721284764277

_mpfr_( self, R)

sage: RealField(1000)(merten)
Traceback (most recent call last):
...
NotImplementedError: Merten's constant only available up to 320 bits
sage: RealField(320)(merten)
0.2614972128476427837554268386086958590515666482611992061920642139249245108
9736820971414263143424673

Class: NotANumber

class NotANumber
Not a Number
NotANumber( self)

Special Functions: _mpfr_,$  $ _repr_

Class: Pi

class Pi
The ratio of a circle's circumference to its diameter.

sage: pi
pi
sage: float(pi)
3.1415926535897931
sage: gp(pi)
3.141592653589793238462643383            # 32-bit
3.1415926535897932384626433832795028842  # 64-bit
sage: R(pi)
3.1415926535897931
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(pi)
3.1415926535897932384626433832795028841971693993751058209749445
sage: pp = pi+pi; pp
(pi + pi)
sage: R(pp)
6.2831853071795864769252867665590057683943387987502116419498890
sage: maxima(pi)
%pi
sage: maxima(pi).float()
3.141592653589793
Pi( self)

Functions: floor

Special Functions: __abs__,$  $ __float__,$  $ _latex_,$  $ _mathml_,$  $ _mpfr_,$  $ _repr_

Class: TwinPrime

class TwinPrime
The Twin Primes constant is defined as $ \prod 1 - 1/(p-1)^2$ for primes $ p > 2$ .

sage: float(twinprime)
       0.66016181584686962
       sage: R=RealField(200);R
       Real Field with 200 bits of precision
       sage: R(twinprime)
       0.66016181584686957392781211001455577843262336028473341331944814
TwinPrime( self)

Functions: floor

Special Functions: __float__,$  $ _mpfr_,$  $ _repr_

__float__( self)

sage: float(twinprime)
0.66016181584686962

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