10000 ENH: Add bits attribute to np.finfo, closes #1886 by jaimefrio · Pull Request #7471 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add bits attribute to np.finfo, closes #1886 #7471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading< 8000 /span>
Diff view
Diff view
17 changes: 11 additions & 6 deletions numpy/core/getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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__
Expand All @@ -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
Expand Down
18 changes: 16 additions & 2 deletions numpy/core/tests/test_getlimits.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,29 @@ def test_singleton(self,level=2):
ftype2 = finfo(longdouble)
assert_equal(id(ftype), id(ftype2))
< 844B /td>
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',
'u1', 'u2', 'u4', 'u8'],
[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):
Expand Down
0