0

I am working on a project that generates an infinite number like PI (3.141...etc) or Euler's number(2.718...etc). It asks the user how many digits they would like to generate then runs an algorithm that will accurately generate that many digits.

Coding the algorithms went well and they work as desired. Now I am attempting to implement a way to control the number of digits that are printed. I am doing this using the getcontext() function from the decimal module. Here is the Euler's number function for example.

2.7182818284590452353602874713527 <--- (Euler's number for reference)

from math import factorial
from decimal import getcontext, Decimal
e = 0
def euler_gen:
  for n in range(0,100):
    n = factorial(n)
    e += 1/n

    getcontext().prec = 5
    print(Decimal(e))

The intended output is 2.7182. Instead 52 digits are printed. Am I misusing the function or is there a differant solution you would recommend for solving this issue?

0 Answers0