Exemple #1
0
 def _print_Real(self, expr):
     prec = expr._prec
     if prec < 5:
         dps = 0
     else:
         dps = prec_to_dps(expr._prec)
     if self._settings["full_prec"] == True:
         strip = False
     elif self._settings["full_prec"] == False:
         strip = True
     elif self._settings["full_prec"] == "auto":
         strip = self._print_level > 1
     return mlib.to_str(expr._mpf_, dps, strip_zeros=strip)
Exemple #2
0
 def _print_Real(self, expr):
     prec = expr._prec
     if prec < 5:
         dps = 0
     else:
         dps = prec_to_dps(expr._prec)
     if self._settings["full_prec"] == True:
         strip = False
     elif self._settings["full_prec"] == False:
         strip = True
     elif self._settings["full_prec"] == "auto":
         strip = self._print_level > 1
     return mlib.to_str(expr._mpf_, dps, strip_zeros=strip)
Exemple #3
0
 def _print_Float(self, expr):
     prec = expr._prec
     if prec < 5:
         dps = 0
     else:
         dps = prec_to_dps(expr._prec)
     if self._settings["full_prec"] == True:
         strip = False
     elif self._settings["full_prec"] == False:
         strip = True
     elif self._settings["full_prec"] == "auto":
         strip = self._print_level > 1
     rv = mlib.to_str(expr._mpf_, dps, strip_zeros=strip)
     if rv.startswith('-.0'):
         rv = '-0.' + rv[3:]
     elif rv.startswith('.0'):
         rv = '0.' + rv[2:]
     return rv
Exemple #4
0
 def _print_Float(self, expr):
     prec = expr._prec
     if prec < 5:
         dps = 0
     else:
         dps = prec_to_dps(expr._prec)
     if self._settings["full_prec"] == True:
         strip = False
     elif self._settings["full_prec"] == False:
         strip = True
     elif self._settings["full_prec"] == "auto":
         strip = self._print_level > 1
     rv = mlib.to_str(expr._mpf_, dps, strip_zeros=strip)
     if rv.startswith('-.0'):
         rv = '-0.' + rv[3:]
     elif rv.startswith('.0'):
         rv = '0.' + rv[2:]
     return rv
Exemple #5
0
    def _print_Float(self, expr):
        # Based off of that in StrPrinter
        dps = prec_to_dps(expr._prec)
        str_real = mlib.to_str(expr._mpf_, dps, strip_zeros=True)

        # Must always have a mul symbol (as 2.5 10^{20} just looks odd)
        separator = r" \times "

        if self._settings['mul_symbol'] is not None:
            separator = self._settings['mul_symbol_latex']

        if 'e' in str_real:
            (mant, exp) = str_real.split('e')

            if exp[0] == '+':
                exp = exp[1:]

            return r"%s%s10^{%s}" % (mant, separator, exp)
        elif str_real == "+inf":
            return r"\infty"
        elif str_real == "-inf":
            return r"- \infty"
        else:
            return str_real
Exemple #6
0
    def _print_Float(self, expr):
        # Based off of that in StrPrinter
        dps = prec_to_dps(expr._prec)
        str_real = mlib.to_str(expr._mpf_, dps, strip_zeros=True)

        # Must always have a mul symbol (as 2.5 10^{20} just looks odd)
        separator = r" \times "

        if self._settings['mul_symbol'] is not None:
            separator = self._settings['mul_symbol_latex']

        if 'e' in str_real:
            (mant, exp) = str_real.split('e')

            if exp[0] == '+':
                exp = exp[1:]

            return r"%s%s10^{%s}" % (mant, separator, exp)
        elif str_real == "+inf":
            return r"\infty"
        elif str_real == "-inf":
            return r"- \infty"
        else:
            return str_real
Exemple #7
0
 def _print_Real(self, expr):
     dps = prec_to_dps(expr._prec)
     r = mlib.to_str(expr._mpf_, repr_dps(expr._prec))
     return "%s('%s', prec=%i)" % (expr.__class__.__name__, r, dps)
Exemple #8
0
 def _print_Real(self, expr):
     dps = prec_to_dps(expr._prec)
     r = mlib.to_str(expr._mpf_, repr_dps(expr._prec))
     return "%s('%s', prec=%i)" % (expr.__class__.__name__, r, dps)
Exemple #9
0
def round(x, p=0):
    """Return x rounded to the given decimal place. If x is not an Expr,
    Python's round function is employed.

    Examples
    ========
    >>> from sympy import round, pi, S, Number
    >>> round(S(10.5))
    11.
    >>> round(pi)
    3.
    >>> round(pi, 2)
    3.14

    If x is not a SymPy Expr then Python's round is used and it returns
    a Python type, not a SymPy Number:

    >>> isinstance(round(543210, -2), Number)
    False
    >>> round(S(543210), -2)
    5.432e+5
    >>> _.is_Number
    True

    """
    from sympy.functions.elementary.exponential import log
    from sympy.mpmath.libmp import prec_to_dps

    if not isinstance(x, Expr):
        return _pyround(x, p)
    if not x.is_number:
        raise TypeError('%s is not a number' % x)
    if not x.is_real:
        raise TypeError("can't convert complex to int")
    if not x:
        return x
    p = int(p)

    precs = [f._prec for f in x.atoms(C.Float)]
    dps = prec_to_dps(max(precs)) if precs else None

    xpos = abs(x.n())
    try:
        mag_first_dig = int(ceil(log10(xpos)))
    except (ValueError, OverflowError):
        mag_first_dig = int(ceil(C.Float(mpf_log(xpos._mpf_, 53))/log(10)))
    # check that we aren't off by 1
    if (xpos/10**mag_first_dig) >= 1:
        mag_first_dig += 1
        assert .1 <= (xpos/10**mag_first_dig) < 1
    allow = digits_needed = mag_first_dig + p
    if dps is not None and allow > dps:
        allow = dps
    mag = Pow(10, p) # magnitude needed to bring digit p to units place
    x += 1/(2*mag) # add the half for rounding
    i10 = 10*mag*x.n((dps if dps is not None else digits_needed) + 1)
    rv = Integer(i10)//10
    q = 1
    if p > 0:
        q = mag
    elif p < 0:
        rv /= mag
    rv = Rational(rv, q)
    if rv.is_Integer:
        # use str or else it won't be a float
        return C.Float(str(rv), digits_needed)
    else:
        return C.Float(rv, allow)