Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating e (base of the natural log) to high precision in 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.

like image 880
skroth Avatar asked Aug 24 '10 18:08

skroth


People also ask

How do I increase precision in Python?

Using “%”:- “%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming. Using format():- This is yet another way to format the string for setting precision.

How do you calculate E in Python?

The exp() function in Python allows users to calculate the exponential value with the base set to e. Note: e is a Mathematical constant, with a value approximately equal to 2.71828.

How do you do natural log in Python?

Use math.Call math. log(x) to return the natural logarithm of x .

What is the precision of float in Python?

Python Decimal default precision The Decimal has a default precision of 28 places, while the float has 18 places.


1 Answers

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')
like image 184
Mermoz Avatar answered Oct 10 '22 06:10

Mermoz