Python Forum
Calculate the Euler Number with more decimal places
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Calculate the Euler Number with more decimal places
#1
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)
Reply
#2
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
Pedroski55 likes this post
Reply
#3
Thanks!

That's great!
Reply
#4
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
Reply
#5
Seems like you need to install GMP and presumably the other dependencies. Did you follow the instructions in the docs?
Pedroski55 likes this post
Reply
#6
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
>>> 
Pedroski55 likes this post
Reply
#7
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!
Reply
#8
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
Pedroski55 likes this post
Reply
#9
(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
Pedroski55 likes this post
Reply
#10
(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
Gribouillis likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Not rounding to desired decimal places? pprod 2 2,506 Mar-05-2021, 11:11 AM
Last Post: pprod
  Can I format decimal places by column with a dictionary? Mark17 2 2,514 Dec-28-2020, 10:13 PM
Last Post: Mark17
  Creating table in MySQL db with decimal number issue dangermaus33 7 4,782 Nov-20-2020, 10:40 PM
Last Post: dangermaus33
  IndexError in Project Euler attempt CRISPRmetoopls 3 2,270 Aug-10-2020, 10:00 AM
Last Post: perfringo
  Numpy savetxt, how save number with decimal separator SpongeB0B 1 3,161 May-10-2020, 01:05 PM
Last Post: ThomasL
  How show decimal only for none whole number ? SpongeB0B 6 2,558 Mar-27-2020, 09:15 AM
Last Post: DeaD_EyE
  3rd problem from project Euler Richard_SS 6 3,119 Jun-05-2019, 02:06 PM
Last Post: DeaD_EyE
  testing for Decimal w/o importing decimal every time Skaperen 7 4,363 May-06-2019, 10:23 PM
Last Post: Skaperen
  graphing euler method rondo 1 2,462 May-03-2019, 01:03 AM
Last Post: scidam
  Search "Places near by me" or "where am I" in google maps barry76 1 2,650 Feb-07-2019, 04:10 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020