Beispiel #1
0
 def _convert_tol(ctx, tol):
     if isinstance(tol, int_types):
         return from_int(tol)
     if isinstance(tol, float):
         return from_float(tol)
     if hasattr(tol, "_mpf_"):
         return tol._mpf_
     prec, rounding = ctx._prec_rounding
     if isinstance(tol, basestring):
         return from_str(tol, prec, rounding)
     raise ValueError("expected a real number, got %s" % tol)
Beispiel #2
0
 def _convert_tol(ctx, tol):
     if isinstance(tol, int_types):
         return from_int(tol)
     if isinstance(tol, float):
         return from_float(tol)
     if hasattr(tol, "_mpf_"):
         return tol._mpf_
     prec, rounding = ctx._prec_rounding
     if isinstance(tol, basestring):
         return from_str(tol, prec, rounding)
     raise ValueError("expected a real number, got %s" % tol)
Beispiel #3
0
 def __new__(cls, num, prec=15):
     prec = mlib.libmpf.dps_to_prec(prec)
     if isinstance(num, (int, long)):
         return Integer(num)
     if isinstance(num, (str, decimal.Decimal)):
         _mpf_ = mlib.from_str(str(num), prec, rnd)
     elif isinstance(num, tuple) and len(num) == 4:
         if type(num[1]) is str:
             # it's a hexadecimal (coming from a pickled object)
             # assume that it is in standard form
             num = list(num)
             num[1] = long(num[1], 16)
             _mpf_ = tuple(num)
         else:
             _mpf_ = mpmath.mpf(
                 S.NegativeOne ** num[0] * num[1] * 2 ** num[2])._mpf_
     else:
         _mpf_ = mpmath.mpf(num)._mpf_
     if not num:
         return C.Zero()
     obj = Expr.__new__(cls)
     obj._mpf_ = _mpf_
     obj._prec = prec
     return obj
Beispiel #4
0
 def __new__(cls, num, prec=15):
     prec = mlib.libmpf.dps_to_prec(prec)
     if isinstance(num, (int, long)):
         return Integer(num)
     if isinstance(num, (str, decimal.Decimal)):
         _mpf_ = mlib.from_str(str(num), prec, rnd)
     elif isinstance(num, tuple) and len(num) == 4:
         if type(num[1]) is str:
             # it's a hexadecimal (coming from a pickled object)
             # assume that it is in standard form
             num = list(num)
             num[1] = long(num[1], 16)
             _mpf_ = tuple(num)
         else:
             _mpf_ = mpmath.mpf(
                 S.NegativeOne ** num[0] * num[1] * 2 ** num[2])._mpf_
     else:
         _mpf_ = mpmath.mpf(num)._mpf_
     if not num:
         return C.Zero()
     obj = Expr.__new__(cls)
     obj._mpf_ = _mpf_
     obj._prec = prec
     return obj