diff options
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/getlimits.py | 55 | ||||
-rw-r--r-- | numpy/lib/tests/test_getlimits.py | 19 |
2 files changed, 72 insertions, 2 deletions
diff --git a/numpy/lib/getlimits.py b/numpy/lib/getlimits.py index d03f000c1..a0a2a94ca 100644 --- a/numpy/lib/getlimits.py +++ b/numpy/lib/getlimits.py @@ -7,7 +7,7 @@ from machar import MachAr 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""" @@ -21,6 +21,15 @@ _convert_to_float = { } class finfo(object): + """Machine limits for floating point types. + + :Parameters: + dtype : floating point type or instance + + :SeeAlso: + - numpy.lib.machar.MachAr + + """ _finfo_cache = {} @@ -106,6 +115,50 @@ nexp =%(nexp)6s min= -max --------------------------------------------------------------------- ''' % 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 diff --git a/numpy/lib/tests/test_getlimits.py b/numpy/lib/tests/test_getlimits.py index 1eaadc953..7a4fea57a 100644 --- a/numpy/lib/tests/test_getlimits.py +++ b/numpy/lib/tests/test_getlimits.py @@ -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 @@ class test_longdouble(NumpyTestCase): 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() |