def display_float(x, digits, method="truncate", extra_truncation_digits=3, try_halfinteger=True, no_sci=None, latex=False): if abs(x) < 10.**(-digits - extra_truncation_digits): return "0" # if small, try to display it as an exact or half integer if try_halfinteger and abs(x) < 10.**digits: if is_exact(x): s = str(x) if len(s) < digits + 2: # 2 = '/' + '-' return str(x) k = round_to_half_int(x) if k == x: k2 = None try: k2 = ZZ(2 * x) except TypeError: pass # the second statement checks for overflow if k2 == 2 * x and (2 * x + 1) - k2 == 1: if k2 % 2 == 0: s = '%s' % (k2 / 2) else: s = '%s' % (float(k2) / 2) return s if method == 'truncate': rnd = 'RNDZ' else: rnd = 'RNDN' if no_sci is None: no_sci = 'e' not in "%.{}g".format(digits) % float(x) try: s = RealField(max(53, 4 * digits), rnd=rnd)(x).str(digits=digits, no_sci=no_sci) except TypeError: # older versions of Sage don't support the digits keyword s = RealField(max(53, 4 * digits), rnd=rnd)(x).str(no_sci=no_sci) point = s.find('.') if point != -1: if point < digits: s = s[:digits + 1] else: s = s[:point] if latex and "e" in s: s = s.replace("e", r"\times 10^{") + "}" return s
def display_float(x, digits, method = "truncate", extra_truncation_digits=3, try_halfinteger=True): if abs(x) < 10.**(- digits - extra_truncation_digits): return "0" # if small, try to display it as an exact or half integer if try_halfinteger and abs(x) < 10.**digits: if is_exact(x): s = str(x) if len(s) < digits + 2: # 2 = '/' + '-' return str(x) k = round_to_half_int(x) if k == x : k2 = None try: k2 = ZZ(2*x) except TypeError: pass # the second statment checks for overflow if k2 == 2*x and (2*x + 1) - k2 == 1: if k2 % 2 == 0: s = '%s' % (k2/2) else: s = '%s' % (float(k2)/2) return s if method == 'truncate': rnd = 'RNDZ' else: rnd = 'RNDN' no_sci = 'e' not in "%.{}g".format(digits) % float(x) try: s = RealField(max(53,4*digits), rnd=rnd)(x).str(digits=digits, no_sci=no_sci) except TypeError: # older versions of Sage don't support the digits keyword s = RealField(max(53,4*digits), rnd=rnd)(x).str(no_sci=no_sci) point = s.find('.') if point != -1: if point < digits: s = s[:digits+1] else: s = s[:point] return s
def display_float(x, digits, method="truncate", extra_truncation_digits=3): if is_exact(x): return '%s' % x if abs(x) < 10.**(-digits - extra_truncation_digits): return "0" k = round_to_half_int(x) if k == x: k2 = None try: k2 = ZZ(2 * x) except TypeError: pass # the second statment checks for overflow if k2 == 2 * x and (2 * x + 1) - k2 == 1: if k2 % 2 == 0: s = '%s' % (k2 / 2) else: s = '%s' % (float(k2) / 2) return s if method == 'truncate': rnd = 'RNDZ' else: rnd = 'RNDN' no_sci = 'e' not in "%.{}g".format(digits) % float(x) try: s = RealField(max(53, 4 * digits), rnd=rnd)(x).str(digits=digits, no_sci=no_sci) except TypeError: # older versions of Sage don't support the digits keyword s = RealField(max(53, 4 * digits), rnd=rnd)(x).str(no_sci=no_sci) point = s.find('.') if point != -1: if point < digits: s = s[:digits + 1] else: s = s[:point] return s