Exemple #1
0
 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)
Exemple #2
0
 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)
Exemple #3
0
def number_format(interpreter, space, args):
    number = args[0].get_value()
    assert(isfloat(number))
    positions = args[1].get_value()
    assert(isint(positions))

    number = number.to_number()
    positions = positions.get_int()

    return space.wrap(formatd(number, "f", positions))
Exemple #4
0
def gettimeofday(interpreter, space, args):
    seconds = time.time()
    seconds = str(formatd(seconds, "f", 6))
    sec = int(seconds.split('.')[0])
    usec = int(seconds.split('.')[1])

    return space.newdictarray({
        u'sec': space.wrap(sec),
        u'usec': space.wrap(usec)
    })
Exemple #5
0
 def method_to_s(self, space):
     if math.isinf(self.floatvalue):
         if self.floatvalue >= 0:
             return space.newstr_fromstr("Infinity")
         else:
             return space.newstr_fromstr("-Infinity")
     elif math.isnan(self.floatvalue):
         return space.newstr_fromstr("NaN")
     else:
         return space.newstr_fromstr(formatd(self.floatvalue, "g", DTSF_STR_PRECISION, DTSF_ADD_DOT_0))
Exemple #6
0
 def method_to_s(self, space):
     if math.isinf(self.floatvalue):
         if self.floatvalue >= 0:
             return space.newstr_fromstr("Infinity")
         else:
             return space.newstr_fromstr("-Infinity")
     elif math.isnan(self.floatvalue):
         return space.newstr_fromstr("NaN")
     else:
         return space.newstr_fromstr(formatd(self.floatvalue, "g", DTSF_STR_PRECISION, DTSF_ADD_DOT_0))
Exemple #7
0
def gettimeofday(space, args):
    seconds = time.time()
    seconds = str(formatd(seconds, "f", 6))
    sec = int(seconds.split('.')[0])
    usec = int(seconds.split('.')[1])

    array = W_Array()
    array.put(newstring('sec'), newint(intmask(sec)))
    array.put(newstring('usec'), newint(intmask(usec)))
    return array
Exemple #8
0
def format_float(x, code, precision):
    # like float2string, except that the ".0" is not necessary
    if math.isinf(x):
        if x > 0.0:
            return "inf"
        else:
            return "-inf"
    elif math.isnan(x):
        return "nan"
    else:
        return formatd(x, code, precision)
Exemple #9
0
def float_to_string(x, code='g', precision=DTSF_STR_PRECISION):
    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.decode('utf-8')
Exemple #10
0
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)
Exemple #11
0
def number_format(args):
    number = args[0]
    positions = args[1]
    assert(isint(positions))

    number = number.to_number()
    positions = positions.get_int()

    formatted = str(formatd(number, "f", positions))

    return W_StringObject(formatted)
Exemple #12
0
def float_to_string(x, code='g', precision=DTSF_STR_PRECISION):
    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.decode('utf-8')
Exemple #13
0
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
Exemple #14
0
def number_format(space, args):
    number = args[0].get_value()
    assert(isfloat(number))
    positions = args[1].get_value()
    assert(isint(positions))

    number = number.to_number()
    positions = positions.get_int()

    formatted = str(formatd(number, "f", positions))

    return newstring(formatted)
Exemple #15
0
    def to_string(self):
        # XXX incomplete, this doesn't follow the 9.8.1 recommendation
        if isnan(self._floatval_):
            return u'NaN'
        if isinf(self._floatval_):
            if self._floatval_ > 0:
                return u'Infinity'
            else:
                return u'-Infinity'

        if self._floatval_ == 0:
            return u'0'

        res = u''
        try:
            res = unicode(formatd(self._floatval_, 'g', 10))
        except OverflowError:
            raise

        if len(res) > 3 and (res[-3] == '+' or res[-3] == '-') and res[-2] == '0':
            cut = len(res) - 2
            assert cut >= 0
            res = res[:cut] + res[-1]
        return res
Exemple #16
0
 def f(x):
     return formatd(x, 'f', 1234, 0)
Exemple #17
0
 def f(x):
     return formatd(x, 'f', 1234, 0)
Exemple #18
0
 def f(y):
     return rfloat.formatd(y, 'g', 2, flags)
Exemple #19
0
def llrepr_out(v):
    if isinstance(v, float):
        from rpython.rlib.rfloat import formatd, DTSF_ADD_DOT_0
        return formatd(v, 'r', 0, DTSF_ADD_DOT_0)
    return str(v)   # always return a string, to get consistent types
Exemple #20
0
 def func(x):
     # Test the %F format, which is not supported by
     # the Microsoft's msvcrt library.
     return formatd(x, 'F', 4)
Exemple #21
0
 def oofakeimpl(x, code, precision, flags):
     return ootype.oostring(rfloat.formatd(x, code, precision, flags), -1)
Exemple #22
0
 def fn(x):
     return formatd(x, 'f', 2, 0)
Exemple #23
0
 def prim_as_string(self, universe):
     s = formatd(self._embedded_double, "g", DTSF_STR_PRECISION, DTSF_ADD_DOT_0)
     return universe.new_string(s)
Exemple #24
0
 def f():
     result = ''
     for num, string, fval in unmarshaller(buf):
         result += '%d=%s/%s;' % (num, string, formatd(fval, 'g', 17))
     return result
Exemple #25
0
 def tostring(self):
     from rpython.rlib.rfloat import formatd, DTSF_STR_PRECISION, DTSF_ADD_DOT_0
     return formatd(self.value, 'g', DTSF_STR_PRECISION, DTSF_ADD_DOT_0)
Exemple #26
0
 def f(y):
     return rfloat.formatd(y, 'g', 2, flags)
Exemple #27
0
 def ll_str(self, f):
     from rpython.rlib.rfloat import formatd
     return llstr(formatd(f, 'f', 6))
Exemple #28
0
 def fmt_f(self, w_item, width):
     num = w_item.to_number()
     return self._fmt_num(unicode(formatd(num, "f", width)), width)
Exemple #29
0
 def fmt_f(self, space, width):
     num = Coerce.float(space, self._next_item())
     return self._fmt_num(space, formatd(num, "f", 6), width)
Exemple #30
0
 def func(x):
     # Test the %F format, which is not supported by
     # the Microsoft's msvcrt library.
     return formatd(x, 'F', 4)
Exemple #31
0
def llrepr_out(v):
    if isinstance(v, float):
        from rpython.rlib.rfloat import formatd, DTSF_ADD_DOT_0
        return formatd(v, 'r', 0, DTSF_ADD_DOT_0)
    return str(v)  # always return a string, to get consistent types
Exemple #32
0
 def prim_as_string(self, universe):
     s = formatd(self._embedded_double, "g", DTSF_STR_PRECISION,
                 DTSF_ADD_DOT_0)
     return universe.new_string(s)
Exemple #33
0
def _asString(ivkbl, frame, interpreter):
    rcvr = frame.pop()
    d = rcvr.get_embedded_double()
    s = formatd(d, "g", DTSF_STR_PRECISION, DTSF_ADD_DOT_0)
    frame.push(interpreter.get_universe().new_string(s))
Exemple #34
0
 def fmt_f(self, space, w_item, width):
     num = space.float_w(w_item)
     return self._fmt_num(space, formatd(num, "f", 6), width)
Exemple #35
0
 def fn(x):
     return formatd(x, "f", 2, 0)
Exemple #36
0
 def fn(x):
     return formatd(x, 'f', 2, 0)
Exemple #37
0
 def f():
     result = ''
     for num, string, fval in unmarshaller(buf):
         result += '%d=%s/%s;' % (num, string, formatd(fval, 'g', 17))
     return result
Exemple #38
0
        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
                raise oefmt(
                    space.w_ValueError,
                    "'=' alignment flag is not allowed in complex "
                    "format specifier")
            if self._fill_char == "0":
                # zero padding is invalid
                raise oefmt(
                    space.w_ValueError,
                    "Zero padding is not allowed in complex format "
                    "specifier")
            if self._alternate:
                # alternate is invalid
                raise oefmt(
                    space.w_ValueError,
                    "Alternate form (#) not allowed in complex format "
                    "specifier")
            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 math.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 = rutf8.decode_latin_1(re_num)
                im_num = rutf8.decode_latin_1(im_num)

            #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(
                "", re_spec.n_total + im_spec.n_total + 1 + add_parens * 2)

            out = self._builder()
            fill = self._fill_char

            #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.wrap(out.build())
Exemple #39
0
        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

            #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())
Exemple #40
0
def ll_float_str(repr, f):
    from rpython.rlib.rfloat import formatd
    return llstr(formatd(f, 'f', 6))
Exemple #41
0
def dump_float(buf, x):
    buf.append(TYPE_FLOAT)
    s = formatd(x, 'g', 17)
    buf.append(chr(len(s)))
    buf += s
Exemple #42
0
 def tostring(self):
     from rpython.rlib.rfloat import formatd, DTSF_STR_PRECISION, DTSF_ADD_DOT_0
     return formatd(self.value, 'g', DTSF_STR_PRECISION, DTSF_ADD_DOT_0)
Exemple #43
0
 def ll_str(self, f):
     return llstr(formatd(f, 'f', 6))
Exemple #44
0
 def fmt_f(self, space, width, precision, format_char):
     num = Coerce.float(space, self._next_item())
     return self._fmt_num(space, formatd(num, "f", precision), width)
Exemple #45
0
 def f(x):
     return formatd(x, 'r', 0, 0)
Exemple #46
0
 def fmt_f(self, space, w_item, width):
     num = space.float_w(w_item)
     return self._fmt_num(space, formatd(num, "f", 6), width)
Exemple #47
0
 def f(x):
     return formatd(x, 'r', 0, 0)
Exemple #48
0
def dump_float(buf, x):
    buf.append(TYPE_FLOAT)
    s = formatd(x, 'g', 17)
    buf.append(chr(len(s)))
    buf += s
Exemple #49
0
 def fmt_f(self, w_item, width):
     num = w_item.to_number()
     return self._fmt_num(formatd(num, "f", width), width)