6
6
from __future__ import annotations
7
7
8
8
import warnings
9
- from numbers import Integral
10
9
from platform import machine , processor
11
10
12
11
import numpy as np
@@ -23,6 +22,43 @@ class CastingError(Exception):
23
22
_test_val = 2 ** 63 + 2 ** 11 # Should be exactly representable in float64
24
23
TRUNC_UINT64 = np .float64 (_test_val ).astype (np .uint64 ) != _test_val
25
24
25
+ # np.sctypes is deprecated in numpy 2.0 and np.core.sctypes should not be used instead.
26
+ sctypes = {
27
+ 'int' : [
28
+ getattr (np , dtype ) for dtype in ('int8' , 'int16' , 'int32' , 'int64' ) if hasattr (np , dtype )
29
+ ],
30
+ 'uint' : [
31
+ getattr (np , dtype )
32
+ for dtype in ('uint8' , 'uint16' , 'uint32' , 'uint64' )
33
+ if hasattr (np , dtype )
34
+ ],
35
+ 'float' : [
36
+ getattr (np , dtype )
37
+ for dtype in ('float16' , 'float32' , 'float64' , 'float96' , 'float128' )
38
+ if hasattr (np , dtype )
39
+ ],
40
+ 'complex' : [
41
+ getattr (np , dtype )
42
+ for dtype in ('complex64' , 'complex128' , 'complex192' , 'complex256' )
43
+ if hasattr (np , dtype )
44
+ ],
45
+ 'others' : [bool , object , bytes , str , np .void ],
46
+ }
47
+ sctypes_aliases = {
48
+ getattr (np , dtype )
49
+ for dtype in (
50
+ 'int8' , 'byte' , 'int16' , 'short' , 'int32' , 'intc' , 'int_' , 'int64' , 'longlong' ,
51
+ 'uint8' , 'ubyte' , 'uint16' , 'ushort' , 'uint32' , 'uintc' , 'uint' , 'uint64' , 'ulonglong' , # noqa: E501
52
+ 'float16' , 'half' , 'float32' , 'single' , 'float64' , 'double' , 'float96' , 'float128' , 'longdouble' , # noqa: E501
53
+ 'complex64' , 'csingle' , 'complex128' , 'cdouble' , 'complex192' , 'complex256' , 'clongdouble' , # noqa: E501
54
+ # other names of the built-in scalar types
55
+ 'int_' , 'float_' , 'complex_' , 'bytes_' , 'str_' , 'bool_' , 'datetime64' , 'timedelta64' , # noqa: E501
56
+ # other
57
+ 'object_' , 'void' ,
58
+ )
59
+ if hasattr (np , dtype )
60
+ } # fmt:skip
61
+
26
62
27
63
def float_to_int (arr , int_type , nan2zero = True , infmax = False ):
28
64
"""Convert floating point array `arr` to type `int_type`
@@ -252,7 +288,7 @@ def type_info(np_type):
252
288
return ret
253
289
info_64 = np .finfo (np .float64 )
254
290
if dt .kind == 'c' :
255
- assert np_type is np .longcomplex
291
+ assert np_type is np .clongdouble
256
292
vals = (nmant , nexp , width / 2 )
257
293
else :
258
294
assert np_type is np .longdouble
@@ -280,7 +316,7 @@ def type_info(np_type):
280
316
# Oh dear, we don't recognize the type information. Try some known types
281
317
# and then give up. At this stage we're expecting exotic longdouble or
282
318
# their complex equivalent.
283
- if np_type not in (np .longdouble , np .longcomplex ) or width not in (16 , 32 ):
319
+ if np_type not in (np .longdouble , np .clongdouble ) or width not in (16 , 32 ):
284
320
raise FloatingError (f'We had not expected type { np_type } ' )
285
321
if vals == (1 , 1 , 16 ) and on_powerpc () and _check_maxexp (np .longdouble , 1024 ):
286
322
# double pair on PPC. The _check_nmant routine does not work for this
@@ -290,13 +326,13 @@ def type_info(np_type):
290
326
# Got float64 despite everything
291
327
pass
292
328
elif _check_nmant (np .longdouble , 112 ) and _check_maxexp (np .longdouble , 16384 ):
293
- # binary 128, but with some busted type information. np.longcomplex
329
+ # binary 128, but with some busted type information. np.clongdouble
294
330
# seems to break here too, so we need to use np.longdouble and
295
331
# complexify
296
332
two = np .longdouble (2 )
297
333
# See: https://matthew-brett.github.io/pydagogue/floating_point.html
298
334
max_val = (two ** 113 - 1 ) / (two ** 112 ) * two ** 16383
299
- if np_type is np .longcomplex :
335
+ if np_type is np .clongdouble :
300
336
max_val += 0j
301
337
ret = dict (
302
338
min = - max_val ,
@@ -453,9 +489,7 @@ def int_to_float(val, flt_type):
453
489
return flt_type (val )
454
490
# The following works around a nasty numpy 1.4.1 bug such that:
455
491
# >>> int(np.uint32(2**32-1)
456
- # -1
457
- if not isinstance (val , Integral ):
458
- val = int (str (val ))
492
+ val = int (val )
459
493
faval = np .longdouble (0 )
460
494
while val != 0 :
461
495
f64 = np .float64 (val )
@@ -714,7 +748,7 @@ def ok_floats():
714
748
Remove longdouble if it has no higher precision than float64
715
749
"""
716
750
# copy float list so we don't change the numpy global
717
- floats = np . sctypes ['float' ][:]
751
+ floats = sctypes ['float' ][:]
718
752
if best_float () != np .longdouble and np .longdouble in floats :
719
753
floats .remove (np .longdouble )
720
754
return sorted (floats , key = lambda f : type_info (f )['nmant' ])
@@ -750,10 +784,10 @@ def able_int_type(values):
750
784
mn = min (values )
751
785
mx = max (values )
752
786
if mn >= 0 :
753
- for ityp in np . sctypes ['uint' ]:
787
+ for ityp in sctypes ['uint' ]:
754
788
if mx <= np .iinfo (ityp ).max :
755
789
return ityp
756
- for ityp in np . sctypes ['int' ]:
790
+ for ityp in sctypes ['int' ]:
757
791
info = np .iinfo (ityp )
758
792
if mn >= info .min and mx <= info .max :
759
793
return ityp
0 commit comments