import math


def Vcell_Calc(E0, i, i_0, i_n, i_L, R_M, A, B):
    """
    Calculate cell voltage.

    :param E0:  fuel Cell reversible no loss voltage [V]
    :type E0 : float
    :param i : cell operating current [A]
    :type i : float
    :param i_0: exchange current at which the overvoltage begins to move from zero [A]
    :type i_0 : float
    :param i_n : internal current [A]
    :type i_n : float
    :param i_L : limiting current [A]
    :type i_L : float
    :param R_M : the membrane and contact resistances [ohm]
    :type R_M : float
    :param A : the slope of the Tafel line [V]
    :type A : float
    :param B : constant in the mass transfer term [V}
    :type B : float
    :return:  cell voltage [V]
    """
    try:
        result = E0 - A * (math.log((i + i_n) / i_0)) - R_M * \
            (i + i_n) + B * (math.log(1 - ((i + i_n) / i_L)))
        return result
    except (TypeError, ZeroDivisionError, OverflowError, ValueError):
        print(
            "[Error] Vcell Calculation Error (E0:%s, i:%s, i_0:%s, i_n:%s, i_L:%s, R_M:%s, A:%s, B:%s)" %
            (str(E0), str(i), str(i_0), str(i_n), str(i_L), str(R_M), str(A), str(B)))