0
how to write a program in python to find e**x using while function
2 Respuestas
0
Hello
0
do you really want to use while?
if not it can be calculated using->
import math
x=3
print(math.e**x)
using while (calculating to nearest N digits)
import math
x=5
N=3
c0=0.0
c1=-10.0
i=0
while round(c0,N+1)!=round(c1,N+1):
c1=c0
c0+=x**i/ math.factorial (i)
i+=1
print(("{:."+str(N+len(str(int(c0))))+"}").format(c0))
#you can also use print(c0) too but it will give c0 without rounding it
#using print(round(c0,N)) is also OK but it will not give you the exact rounded number
this is not a good solution and sorry about it
this may be much slower when N and x are huge
so using first method is okay I think