[Numpy-svn] r3755 - in trunk/numpy/lib: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Sun May 13 13:36:35 EDT 2007


Author: stefan
Date: 2007-05-13 12:36:19 -0500 (Sun, 13 May 2007)
New Revision: 3755

Modified:
   trunk/numpy/lib/getlimits.py
   trunk/numpy/lib/tests/test_getlimits.py
Log:
Add iinfo based on a patch by Albert Strasheim (ticket #250).


Modified: trunk/numpy/lib/getlimits.py
===================================================================
--- trunk/numpy/lib/getlimits.py	2007-05-13 08:19:11 UTC (rev 3754)
+++ trunk/numpy/lib/getlimits.py	2007-05-13 17:36:19 UTC (rev 3755)
@@ -7,8 +7,8 @@
 import numpy.core.numeric as numeric
 import numpy.core.numerictypes as ntypes
 from numpy.core.numeric import array
+import numpy as N
 
-
 def _frz(a):
     """fix rank-0 --> rank-1"""
     if a.ndim == 0: a.shape = (1,)
@@ -21,7 +21,16 @@
     }
 
 class finfo(object):
+    """Machine limits for floating point types.
 
+    :Parameters:
+        dtype : floating point type or instance
+
+    :SeeAlso:
+      - numpy.lib.machar.MachAr
+
+    """
+
     _finfo_cache = {}
 
     def __new__(cls, dtype):
@@ -106,6 +115,50 @@
 ---------------------------------------------------------------------
 ''' % self.__dict__
 
+
+class iinfo:
+    """Limits for integer types.
+
+    :Parameters:
+        type : integer type or instance
+
+    """
+
+    # Should be using dtypes as keys, but hash-function isn't yet implemented
+    _min_values = {'int8': -2**7,
+                   'int16': -2**15,
+                   'int32': -2**31,
+                   'int64': -2**63,
+                   'uint8': 0,
+                   'uint16': 0,
+                   'uint32': 0,
+                   'uint64': 0}
+
+    _max_values = {'int8': 2**7 - 1,
+                   'int16': 2**15 - 1,
+                   'int32': 2**31 - 1,
+                   'int64': 2**63 - 1,
+                   'uint8': 2**8 - 1,
+                   'uint16': 2**16 - 1,
+                   'uint32': 2**32 - 1,
+                   'uint64': 2**64 - 1}
+
+    def __init__(self, type):
+        self.dtype = str(N.dtype(type))
+        if not (self.dtype in self._min_values and \
+                self.dtype in self._max_values):
+            raise ValueError("Invalid integer data type.")
+
+    def min(self):
+        """Minimum value of given dtype."""
+        return self._min_values[self.dtype]
+    min = property(min)
+
+    def max(self):
+        """Maximum value of given dtype."""
+        return self._max_values[self.dtype]
+    max = property(max)
+
 if __name__ == '__main__':
     f = finfo(ntypes.single)
     print 'single epsilon:',f.eps

Modified: trunk/numpy/lib/tests/test_getlimits.py
===================================================================
--- trunk/numpy/lib/tests/test_getlimits.py	2007-05-13 08:19:11 UTC (rev 3754)
+++ trunk/numpy/lib/tests/test_getlimits.py	2007-05-13 17:36:19 UTC (rev 3755)
@@ -4,8 +4,9 @@
 from numpy.testing import *
 set_package_path()
 import numpy.lib;reload(numpy.lib)
-from numpy.lib.getlimits import finfo
+from numpy.lib.getlimits import finfo, iinfo
 from numpy import single,double,longdouble
+import numpy as N
 restore_path()
 
 ##################################################
@@ -34,5 +35,21 @@
         ftype2 = finfo(longdouble)
         assert_equal(id(ftype),id(ftype2))
 
+class test_iinfo(NumpyTestCase):
+    def check_basic(self):
+        dts = zip(['i1', 'i2', 'i4', 'i8',
+                   'u1', 'u2', 'u4', 'u8'],
+                  [N.int8, N.int16, N.int32, N.int64,
+                   N.uint8, N.uint16, N.uint32, N.uint64])
+        for dt1, dt2 in dts:
+            assert_equal(iinfo(dt1).min, iinfo(dt2).min)
+            assert_equal(iinfo(dt1).max, iinfo(dt2).max)
+        self.assertRaises(ValueError, iinfo, 'f4')
+
+    def check_unsigned_max(self):
+        types = N.sctypes['uint']
+        for T in types:
+            assert_equal(iinfo(T).max, T(-1))
+
 if __name__ == "__main__":
     NumpyTest().run()




More information about the Numpy-svn mailing list