Skip to content

Commit a50fc88

Browse files
committed
RF - refactor type_info to handle all complex
Don't check nmant etc logic for types where we can be confident (e.g. float32)
1 parent 33f6369 commit a50fc88

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

nibabel/casting.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,30 +200,29 @@ def type_info(np_type):
200200
return dict(min=np_type(info.min), max=np_type(info.max),
201201
nmant=None, nexp=None, width=width)
202202
info = np.finfo(dt)
203-
vals = info.nmant, info.nexp, width
204-
if vals == (10, 5, 2): # binary16
205-
assert np_type is _float16
206-
elif vals == (23, 8, 4): # binary32
207-
assert np_type is np.float32
208-
elif vals == (23, 8, 8): # binary32, complex
209-
assert np_type is np.complex64
210-
elif vals == (52, 11, 8): # binary64
211-
assert np_type in (np.float64, np.longdouble)
212-
elif vals == (52, 11, 16): # binary64, complex
213-
assert np_type is np.complex128
214-
elif vals == (112, 15, 16): # binary128
215-
assert np_type is np.longdouble
216-
elif vals in ((63, 15, 12), (63, 15, 16)): # Intel extended 80
203+
# Trust the standard IEEE types
204+
nmant, nexp = info.nmant, info.nexp
205+
ret = dict(min=np_type(info.min), max=np_type(info.max), nmant=nmant,
206+
nexp=nexp, width=width)
207+
if np_type in (_float16, np.float32, np.float64,
208+
np.complex64, np.complex128):
209+
return ret
210+
if dt.kind == 'c':
211+
assert np_type is np.longcomplex
212+
vals = (nmant, nexp, width / 2)
213+
else:
217214
assert np_type is np.longdouble
215+
vals = (nmant, nexp, width)
216+
if vals in ((112, 15, 16), # binary128
217+
(63, 15, 12), (63, 15, 16)): # Intel extended 80
218+
pass # these are OK
218219
elif vals == (1, 1, 16) and processor() == 'powerpc': # broken PPC
219-
assert np_type is np.longdouble
220220
dbl_info = np.finfo(np.float64)
221221
return dict(min=np_type(dbl_info.min), max=np_type(dbl_info.max),
222222
nmant=106, nexp=11, width=width)
223223
else: # don't recognize the type
224224
raise FloatingError('We had not expected this type')
225-
return dict(min=np_type(info.min), max=np_type(info.max), nmant=info.nmant,
226-
nexp=info.nexp, width=width)
225+
return ret
227226

228227

229228
def as_int(x, check=True):

0 commit comments

Comments
 (0)