

def _real_to_rational(expr, tolerance=None):
    '\n    Replace all reals in expr with rationals.\n\n    >>> from sympy import nsimplify\n    >>> from sympy.abc import x\n\n    >>> nsimplify(.76 + .1*x**.5, rational=True)\n    sqrt(x)/10 + 19/25\n\n    '
    inf = Float('inf')
    p = expr
    reps = {
        
    }
    reduce_num = None
    if ((tolerance is not None) and (tolerance < 1)):
        reduce_num = ceiling((1 / tolerance))
    for float in p.atoms(Float):
        key = float
        if (reduce_num is not None):
            r = Rational(float).limit_denominator(reduce_num)
        elif ((tolerance is not None) and (tolerance >= 1) and (float.is_Integer is False)):
            r = Rational((tolerance * round((float / tolerance)))).limit_denominator(int(tolerance))
        else:
            r = nsimplify(float, rational=False)
            if (float and (not r)):
                r = Rational(float)
            elif (not r.is_Rational):
                if ((float == inf) or (r == (- inf))):
                    r = S.ComplexInfinity
                elif (float < 0):
                    float = (- float)
                    d = Pow(10, int((mpmath.log(float) / mpmath.log(10))))
                    r = ((- Rational(str((float / d)))) * d)
                elif (float > 0):
                    d = Pow(10, int((mpmath.log(float) / mpmath.log(10))))
                    r = (Rational(str((float / d))) * d)
                else:
                    r = Integer(0)
        reps[key] = r
    return p.subs(reps, simultaneous=True)

dataset/ETHPy150Open sympy/sympy/sympy/simplify/simplify.py _real_to_rational/VarMisuse@214/343 `float`->`r`