Example #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
 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
Example #3
0
 def __hash__(s):
     a, b = s._mpq_
     if b == 1:
         return hash(a)
     # Power of two: mpf compatible hash
     if not (b & (b-1)):
         return mpf_hash(from_man_exp(a, 1-bitcount(b)))
     return hash((a,b))
Example #4
0
 def __hash__(s):
     a, b = s._mpq_
     if b == 1:
         return hash(a)
     # Power of two: mpf compatible hash
     if not (b & (b - 1)):
         return mpf_hash(from_man_exp(a, 1 - bitcount(b)))
     return hash((a, b))