Calculating e (base of the natural log) to high precision in Python? - python

Is it possible to calculate the value of the mathematical constant, e with high precision (2000+ decimal places) using Python?
I am particularly interested in a solution either in or that integrates with NumPy or SciPy.

You can set the precision you want with the decimal built-in module:
from decimal import *
getcontext().prec = 40
Decimal(1).exp()
This returns:
Decimal('2.718281828459045235360287471352662497757')

This can also be done with sympy using numerical evaluation:
import sympy
print sympy.N(sympy.E, 100)

Using a series sum you could calculate it:
getcontext().prec = 2000
e = Decimal(0)
i = 0
while True:
fact = math.factorial(i)
e += Decimal(1)/fact
i += 1
if fact > 10**2000: break
But that's not really necessary, as what Mermoz did agrees just fine with it:
>>> e
Decimal('2.7182818284590452353602874713526624977572470936999595749669676
277240766303535475945713821785251664274274663919320030599218174135966290
435729003342952605956307381323286279434907632338298807531952510190115738
341879307021540891499348841675092447614606680822648001684774118537423454
424371075390777449920695517027618386062613313845830007520449338265602976
067371132007093287091274437470472306969772093101416928368190255151086574
637721112523897844250569536967707854499699679468644549059879316368892300
987931277361782154249992295763514822082698951936680331825288693984964651
058209392398294887933203625094431173012381970684161403970198376793206832
823764648042953118023287825098194558153017567173613320698112509961818815
930416903515988885193458072738667385894228792284998920868058257492796104
841984443634632449684875602336248270419786232090021609902353043699418491
463140934317381436405462531520961836908887070167683964243781405927145635
490613031072085103837505101157477041718986106873969655212671546889570350
354021234078498193343210681701210056278802351930332247450158539047304199
577770935036604169973297250886876966403555707162268447162560798826517871
341951246652010305921236677194325278675398558944896970964097545918569563
802363701621120477427228364896134225164450781824423529486363721417402388
934412479635743702637552944483379980161254922785092577825620926226483262
779333865664816277251640191059004916449982893150566047258027786318641551
956532442586982946959308019152987211725563475463964479101459040905862984
967912874068705048958586717479854667757573205681288459205413340539220001
137863009455606881667400169842055804033637953764520304024322566135278369
511778838638744396625322498506549958862342818997077332761717839280349465
014345588970719425863987727547109629537415211151368350627526023264847287
039207643100595841166120545297030236472549296669381151373227536450988890
313602057248176585118063036442812314965507047510254465011727211555194866
850800368532281831521960037356252794495158284188294787610852639810')
>>> Decimal(1).exp()
Decimal('2.7182818284590452353602874713526624977572470936999595749669676
277240766303535475945713821785251664274274663919320030599218174135966290
435729003342952605956307381323286279434907632338298807531952510190115738
341879307021540891499348841675092447614606680822648001684774118537423454
424371075390777449920695517027618386062613313845830007520449338265602976
067371132007093287091274437470472306969772093101416928368190255151086574
637721112523897844250569536967707854499699679468644549059879316368892300
987931277361782154249992295763514822082698951936680331825288693984964651
058209392398294887933203625094431173012381970684161403970198376793206832
823764648042953118023287825098194558153017567173613320698112509961818815
930416903515988885193458072738667385894228792284998920868058257492796104
841984443634632449684875602336248270419786232090021609902353043699418491
463140934317381436405462531520961836908887070167683964243781405927145635
490613031072085103837505101157477041718986106873969655212671546889570350
354021234078498193343210681701210056278802351930332247450158539047304199
577770935036604169973297250886876966403555707162268447162560798826517871
341951246652010305921236677194325278675398558944896970964097545918569563
802363701621120477427228364896134225164450781824423529486363721417402388
934412479635743702637552944483379980161254922785092577825620926226483262
779333865664816277251640191059004916449982893150566047258027786318641551
956532442586982946959308019152987211725563475463964479101459040905862984
967912874068705048958586717479854667757573205681288459205413340539220001
137863009455606881667400169842055804033637953764520304024322566135278369
511778838638744396625322498506549958862342818997077332761717839280349465
014345588970719425863987727547109629537415211151368350627526023264847287
039207643100595841166120545297030236472549296669381151373227536450988890
313602057248176585118063036442812314965507047510254465011727211555194866
850800368532281831521960037356252794495158284188294787610852639814')

The excellent pure-python library, Mpmath, will certainly do the trick.
The sole focus of this library is multi-precision floating-point arithmetic.
E.g., Mpath can evaluate e to arbitrary precision:
In [2]: from mpmath import *
# set the desired precision on the fly
In [3]: mp.dps=20; mp.pretty=True
In [4]: +e
Out[4]: 2.7182818284590452354
# re-set the precision (50 digits)
In [5]: mp.dps=50; mp.pretty=True
In [6]: +e
Out[6]: 2.7182818284590452353602874713526624977572470937
As an aside, Mpmath is also tightly integrated with Matplotlib.

I would think you could combine the info from these webpages:
http://en.wikipedia.org/wiki/Taylor_series
This gives you the familiar power series. Since you're working with large factorial numbers you should then probably work with gmpy which implements multi-precision arithmetic.

Using Sage:
N(e, digits=2000)

Related

How to get value for 0.5^(5000) in Python3?

Current I want to calculate the value of a=0.5**5000. I knowa is very small value, but I need use a to multiply b, where b = 5000!, a very large value.
But when I calculate value of a=0.5^5000, the result is 0. Does anyone have a good method to solve this problem? Thanks!
I need use a to multiply b, where b = 5000!
I'd just divide 5000! by 25000 or shift it:
from math import factorial
print(factorial(5000) // 2**5000)
print(factorial(5000) >> 5000)
Output:
299375336...(more than 14000 digits)...080261230
Or as #wim points out, fractions.Fraction would be exact:
print(Fraction(factorial(5000), 2**5000))
Output:
958001075...(more than 14000 digits)...568359375/32
Which in decimal notation is:
299375336...(more than 14000 digits)...080261230.46875
So in this case that gives us only five more digits of precision, not much compared to already over 14000. But for example for 5!/25 it would be 3.75 instead of 3, quite a big difference. Then again, this small number (if you're using it as well) might be insignificant in what you're doing overall. Which we don't know because you only told us about this tiny part of your attempt to do whatever it is you're actually after.
Try the decimal module:
>>> from decimal import Decimal
>>> print(Decimal("0.5") ** 5000)
7.079811261048172892385615159E-1506
>>>
Here's an alternative answer using the mpmath library:
import mpmath
a = mpmath.power(0.5, 5000)
b = mpmath.factorial(5000)
c = mpmath.fmul(a, b)
print(a)
print(b)
print(c)
That includes the other calculations that you mentioned.
Output:
7.0798112610481728923856151586941e-1506
4.2285779266055435222010642002336e+16325
2.9937533623001661362907254353912e+14820
Use Decimal :
from decimal import Decimal
if __name__ == '__main__':
a = Decimal(str(0.5)) ** Decimal(str(5000))
print(a)
7.079811261048172892385615159E-1506

How to correctly deal with floating point arithmetic in Python?

How to correctly add or subtract using floats?
For example how to perform:
2.4e-07 - 1e-8
so that it returns 2.3e-7 instead of 2.2999999999999997e-07.
Converting to int first yields unexpected results, the below returns 2.2e-07:
int(2.4e-07 * 1e8 - 1) * 1e-8
Similarly,
(2.4e-07 * 1e8 - 1) * 1e-8
returns 2.2999999999999997e-07.
How to perform subtraction and addition of numbers with 8 decimal point precision?
2.2999999999999997e-07 is not sufficient as the number is used as a lookup in a dictionary, and the key is 2.3e-7. This means that any value other than 2.3e-7 results in an incorrect lookup.
I suggest using the decimal data type (it is present in the stardard installation of Python), because it uses fixed precision to avoid just the differences you are talking about.
>>> from decimal import Decimal
>>> x = Decimal('2.4e-7')
>>> x
Decimal('2.4E-7')
>>> y = Decimal('1e-8')
>>> y
Decimal('1E-8')
>>> x - y
Decimal('2.3E-7')
It's really just a way of skirting around the issue of floating point arithmetic, but I suggest using the decimal package from the standard library. It lets you do exact floating point math.
Using your example,
$ from decimal import Decimal
$ x = Decimal('2.4e-7')
$ y = Decimal('1e-8')
$ x-y
Decimal('2.3E-7')
It's worth noting that Decimal objects are different than the float built-in, but they are mostly interchangeable.
I do not know if it is what you are looking for but you can try that kind of thing:
a = 0.555555555
a = float("{0:.2f}".format(a))
>>> 0.56
I hope it will help you!
Adrien

adjusting floats to maintain high precision

I am writing some Python code that requires a very high degree of precision. I started to use Numpy float64, but that didn't work as required, and I then started using the "Decimal" module, which then worked fine.
I would ideally prefer, however, to use floats rather than use the decimal module - and I recall someone once telling me that it's possible manipulate floats in some way so that the level of precision can be achieved (by multiplying or something?).
Is this true or am I misremembering? Sorry if this is a little vague.
Thank you!
It depends on the kind of number you have. For example if you are adding values in the interval [1...2) you might be better of using offsettet values:
>>> a = 1.0000000000000000001
>>> a
1.0
>>> a+a
1.0
>>> a = 0.0000000000000000001
>>> a
1e-19
For simpler storage you can write them as tuple (n, f) with n being a natural number (int) and f the fraction in the interval [0...1).
Computation with such kind of values is tricky however.
>>> (1+1, a+a)
(2, 2e-19)
If in doubt stick with Decimal or use bigfloat as suggested by BenDundee.
Another useful package is mpmath:
import mpmath as mp
p.dps = 64 #64 decimal places
msqrt(3)/2
mpf('0.8660254037844386467637231707529361834714026269051903140279034897246')
p.dps = 30 #30 decimal places
mpf('0.866025403784438646763723170752918')

how can i show an irrational number to 100 decimal places in python?

I am trying to find the square root of 2 to 100 decimal places, but it only shows to like 10 by default, how can I change this?
decimal module comes in handy.
>>> from decimal import *
>>> getcontext().prec = 100
>>> Decimal(2).sqrt()
Decimal('1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641573')
You can use the decimal module for arbitrary precision numbers:
import decimal
d2 = decimal.Decimal(2)
# Add a context with an arbitrary precision of 100
dot100 = decimal.Context(prec=100)
print d2.sqrt(dot100)
If you need the same kind of ability coupled to speed, there are some other options: [gmpy], 2, cdecimal.
You can use gmpy2.
import gmpy2
ctx = gmpy2.get_context()
ctx.precision = 300
print(gmpy2.sqrt(2))
You can use sympy and evalf()
from sympy import sqrt
print(sqrt(2).evalf(101))

Operations for Long and Float in Python

I'm trying to compute this:
from scipy import *
3600**3400 * (exp(-3600)) / factorial(3400)
the error: unsupported long and float
Try using logarithms instead of working with the numbers directly. Since none of your operations are addition or subtraction, you could do the whole thing in logarithm form and convert back at the end.
Computing with numbers of such magnitude, you just can't use ordinary 64-bit-or-so floats, which is what Python's core runtime supports. Consider gmpy (do not get the sourceforge version, it's aeons out of date) -- with that, math, and some care...:
>>> e = gmpy.mpf(math.exp(1))
>>> gmpy.mpz(3600)**3400 * (e**(-3600)) / gmpy.fac(3400)
mpf('2.37929475533825366213e-5')
(I'm biased about gmpy, of course, since I originated and still participate in that project, but I'd never make strong claims about its floating point abilities... I've been using it mostly for integer stuff... still, it does make this computation possible!-).
You could try using the Decimal object. Calculations will be slower but you won't have trouble with really small numbers.
from decimal import Decimal
I don't know how Decimal interacts with the scipy module, however.
This numpy discussion might be relevant.
Well the error is coming about because you are trying to multiply
3600**3400
which is a long with
exp(-3600)
which is a float.
But regardless, the error you are receiving is disguising the true problem. It seems exp(-3600) is too big a number to fit in a float anyway. The python math library is fickle with large numbers, at best.
exp(-3600) is too smale, factorial(3400) is too large:
In [1]: from scipy import exp
In [2]: exp(-3600)
Out[2]: 0.0
In [3]: from scipy import factorial
In [4]: factorial(3400)
Out[4]: array(1.#INF)
What about calculate it step by step as a workaround(and it makes sense
to check the smallest and biggest intermediate result):
from math import exp
output = 1
smallest = 1e100
biggest = 0
for i,j in izip(xrange(1, 1701), xrange(3400, 1699, -1)):
output = output * 3600 * exp(-3600/3400) / i
output = output * 3600 * exp(-3600/3400) / j
smallest = min(smallest, output)
biggest = max(biggest, output)
print "output: ", output
print "smallest: ", smallest
print "biggest: ", biggest
output is:
output: 2.37929475534e-005
smallest: 2.37929475534e-005
biggest: 1.28724174494e+214

Categories

Resources