def format_float(self, w_value, char): space = self.space x = space.float_w(maybe_float(space, w_value)) if isnan(x): if char in 'EFG': r = 'NAN' else: r = 'nan' elif isinf(x): if x < 0: if char in 'EFG': r = '-INF' else: r = '-inf' else: if char in 'EFG': r = 'INF' else: r = 'inf' else: prec = self.prec if prec < 0: prec = 6 if char in 'fF' and x/1e25 > 1e25: char = chr(ord(char) + 1) # 'f' => 'g' flags = 0 if self.f_alt: flags |= DTSF_ALT r = formatd(x, char, prec, flags) self.std_wp_number(r)
def format_float(self, w_value, char): space = self.space x = space.float_w(maybe_float(space, w_value)) if isnan(x): if char in 'EFG': r = 'NAN' else: r = 'nan' elif isinf(x): if x < 0: if char in 'EFG': r = '-INF' else: r = '-inf' else: if char in 'EFG': r = 'INF' else: r = 'inf' else: prec = self.prec if prec < 0: prec = 6 if char in 'fF' and x / 1e25 > 1e25: char = chr(ord(char) + 1) # 'f' => 'g' flags = 0 if self.f_alt: flags |= DTSF_ALT r = formatd(x, char, prec, flags) self.std_wp_number(r)
def format_float(x, code, precision): # like float2string, except that the ".0" is not necessary if isinf(x): if x > 0.0: return "inf" else: return "-inf" elif isnan(x): return "nan" else: return formatd(x, code, precision)
def float2string(x, code, precision): # we special-case explicitly inf and nan here if isfinite(x): s = formatd(x, code, precision, DTSF_ADD_DOT_0) elif isinf(x): if x > 0.0: s = "inf" else: s = "-inf" else: # isnan(x): s = "nan" return s
def _format_complex(self, w_complex): space = self.space tp = self._type self._get_locale(tp) default_precision = 6 if self._align == "=": # '=' alignment is invalid msg = ("'=' alignment flag is not allowed in" " complex format specifier") raise OperationError(space.w_ValueError, space.wrap(msg)) if self._fill_char == "0": #zero padding is invalid msg = "Zero padding is not allowed in complex format specifier" raise OperationError(space.w_ValueError, space.wrap(msg)) if self._alternate: #alternate is invalid msg = "Alternate form %s not allowed in complex format specifier" raise OperationError(space.w_ValueError, space.wrap(msg % (self._alternate))) skip_re = 0 add_parens = 0 if tp == "\0": #should mirror str() output tp = "g" default_precision = 12 #test if real part is non-zero if (w_complex.realval == 0 and copysign(1., w_complex.realval) == 1.): skip_re = 1 else: add_parens = 1 if tp == "n": #same as 'g' except for locale, taken care of later tp = "g" #check if precision not set if self._precision == -1: self._precision = default_precision #might want to switch to double_to_string from formatd #in CPython it's named 're' - clashes with re module re_num = formatd(w_complex.realval, tp, self._precision) im_num = formatd(w_complex.imagval, tp, self._precision) n_re_digits = len(re_num) n_im_digits = len(im_num) to_real_number = 0 to_imag_number = 0 re_sign = im_sign = '' #if a sign character is in the output, remember it and skip if re_num[0] == "-": re_sign = "-" to_real_number = 1 n_re_digits -= 1 if im_num[0] == "-": im_sign = "-" to_imag_number = 1 n_im_digits -= 1 #turn off padding - do it after number composition #calc_num_width uses self._width, so assign to temporary variable, #calculate width of real and imag parts, then reassign padding, align tmp_fill_char = self._fill_char tmp_align = self._align tmp_width = self._width self._fill_char = "\0" self._align = "<" self._width = -1 #determine if we have remainder, might include dec or exponent or both re_have_dec, re_remainder_ptr = self._parse_number(re_num, to_real_number) im_have_dec, im_remainder_ptr = self._parse_number(im_num, to_imag_number) if self.is_unicode: re_num = re_num.decode("ascii") im_num = im_num.decode("ascii") #set remainder, in CPython _parse_number sets this #using n_re_digits causes tests to fail re_n_remainder = len(re_num) - re_remainder_ptr im_n_remainder = len(im_num) - im_remainder_ptr re_spec = self._calc_num_width(0, re_sign, to_real_number, n_re_digits, re_n_remainder, re_have_dec, re_num) #capture grouped digits b/c _fill_number reads from self._grouped_digits #self._grouped_digits will get overwritten in imaginary calc_num_width re_grouped_digits = self._grouped_digits if not skip_re: self._sign = "+" im_spec = self._calc_num_width(0, im_sign, to_imag_number, n_im_digits, im_n_remainder, im_have_dec, im_num) im_grouped_digits = self._grouped_digits if skip_re: re_spec.n_total = 0 #reassign width, alignment, fill character self._align = tmp_align self._width = tmp_width self._fill_char = tmp_fill_char #compute L and R padding - stored in self._left_pad and self._right_pad self._calc_padding(self.empty, re_spec.n_total + im_spec.n_total + 1 + add_parens * 2) out = self._builder() fill = self._fill_char if fill == "\0": fill = self._lit(" ")[0] #compose the string #add left padding out.append_multiple_char(fill, self._left_pad) if add_parens: out.append(self._lit('(')[0]) #if the no. has a real component, add it if not skip_re: out.append(self._fill_number(re_spec, re_num, to_real_number, 0, fill, re_remainder_ptr, False, re_grouped_digits)) #add imaginary component out.append(self._fill_number(im_spec, im_num, to_imag_number, 0, fill, im_remainder_ptr, False, im_grouped_digits)) #add 'j' character out.append(self._lit('j')[0]) if add_parens: out.append(self._lit(')')[0]) #add right padding out.append_multiple_char(fill, self._right_pad) return self.space.wrap(out.build())
def fn(x): return formatd(x, 'f', 2, 0)
def dump_float(buf, x): buf.append(TYPE_FLOAT) s = formatd(x, 'g', 17) buf.append(chr(len(s))) buf += s
def f(): result = '' for num, string, fval in unmarshaller(buf): result += '%d=%s/%s;' % (num, string, formatd(fval, 'g', 17)) return result
def dump_float(buf, x): buf.append(TYPE_FLOAT) s = formatd(x, "g", 17) buf.append(chr(len(s))) buf += s
def f(y): return rfloat.formatd(y, 'g', 2, flags)
def oofakeimpl(x, code, precision, flags): return ootype.oostring(rfloat.formatd(x, code, precision, flags), -1)
def ll_float_str(repr, f): from pypy.rlib.rfloat import formatd return llstr(formatd(f, 'f', 6))
def fn(x): return formatd(x, "f", 2, 0)