From 3071778e37112377b592b180769f0a44496dc2da Mon Sep 17 00:00:00 2001 From: Jaime Fernandez Date: Sun, 27 Mar 2016 19:32:23 +0200 Subject: [PATCH 1/2] ENH: Add bits attribute to np.finfo, closes #1886 Add a bits attribute to np.finfo, document the existence of this attribute for both np.iinfo and np.finfo, plus some extra spacing around the __str__ labels to make the representation consistent for both classes. --- numpy/core/getlimits.py | 17 +++++++++++------ numpy/core/tests/test_getlimits.py | 18 ++++++++++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/numpy/core/getlimits.py b/numpy/core/getlimits.py index 2ea9c0e11af8..d4025cb3be95 100644 --- a/numpy/core/getlimits.py +++ b/numpy/core/getlimits.py @@ -30,6 +30,8 @@ class finfo(object): Attributes ---------- + bits : int + The number of bits occupied by the type. eps : float The smallest representable positive number such that ``1.0 + eps != 1.0``. Type of `eps` is an appropriate floating @@ -157,6 +159,7 @@ def _init(self, dtype): setattr(self, word, getattr(machar, word)) for word in ['tiny', 'resolution', 'epsneg']: setattr(self, word, getattr(machar, word).flat[0]) + self.bits = self.dtype.itemsize * 8 self.max = machar.huge.flat[0] self.min = -self.max self.eps = machar.eps.flat[0] @@ -174,12 +177,12 @@ def __str__(self): fmt = ( 'Machine parameters for %(dtype)s\n' '---------------------------------------------------------------\n' - 'precision=%(precision)3s resolution= %(_str_resolution)s\n' - 'machep=%(machep)6s eps= %(_str_eps)s\n' - 'negep =%(negep)6s epsneg= %(_str_epsneg)s\n' - 'minexp=%(minexp)6s tiny= %(_str_tiny)s\n' - 'maxexp=%(maxexp)6s max= %(_str_max)s\n' - 'nexp =%(nexp)6s min= -max\n' + 'precision = %(precision)3s resolution = %(_str_resolution)s\n' + 'machep = %(machep)6s eps = %(_str_eps)s\n' + 'negep = %(negep)6s epsneg = %(_str_epsneg)s\n' + 'minexp = %(minexp)6s tiny = %(_str_tiny)s\n' + 'maxexp = %(maxexp)6s max = %(_str_max)s\n' + 'nexp = %(nexp)6s min = -max\n' '---------------------------------------------------------------\n' ) return fmt % self.__dict__ @@ -200,6 +203,8 @@ class iinfo(object): Attributes ---------- + bits : int + The number of bits occupied by the type. min : int The smallest integer expressible by the type. max : int diff --git a/numpy/core/tests/test_getlimits.py b/numpy/core/tests/test_getlimits.py index c36d7c0684ba..600f8f52c5cb 100644 --- a/numpy/core/tests/test_getlimits.py +++ b/numpy/core/tests/test_getlimits.py @@ -42,6 +42,19 @@ def test_singleton(self,level=2): ftype2 = finfo(longdouble) assert_equal(id(ftype), id(ftype2)) +class TestFinfo(TestCase): + def test_basic(self): + dts = list(zip(['f2', 'f4', 'f8', 'c8', 'c16'], + [np.float16, np.float32, np.float64, np.complex64, + np.complex128])) + for dt1, dt2 in dts: + for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machar', 'machep', + 'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp', + 'nmant', 'precision', 'resolution', 'tiny'): + assert_equal(getattr(finfo(dt1), attr), + getattr(finfo(dt2), attr), attr) + self.assertRaises(ValueError, finfo, 'i4') + class TestIinfo(TestCase): def test_basic(self): dts = list(zip(['i1', 'i2', 'i4', 'i8', @@ -49,8 +62,9 @@ def test_basic(self): [np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16, np.uint32, np.uint64])) for dt1, dt2 in dts: - assert_equal(iinfo(dt1).min, iinfo(dt2).min) - assert_equal(iinfo(dt1).max, iinfo(dt2).max) + for attr in ('bits', 'min', 'max'): + assert_equal(getattr(iinfo(dt1), attr), + getattr(iinfo(dt2), attr), attr) self.assertRaises(ValueError, iinfo, 'f4') def test_unsigned_max(self): From 1b162c697536313e993fbc80cab297957e11665d Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Sat, 4 Jun 2016 16:23:49 -0600 Subject: [PATCH 2/2] DOC: Mention bits attribute added to finfo in 1.12.0 release notes. --- doc/release/1.12.0-notes.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/doc/release/1.12.0-notes.rst b/doc/release/1.12.0-notes.rst index 07871c10ceef..9302e02c6df7 100644 --- a/doc/release/1.12.0-notes.rst +++ b/doc/release/1.12.0-notes.rst @@ -164,8 +164,8 @@ Generalized Ufuncs will now unlock the GIL Generalized Ufuncs, including most of the linalg module, will now unlock the Python global interpreter lock. -np.roll can now roll multiple axes at the same time -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``np.roll can now roll multiple axes at the same time`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ The ``shift`` and ``axis`` arguments to ``roll`` are now broadcast against each other, and each specified axis is shifted accordingly. @@ -180,6 +180,11 @@ The standard ``np.load``, ``np.save``, ``np.loadtxt``, ``np.savez``, and similar functions can now take ``pathlib.Path`` objects as an argument instead of a filename or open file object. +Add ``bits`` attribute to ``np.finfo`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This makes ``np.finfo`` consistent with ``np.iinfo`` which already has that +attribute. + Changes =======