Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use python to calculate a special limit

I want to calculate this expression:

(1 + 1 / math.inf) ** math.inf,

which should evaluates to e. However Python returns 1. Why is that?

=====UPDATE========

What I want to do here is to derive the effective annual rate from user's input, APR (annual percentage rate).

def get_EAR(APR, conversion_times_per_year = 1):
    return (1 + APR / conversion_times) ** conversion_times - 1

I would want this expression to also apply to continuous compounding. Yes I understand I can write if statements to differentiate continuous compounding from normal cases (and then I can use the constant e directly), but I would better prefer an integrated way.

like image 735
Ryan Avatar asked Dec 23 '22 19:12

Ryan


1 Answers

The calculation of limits is not implemented in python by default, for this you could use sympy

from sympy import *

x= symbols('x')
r = limit((1+1/x)**x, x, oo)
print(r)

Output:

E
like image 162
eyllanesc Avatar answered Dec 28 '22 09:12

eyllanesc