(x, M, L=8)
return M+y
Given integers x and M, M > 0, such that x/M is small in absolute
    value, compute an integer approximation to M*exp(x/M).  For 0 <=
    x/M <= 2.4, the absolute error in the result is bounded by 60 (and
    is usually much smaller).
Given integers x and M, M > 0, such that x/M is small in absolute
    value, compute an integer approximation to M*exp(x/M).  For 0 <=
    x/M <= 2.4, the absolute error in the result is bounded by 60 (and
    is usually much smaller).
Given integers x and M M > 0 such that x / M is small in absolute value compute an integer approximation to M * exp ( x / M ) . For 0 < = x / M < = 2 . 4 the absolute error in the result is bounded by 60 ( and is usually much smaller ) .
def _iexp(x, M, L=8):
    """Given integers x and M, M > 0, such that x/M is small in absolute
    value, compute an integer approximation to M*exp(x/M).  For 0 <=
    x/M <= 2.4, the absolute error in the result is bounded by 60 (and
    is usually much smaller)."""

    # Algorithm: to compute exp(z) for a real number z, first divide z
    # by a suitable power R of 2 so that |z/2**R| < 2**-L.  Then
    # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor
    # series
    #
    #     expm1(x) = x + x**2/2! + x**3/3! + ...
    #
    # Now use the identity
    #
    #     expm1(2x) = expm1(x)*(expm1(x)+2)
    #
    # R times to compute the sequence expm1(z/2**R),
    # expm1(z/2**(R-1)), ... , exp(z/2), exp(z).

    # Find R such that x/2**R/M <= 2**-L
    R = _nbits((x<<L)//M)

    # Taylor series.  (2**L)**T > M
    T = -int(-10*len(str(M))//(3*L))
    y = _div_nearest(x, T)
    Mshift = M<<R
    for i in range(T-1, 0, -1):
        y = _div_nearest(x*(Mshift + y), Mshift * i)

    # Expansion
    for k in range(R-1, -1, -1):
        Mshift = M<<(k+2)
        y = _div_nearest(y*(y+Mshift), Mshift)

    return M+y
def _iexp ( x , M , L = 8 ) : # Algorithm: to compute exp(z) for a real number z, first divide z # by a suitable power R of 2 so that |z/2**R| < 2**-L.  Then # compute expm1(z/2**R) = exp(z/2**R) - 1 using the usual Taylor # series # #     expm1(x) = x + x**2/2! + x**3/3! + ... # # Now use the identity # #     expm1(2x) = expm1(x)*(expm1(x)+2) # # R times to compute the sequence expm1(z/2**R), # expm1(z/2**(R-1)), ... , exp(z/2), exp(z). # Find R such that x/2**R/M <= 2**-L R = _nbits ( ( x << L ) // M ) # Taylor series.  (2**L)**T > M T = - int ( - 10 * len ( str ( M ) ) // ( 3 * L ) ) y = _div_nearest ( x , T ) Mshift = M << R for i in range ( T - 1 , 0 , - 1 ) : y = _div_nearest ( x * ( Mshift + y ) , Mshift * i ) # Expansion for k in range ( R - 1 , - 1 , - 1 ) : Mshift = M << ( k + 2 ) y = _div_nearest ( y * ( y + Mshift ) , Mshift ) return M + y