Repository: sympy/sympy
File: sympy/core/evalf.py (Python)

Code before:
        xre = fzero

    if xim:
        from sympy.functions.elementary.complexes import Abs
        from sympy.functions.elementary.exponential import log

        # XXX: use get_abs etc instead
        re = evalf_log(
            log(Abs(arg, evaluate=False), evaluate=False), prec, options)
        im = mpf_atan2(xim, xre or fzero, prec)
        return re[0], im, re[2], prec

    imaginary_term = (mpf_cmp(xre, fzero) < 0)

    re = mpf_log(mpf_abs(xre), prec, rnd)
    size = fastlog(re)
    if prec - size > workprec:
        from .add import Add
        # We actually need to compute 1+x accurately, not x
        add = Add(S.NegativeOne, arg, evaluate=False)
        xre, xim, xre_acc, _ = evalf_add(add, prec, options)
        if xre != fzero and (xre_acc is None or xre_acc > 1):
            prec2 = workprec - fastlog(xre)
            # xre is now x - 1 so we add 1 back here to calculate x
            re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd)

    re_acc = prec

    if imaginary_term:
        return re, mpf_pi(prec), re_acc, prec
    else:
        return re, None, re_acc, None


def evalf_atan(v: 'atan', prec: int, options: OPT_DICT) -> TMP_RES:
    arg = v.args[0]
    result = evalf(arg, prec + 5, options)
    if result is S.ComplexInfinity:
        return result
    xre, xim, reacc, imacc = result
    if xre is xim is None:
        return (None,)*4
    if xim:
        raise NotImplementedError
    return mpf_atan(xre, prec, rnd), None, prec, None


def evalf_subs(prec: int, subs: dict) -> dict:
    """ Change all Float entries in `subs` to have precision prec. """
    newsubs = {}
    for a, b in subs.items():

Review comment (suggestion):
The accuracy check condition `xre_acc is None or xre_acc > 1` may behave unexpectedly when xre_acc is None. If xre_acc is None (indicating unknown accuracy), the code proceeds with the calculation, which could be risky. Consider whether it's safer to require a known minimum accuracy before proceeding with the high-precision recalculation. If None indicates "sufficiently accurate" or "unknown but assumed good", this should be documented in a comment.
```suggestion
        # Only refine when we have a known accuracy estimate for xre.
        if xre != fzero and (xre_acc is not None and xre_acc > 1):
            prec2 = workprec - fastlog(xre)
            # xre is now x - 1 so we add 1 back here to calculate x
            re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd)
```

Code after:
        xre = fzero

    if xim:
        from sympy.functions.elementary.complexes import Abs
        from sympy.functions.elementary.exponential import log

        # XXX: use get_abs etc instead
        re = evalf_log(
            log(Abs(arg, evaluate=False), evaluate=False), prec, options)
        im = mpf_atan2(xim, xre or fzero, prec)
        return re[0], im, re[2], prec

    imaginary_term = (mpf_cmp(xre, fzero) < 0)

    re = mpf_log(mpf_abs(xre), prec, rnd)
    size = fastlog(re)
    if prec - size > workprec:
        from .add import Add
        # We actually need to compute x-1 accurately, not x
        add = Add(S.NegativeOne, arg, evaluate=False)
        xre, xim, xre_acc, _ = evalf_add(add, prec, options)
        if xre != fzero and (xre_acc is None or xre_acc > 1):
            prec2 = workprec - fastlog(xre)
            # xre is now x - 1 so we add 1 back here to calculate x
            re = mpf_log(mpf_abs(mpf_add(xre, fone, prec2)), prec, rnd)

    re_acc = prec

    if imaginary_term:
        return re, mpf_pi(prec), re_acc, prec
    else:
        return re, None, re_acc, None


def evalf_atan(v: 'atan', prec: int, options: OPT_DICT) -> TMP_RES:
    arg = v.args[0]
    result = evalf(arg, prec + 5, options)
    if result is S.ComplexInfinity:
        return result
    xre, xim, reacc, imacc = result
    if xre is xim is None:
        return (None,)*4
    if xim:
        raise NotImplementedError
    return mpf_atan(xre, prec, rnd), None, prec, None


def evalf_subs(prec: int, subs: dict) -> dict:
    """ Change all Float entries in `subs` to have precision prec. """
    newsubs = {}
    for a, b in subs.items():