Python Forum

Full Version: Calculate the Euler Number with more decimal places
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I was reading about the Euler Number, e.

I read it has been calculated to a trillion decimal places.

I would be happy with 100 decimal places.

I have 15 decimal places.

How could I get more decimal places?

def euler(n):
    print('Potential is:', n)
    inner = 1 + 1/n
    print('inner value is:', inner)
    e = inner**n
    print('e = ', e)
You could use a specialized library such as gmpy2
>>> import gmpy2
>>> with gmpy2.local_context(gmpy2.context(), precision=2000) as ctx:
...     e = gmpy2.exp(gmpy2.mpfr('1'))
...     print(e)
... 
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117303
Thanks!

That's great!
Boohoo, can't install the module using pip!

Can I get gmpy2 from another source?

Output in bash:

Quote:In file included from src/gmpy2.c:426:
src/gmpy.h:106:12: fatal error: gmp.h: No such file or directory
106 | # include "gmp.h"
| ^~~~~~~
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
Seems like you need to install GMP and presumably the other dependencies. Did you follow the instructions in the docs?
What linux distribution do you have ?
In Ubuntu I have a package python3-gmpy2, also if I only want the header gmp.h, I can install the package libgmp-dev. The Gnu Multiprecision Library can be installed on any Linux computer.

Also note that other libraries exist for multiprecision, such as mpmath, which is used by sympy and sage I think, and there is also bigfloat and clnum.
>>> from sympy import *
>>> x = symbols('x')
>>> exp(x).evalf(100, subs={x: Float('1.0')})
2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427
>>> 
Thanks for your replies!

I use Ubuntu 20.04

I only tried to install gmpy2 using pip.

I'm not very clear about installing from source, I never do that, but I suppose I will have to try!
You might want to consider Anaconda ( https://www.anaconda.com/products/individual) instead of installing everything on your own distribution, I think it makes life easier and you have almost everything out of the box
(Oct-18-2021, 11:09 PM)Pedroski55 Wrote: [ -> ]I was reading about the Euler Number, e.

I read it has been calculated to a trillion decimal places.

I would be happy with 100 decimal places.

I have 15 decimal places.

How could I get more decimal places?

def euler(n):
    print('Potential is:', n)
    inner = 1 + 1/n
    print('inner value is:', inner)
    e = inner**n
    print('e = ', e)

Hello, I just started learning Python because I thought that it had libraries which would let me do calculations with lots of decimal places.
If you go to this URL, it starts with a simple program in Java.
Then in the second half it shows how I used mpmath in Python to calculate the square root of two to one million decimal places:
https://andrews-programming-examples.blo...ulate.html
(Oct-22-2021, 12:17 AM)Pedroski55 Wrote: [ -> ]Thanks for your replies!

I use Ubuntu 20.04

I only tried to install gmpy2 using pip.

I'm not very clear about installing from source, I never do that, but I suppose I will have to try!

I happen to be the maintainer of gmpy2. I'm sorry that you have issues installing gmpy2. Here are my recommendations.

If you are using a version of Python provided by a Linux distribution, I recommend installing the version of gmpy2 provided by that distribution. For Ubuntu you can use a GUI tool like "synaptic" or use the following command from the Linux command prompt:

sudo apt install python3-gmpy2

Installing from PyPi using pip is unfortunately not as easy as it should be. The last "official" release was version 2.0.8. Since then, I've released several alpha, beta, and release candidate versions of 2.1.0, but pip will only offer official releases and hides the existence of development versions. You can force pip to provide development versions using the following command:

pip install --pre gmpy2

Installing from source is relatively easy on Linux. But I would only do this both of the previous options fail. gmpy2 requires the development files for GMP (provides mpz and mpq), MPFR (provides mpfr), and MPC (provides mpc). The easiest way to install the required files is the following command:

sudo apt install libmpc-dev

Then try compiling from source with the command:

pip install --pre --no-binary :all: gmpy2

I hope this helps.

Case

p.s. My first multiple precision calculation was calculating e to 3,110 decimal places on an HP41-CV calculator. It took 5 days. My effort was inspired by article in Byte magazine written by Steve Wozniak.

https://downloads.reactivemicro.com/User...201981.pdf
Pages: 1 2