Skip to content

Commit 3071778

Browse files
jaimefriocharris
authored andcommitted
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.
1 parent cc51ba8 commit 3071778

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

‎numpy/core/getlimits.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class finfo(object):
3030
3131
Attributes
3232
----------
33+
bits : int
34+
The number of bits occupied by the type.
3335
eps : float
3436
The smallest representable positive number such that
3537
``1.0 + eps != 1.0``. Type of `eps` is an appropriate floating
@@ -157,6 +159,7 @@ def _init(self, dtype):
157159
setattr(self, word, getattr(machar, word))
158160
for word in ['tiny', 'resolution', 'epsneg']:
159161
setattr(self, word, getattr(machar, word).flat[0])
162+
self.bits = self.dtype.itemsize * 8
160163
self.max = machar.huge.flat[0]
161164
self.min = -self.max
162165
self.eps = machar.eps.flat[0]
@@ -174,12 +177,12 @@ def __str__(self):
174177
fmt = (
175178
'Machine parameters for %(dtype)s\n'
176179
'---------------------------------------------------------------\n'
177-
'precision=%(precision)3s resolution= %(_str_resolution)s\n'
178-
'machep=%(machep)6s eps= %(_str_eps)s\n'
179-
'negep =%(negep)6s epsneg= %(_str_epsneg)s\n'
180-
'minexp=%(minexp)6s tiny= %(_str_tiny)s\n'
181-
'maxexp=%(maxexp)6s max= %(_str_max)s\n'
182-
'nexp =%(nexp)6s min= -max\n'
180+
'precision = %(precision)3s resolution = %(_str_resolution)s\n'
181+
'machep = %(machep)6s eps = %(_str_eps)s\n'
182+
'negep = %(negep)6s epsneg = %(_str_epsneg)s\n'
183+
'minexp = %(minexp)6s tiny = %(_str_tiny)s\n'
184+
'maxexp = %(maxexp)6s max = %(_str_max)s\n'
185+
'nexp = %(nexp)6s min = -max\n'
183186
'---------------------------------------------------------------\n'
184187
)
185188
return fmt % self.__dict__
@@ -200,6 +203,8 @@ class iinfo(object):
200203
201204
Attributes
202205
----------
206+
bits : int
207+
The number of bits occupied by the type.
203208
min : int
204209
The smallest integer expressible by the type.
205210
max : int

‎numpy/core/tests/test_getlimits.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,29 @@ def test_singleton(self,level=2):
4242
ftype2 = finfo(longdouble)
4343
assert_equal(id(ftype), id(ftype2))
4444

45+
class TestFinfo(TestCase):
46+
def test_basic(self):
47+
dts = list(zip(['f2', 'f4', 'f8', 'c8', 'c16'],
48+
[np.float16, np.float32, np.float64, np.complex64,
49+
np.complex128]))
50+
for dt1, dt2 in dts:
51+
for attr in ('bits', 'eps', 'epsneg', 'iexp', 'machar', 'machep',
52+
'max', 'maxexp', 'min', 'minexp', 'negep', 'nexp',
53+
'nmant', 'precision', 'resolution', 'tiny'):
54+
assert_equal(getattr(finfo(dt1), attr),
55+
getattr(finfo(dt2), attr), attr)
56+
self.assertRaises(ValueError, finfo, 'i4')
57+
4558
class TestIinfo(TestCase):
4659
def test_basic(self):
4760
dts = list(zip(['i1', 'i2', 'i4', 'i8',
4861
'u1', 'u2', 'u4', 'u8'],
4962
[np.int8, np.int16, np.int32, np.int64,
5063
np.uint8, np.uint16, np.uint32, np.uint64]))
5164
for dt1, dt2 in dts:
52-
assert_equal(iinfo(dt1).min, iinfo(dt2).min)
53-
assert_equal(iinfo(dt1).max, iinfo(dt2).max)
65+
for attr in ('bits', 'min', 'max'):
66+
assert_equal(getattr(iinfo(dt1), attr),
67+
getattr(iinfo(dt2), attr), attr)
5468
self.assertRaises(ValueError, iinfo, 'f4')
5569

5670
def test_unsigned_max(self):

0 commit comments

Comments
 (0)