[Python-checkins] python/nondist/sandbox/decimal Decimal.py, 1.15, 1.16

facundobatista at users.sourceforge.net facundobatista at users.sourceforge.net
Fri Apr 2 21:15:24 EST 2004


Update of /cvsroot/python/python/nondist/sandbox/decimal
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1582

Modified Files:
	Decimal.py 
Log Message:
Added from_float static method. Several fixes.

Index: Decimal.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/decimal/Decimal.py,v
retrieving revision 1.15
retrieving revision 1.16
diff -C2 -d -r1.15 -r1.16
*** Decimal.py	9 Mar 2004 02:44:59 -0000	1.15
--- Decimal.py	3 Apr 2004 02:15:21 -0000	1.16
***************
*** 47,53 ****
  >>> print Decimal(3).sqrt()
  1.73205080756887729
! ### This example doesn't work yet
! ###>>> print Decimal(3) ** 123
! ###4.85192780976896427E+58
  >>> inf = Decimal(1) / Decimal(0)
  >>> print inf
--- 47,52 ----
  >>> print Decimal(3).sqrt()
  1.73205080756887729
! >>> print Decimal(3) ** 123
! 4.85192780976896427E+58
  >>> inf = Decimal(1) / Decimal(0)
  >>> print inf
***************
*** 97,100 ****
--- 96,101 ----
  """
  
+ # 0.7.2     2003.4.02  facundobatista: Fixed __pow__ to run the example ok. Added
+ #                      from_float method. Fixed isnan to accept empty strings.
  # 0.7.1     2003.3.08  facundobatista: Corrected initial examples
  # 0.7.0     2003.2.28  eprice: More changes.  Contexts done nicely, exponent limits
***************
*** 474,492 ****
            return
  
!         if isinstance(value, float):
!             # Another possibility would be:
!             #   self._convertString(_floatToString(value), context)
!             #
!             # This would give a more strictly accurate representation,
!             # but probably isn't what is really meant (Decimal(1.1) !=
!             # Decimal('1.1') this way, but is instead Decimal(\
!             # '1100000000000000088817841970012523233890533447265625e-51')
!             # The extra digits really aren't worth anything, since they
!             # likely weren't intended.
  
!             self._convertString(str(value), context)
!             return
  
!         raise TypeError("Can't convert " + `value`)
  
      def _isnan(self):
--- 475,506 ----
            return
  
!         raise TypeError("Can't convert " + `value`)
  
!     def from_float(cls, value, positions=None, context=None):
!         """Static function that creates Decimal from float
  
!         value must be float
!         if positions is present, rounds to that quantity of decimal places
!         """
!         if not isinstance(value, float):
!             raise TypeError, "value must be float in Decimal.from_float(value, [positions])"
!         if context is None:
!             context = getcontext()
! 
!         # get the *very* exact string representation of the float
!         string_number = _floatToString(value)
!         d = cls(string_number)
! 
!         # round up to positions
!         if positions is not None:
!             if not isinstance(positions, (int,long)):
!                 raise TypeError, "positions must be int or long in Decimal.from_float(value, [positions])"
!             if positions < 0:
!                 raise TypeError, "positions must be not negative in Decimal.from_float(value, [positions])"
! #            d = d.fix(positions)
!             d = d.round(positions, ROUND_HALF_UP)
!             
!         return d
!     from_float = classmethod(from_float)
  
      def _isnan(self):
***************
*** 1677,1680 ****
--- 1691,1697 ----
              context = getcontext()
  
+         if not isinstance(n, Decimal):
+             n = Decimal(n)
+ 
          #Because the spot << doesn't work with really big exponents
          if n._isinfinity() or n.adjusted() > 8:
***************
*** 2583,2586 ****
--- 2600,2605 ----
      if isinstance(num, float):
          num = str(num)
+     if len(num) == 0:
+         return 0
      num = num.lower()
      




More information about the Python-checkins mailing list