def _print_Float(self, expr): """Print a Float in C-like scientific notation.""" prec = expr._prec if prec < 5: dps = 0 else: dps = prec_to_dps(expr._prec) if self._settings["full_prec"] is True: strip = False elif self._settings["full_prec"] is False: strip = True elif self._settings["full_prec"] == "auto": strip = self._print_level > 1 rv = to_str(expr._mpf_, dps, strip_zeros=strip, max_fixed=-2, min_fixed=2) if rv.startswith('-.0'): rv = '-0.' + rv[3:] elif rv.startswith('.0'): rv = '0.' + rv[2:] if self.dtype == np.float32: rv = rv + 'F' return rv
def _print_Float(self, expr): """ override method in StrPrinter always printing floating point numbers in scientific notation """ prec = expr._prec if prec < 5: dps = 0 else: dps = prec_to_dps(expr._prec) if self._settings["full_prec"] is True: strip = False elif self._settings["full_prec"] is False: strip = True elif self._settings["full_prec"] == "auto": strip = self._print_level > 1 rv = to_str(expr._mpf_, dps, strip_zeros=strip, max_fixed=-2, min_fixed=2) if rv.startswith('-.0'): rv = '-0.' + rv[3:] elif rv.startswith('.0'): rv = '0.' + rv[2:] return rv
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"] is True: strip = False elif self._settings["full_prec"] is False: strip = True elif self._settings["full_prec"] == "auto": strip = self._print_level > 1 low = self._settings["min"] if "min" in self._settings else None high = self._settings["max"] if "max" in self._settings else None rv = mlib_to_str(expr._mpf_, dps, strip_zeros=strip, min_fixed=low, max_fixed=high) if rv.startswith('-.0'): rv = '-0.' + rv[3:] elif rv.startswith('.0'): rv = '0.' + rv[2:] if rv.startswith('+'): # e.g., +inf -> inf rv = rv[1:] return rv
def _print_Float(self, expr): """Always printing floating point numbers in scientific notation :param expr: A floating point number :returns: The resulting code as a string """ prec = expr._prec if prec < 5: dps = 0 else: dps = prec_to_dps(expr._prec) if self._settings["full_prec"] is True: strip = False elif self._settings["full_prec"] is False: strip = True elif self._settings["full_prec"] == "auto": strip = self._print_level > 1 rv = to_str(expr._mpf_, dps, strip_zeros=strip, max_fixed=-2, min_fixed=2) if rv.startswith('-.0'): rv = '-0.' + rv[3:] elif rv.startswith('.0'): rv = '0.' + rv[2:] return rv + 'F'
def modified_round(w, p=0): """ sympy expr.round() function modified so that gives extra digits in it representation when p <= 0 """ try: from mpmath.libmp import prec_to_dps except ImportError: from sympy.mpmath.libmp import prec_to_dps from sympy.core.expr import _mag from sympy import Pow, Rational, Integer x = w if not x.is_number: raise TypeError('%s is not a number' % type(x)) if x in (S.NaN, S.Infinity, S.NegativeInfinity, S.ComplexInfinity): return x if not x.is_real: i, r = x.as_real_imag() return i.round(p) + S.ImaginaryUnit*r.round(p) if not x: return x p = int(p) precs = [f._prec for f in x.atoms(Float)] dps = prec_to_dps(max(precs)) if precs else None mag_first_dig = _mag(x) allow = digits_needed = mag_first_dig + p if p <= 0: allow += 1 digits_needed += 1 if dps is not None and allow > dps: allow = dps mag = Pow(10, p) # magnitude needed to bring digit p to units place xwas = x x += 1/(2*mag) # add the half for rounding i10 = 10*mag*x.n((dps if dps is not None else digits_needed) + 1) if i10.is_negative: x = xwas - 1/(2*mag) # should have gone the other way i10 = 10*mag*x.n((dps if dps is not None else digits_needed) + 1) rv = -(Integer(-i10)//10) else: 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 Float(str(rv), digits_needed) else: if not allow and rv > w: allow += 1 return Float(rv, allow)
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"] is True: strip = False elif self._settings["full_prec"] is 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
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'] is True: strip = False elif self._settings['full_prec'] is False: strip = True elif self._settings['full_prec'] == 'auto': strip = self._print_level > 1 else: raise NotImplementedError 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:] elif rv.startswith('+'): rv = rv[1:] return rv
def _print_Float(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)
def _print_Float(self, expr): dps = prec_to_dps(expr._prec) r = mlib.to_str(expr._mpf_, repr_dps(expr._prec)) return f"{expr.__class__.__name__}('{r}', dps={dps:d})"