Пример #1
0
 def __new__(cls, val=fzero, **kwargs):
     """A new mpf can be created from a Python float, an int, a
     or a decimal string representing a number in floating-point
     format."""
     prec, rounding = cls.context._prec_rounding
     if kwargs:
         prec = kwargs.get('prec', prec)
         if 'dps' in kwargs:
             prec = dps_to_prec(kwargs['dps'])
         rounding = kwargs.get('rounding', rounding)
     if type(val) is cls:
         sign, man, exp, bc = val._mpf_
         if (not man) and exp:
             return val
         v = new(cls)
         v._mpf_ = normalize(sign, man, exp, bc, prec, rounding)
         return v
     elif type(val) is tuple:
         if len(val) == 2:
             v = new(cls)
             v._mpf_ = from_man_exp(val[0], val[1], prec, rounding)
             return v
         if len(val) == 4:
             sign, man, exp, bc = val
             v = new(cls)
             v._mpf_ = normalize(sign, MPZ(man), exp, bc, prec, rounding)
             return v
         raise ValueError
     else:
         v = new(cls)
         v._mpf_ = mpf_pos(cls.mpf_convert_arg(val, prec, rounding), prec,
                           rounding)
         return v
Пример #2
0
 def __new__(cls, val=fzero, **kwargs):
     """A new mpf can be created from a Python float, an int, a
     or a decimal string representing a number in floating-point
     format."""
     prec, rounding = cls.context._prec_rounding
     if kwargs:
         prec = kwargs.get('prec', prec)
         if 'dps' in kwargs:
             prec = dps_to_prec(kwargs['dps'])
         rounding = kwargs.get('rounding', rounding)
     if type(val) is cls:
         sign, man, exp, bc = val._mpf_
         if (not man) and exp:
             return val
         v = new(cls)
         v._mpf_ = normalize(sign, man, exp, bc, prec, rounding)
         return v
     elif type(val) is tuple:
         if len(val) == 2:
             v = new(cls)
             v._mpf_ = from_man_exp(val[0], val[1], prec, rounding)
             return v
         if len(val) == 4:
             sign, man, exp, bc = val
             v = new(cls)
             v._mpf_ = normalize(sign, MPZ(man), exp, bc, prec, rounding)
             return v
         raise ValueError
     else:
         v = new(cls)
         v._mpf_ = mpf_pos(cls.mpf_convert_arg(val, prec, rounding), prec, rounding)
         return v
Пример #3
0
 def _parse_prec(ctx, kwargs):
     if kwargs:
         if kwargs.get('exact'):
             return 0, 'f'
         prec, rounding = ctx._prec_rounding
         if 'rounding' in kwargs:
             rounding = kwargs['rounding']
         if 'prec' in kwargs:
             prec = kwargs['prec']
             if prec == ctx.inf:
                 return 0, 'f'
             else:
                 prec = int(prec)
         elif 'dps' in kwargs:
             dps = kwargs['dps']
             if dps == ctx.inf:
                 return 0, 'f'
             prec = dps_to_prec(dps)
         return prec, rounding
     return ctx._prec_rounding
Пример #4
0
 def _parse_prec(ctx, kwargs):
     if kwargs:
         if kwargs.get('exact'):
             return 0, 'f'
         prec, rounding = ctx._prec_rounding
         if 'rounding' in kwargs:
             rounding = kwargs['rounding']
         if 'prec' in kwargs:
             prec = kwargs['prec']
             if prec == ctx.inf:
                 return 0, 'f'
             else:
                 prec = int(prec)
         elif 'dps' in kwargs:
             dps = kwargs['dps']
             if dps == ctx.inf:
                 return 0, 'f'
             prec = dps_to_prec(dps)
         return prec, rounding
     return ctx._prec_rounding
Пример #5
0
 def _parse_prec(ctx, kwargs):
     if kwargs:
         if kwargs.get("exact"):
             return 0, "f"
         prec, rounding = ctx._prec_rounding
         if "rounding" in kwargs:
             rounding = kwargs["rounding"]
         if "prec" in kwargs:
             prec = kwargs["prec"]
             if prec == ctx.inf:
                 return 0, "f"
             else:
                 prec = int(prec)
         elif "dps" in kwargs:
             dps = kwargs["dps"]
             if dps == ctx.inf:
                 return 0, "f"
             prec = dps_to_prec(dps)
         return prec, rounding
     return ctx._prec_rounding
Пример #6
0
 def f(x, **kwargs):
     if type(x) not in ctx.types:
         x = ctx.convert(x)
     prec, rounding = ctx._prec_rounding
     if kwargs:
         prec = kwargs.get('prec', prec)
         if 'dps' in kwargs:
             prec = dps_to_prec(kwargs['dps'])
         rounding = kwargs.get('rounding', rounding)
     if hasattr(x, '_mpf_'):
         try:
             return ctx.make_mpf(mpf_f(x._mpf_, prec, rounding))
         except ComplexResult:
             # Handle propagation to complex
             if ctx.trap_complex:
                 raise
             return ctx.make_mpc(mpc_f((x._mpf_, fzero), prec, rounding))
     elif hasattr(x, '_mpc_'):
         return ctx.make_mpc(mpc_f(x._mpc_, prec, rounding))
     raise NotImplementedError("%s of a %s" % (name, type(x)))
Пример #7
0
 def f(x, **kwargs):
     if type(x) not in ctx.types:
         x = ctx.convert(x)
     prec, rounding = ctx._prec_rounding
     if kwargs:
         prec = kwargs.get('prec', prec)
         if 'dps' in kwargs:
             prec = dps_to_prec(kwargs['dps'])
         rounding = kwargs.get('rounding', rounding)
     if hasattr(x, '_mpf_'):
         try:
             return ctx.make_mpf(mpf_f(x._mpf_, prec, rounding))
         except ComplexResult:
             # Handle propagation to complex
             if ctx.trap_complex:
                 raise
             return ctx.make_mpc(mpc_f((x._mpf_, fzero), prec,
                                       rounding))
     elif hasattr(x, '_mpc_'):
         return ctx.make_mpc(mpc_f(x._mpc_, prec, rounding))
     raise NotImplementedError("%s of a %s" % (name, type(x)))
Пример #8
0
 def _set_dps(ctx, n):
     ctx._prec = ctx._prec_rounding[0] = dps_to_prec(n)
     ctx._dps = max(1, int(n))
Пример #9
0
 def __call__(self, prec=None, dps=None, rounding=None):
     prec2, rounding2 = self.context._prec_rounding
     if not prec: prec = prec2
     if not rounding: rounding = rounding2
     if dps: prec = dps_to_prec(dps)
     return self.context.make_mpf(self.func(prec, rounding))
Пример #10
0
 def _set_dps(ctx, n):
     ctx._prec[0] = dps_to_prec(n)
     ctx._dps = max(1, int(n))
Пример #11
0
 def _set_dps(ctx, n):
     ctx._prec[0] = dps_to_prec(n)
     ctx._dps = max(1, int(n))
Пример #12
0
 def _set_dps(ctx, n):
     ctx._prec = ctx._prec_rounding[0] = dps_to_prec(n)
     ctx._dps = max(1, int(n))
Пример #13
0
 def __call__(self, prec=None, dps=None, rounding=None):
     prec2, rounding2 = self.context._prec_rounding
     if not prec: prec = prec2
     if not rounding: rounding = rounding2
     if dps: prec = dps_to_prec(dps)
     return self.context.make_mpf(self.func(prec, rounding))