Skip to content

Commit 2334a25

Browse files
committed
RF - make sure min max are of input type
The iinfo and finfo types don't guarantee this.
1 parent f30b8c0 commit 2334a25

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

nibabel/casting.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def type_info(np_type):
183183
with fields ``min`` (minimum value), ``max`` (maximum value), ``nexp``
184184
(exponent width), ``nmant`` (significand precision not including
185185
implicit first digit) ``width`` (width in bytes). ``nexp``, ``nmant``
186-
are None for integer types.
186+
are None for integer types. Both ``min`` and ``max`` are of type
187+
`np_type`.
187188
188189
Raises
189190
------
@@ -197,39 +198,40 @@ def type_info(np_type):
197198
that we know are likely to be correct.
198199
"""
199200
dt = np.dtype(np_type)
201+
np_type = dt.type
200202
width = dt.itemsize
201203
try: # integer type
202204
info = np.iinfo(dt)
203205
except ValueError:
204206
pass
205207
else:
206-
return dict(min=info.min, max=info.max, nmant=None, nexp=None,
207-
width=width)
208+
return dict(min=np_type(info.min), max=np_type(info.max),
209+
nmant=None, nexp=None, width=width)
208210
info = np.finfo(dt)
209211
vals = info.nmant, info.nexp, width
210212
if vals == (10, 5, 2): # binary16
211-
assert dt.type is _float16
213+
assert np_type is _float16
212214
elif vals == (23, 8, 4): # binary32
213-
assert dt.type is np.float32
215+
assert np_type is np.float32
214216
elif vals == (23, 8, 8): # binary32, complex
215-
assert dt.type is np.complex64
217+
assert np_type is np.complex64
216218
elif vals == (52, 11, 8): # binary64
217-
assert dt.type in (np.float64, np.longdouble)
219+
assert np_type in (np.float64, np.longdouble)
218220
elif vals == (52, 11, 16): # binary64, complex
219-
assert dt.type is np.complex128
221+
assert np_type is np.complex128
220222
elif vals == (112, 15, 16): # binary128
221-
assert dt.type is np.longdouble
223+
assert np_type is np.longdouble
222224
elif vals in ((63, 15, 12), (63, 15, 16)): # Intel extended 80
223-
assert dt.type is np.longdouble
225+
assert np_type is np.longdouble
224226
elif vals == (1, 1, 16) and processor() == 'powerpc': # broken PPC
225-
assert dt.type is np.longdouble
227+
assert np_type is np.longdouble
226228
dbl_info = np.finfo(np.float64)
227-
return dict(min=dbl_info.min, max=dbl_info.max, nmant=106, nexp=11,
228-
width=width)
229+
return dict(min=np_type(dbl_info.min), max=np_type(dbl_info.max),
230+
nmant=106, nexp=11, width=width)
229231
else: # don't recognize the type
230232
raise FloatingError('We had not expected this type')
231-
return dict(min=info.min, max=info.max, nmant=info.nmant, nexp=info.nexp,
232-
width=width)
233+
return dict(min=np_type(info.min), max=np_type(info.max), nmant=info.nmant,
234+
nexp=info.nexp, width=width)
233235

234236

235237
def flt2nmant(flt_type):

nibabel/tests/test_floating.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,17 @@ def test_type_info():
2828
infod = type_info(dtt)
2929
assert_equal(dict(min=info.min, max=info.max, nexp=None, nmant=None,
3030
width=np.dtype(dtt).itemsize), infod)
31+
assert_equal(infod['min'].dtype.type, dtt)
32+
assert_equal(infod['max'].dtype.type, dtt)
3133
for dtt in IEEE_floats + [np.complex64, np.complex64]:
3234
info = np.finfo(dtt)
3335
infod = type_info(dtt)
3436
assert_equal(dict(min=info.min, max=info.max,
3537
nexp=info.nexp, nmant=info.nmant,
3638
width=np.dtype(dtt).itemsize),
3739
infod)
40+
assert_equal(infod['min'].dtype.type, dtt)
41+
assert_equal(infod['max'].dtype.type, dtt)
3842
# What is longdouble?
3943
info = np.finfo(np.longdouble)
4044
dbl_info = np.finfo(np.float64)

0 commit comments

Comments
 (0)