66from __future__ import annotations
77
88import warnings
9- from numbers import Integral
109from platform import machine , processor
1110
1211import numpy as np
@@ -23,6 +22,43 @@ class CastingError(Exception):
2322_test_val = 2 ** 63 + 2 ** 11 # Should be exactly representable in float64
2423TRUNC_UINT64 = np .float64 (_test_val ).astype (np .uint64 ) != _test_val
2524
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+
2662
2763def float_to_int (arr , int_type , nan2zero = True , infmax = False ):
2864 """Convert floating point array `arr` to type `int_type`
@@ -252,7 +288,7 @@ def type_info(np_type):
252288 return ret
253289 info_64 = np .finfo (np .float64 )
254290 if dt .kind == 'c' :
255- assert np_type is np .longcomplex
291+ assert np_type is np .clongdouble
256292 vals = (nmant , nexp , width / 2 )
257293 else :
258294 assert np_type is np .longdouble
@@ -280,7 +316,7 @@ def type_info(np_type):
280316 # Oh dear, we don't recognize the type information. Try some known types
281317 # and then give up. At this stage we're expecting exotic longdouble or
282318 # 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 ):
284320 raise FloatingError (f'We had not expected type { np_type } ' )
285321 if vals == (1 , 1 , 16 ) and on_powerpc () and _check_maxexp (np .longdouble , 1024 ):
286322 # double pair on PPC. The _check_nmant routine does not work for this
@@ -290,13 +326,13 @@ def type_info(np_type):
290326 # Got float64 despite everything
291327 pass
292328 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
294330 # seems to break here too, so we need to use np.longdouble and
295331 # complexify
296332 two = np .longdouble (2 )
297333 # See: https://matthew-brett.github.io/pydagogue/floating_point.html
298334 max_val = (two ** 113 - 1 ) / (two ** 112 ) * two ** 16383
299- if np_type is np .longcomplex :
335+ if np_type is np .clongdouble :
300336 max_val += 0j
301337 ret = dict (
302338 min = - max_val ,
@@ -453,9 +489,7 @@ def int_to_float(val, flt_type):
453489 return flt_type (val )
454490 # The following works around a nasty numpy 1.4.1 bug such that:
455491 # >>> int(np.uint32(2**32-1)
456- # -1
457- if not isinstance (val , Integral ):
458- val = int (str (val ))
492+ val = int (val )
459493 faval = np .longdouble (0 )
460494 while val != 0 :
461495 f64 = np .float64 (val )
@@ -714,7 +748,7 @@ def ok_floats():
714748 Remove longdouble if it has no higher precision than float64
715749 """
716750 # copy float list so we don't change the numpy global
717- floats = np . sctypes ['float' ][:]
751+ floats = sctypes ['float' ][:]
718752 if best_float () != np .longdouble and np .longdouble in floats :
719753 floats .remove (np .longdouble )
720754 return sorted (floats , key = lambda f : type_info (f )['nmant' ])
@@ -750,10 +784,10 @@ def able_int_type(values):
750784 mn = min (values )
751785 mx = max (values )
752786 if mn >= 0 :
753- for ityp in np . sctypes ['uint' ]:
787+ for ityp in sctypes ['uint' ]:
754788 if mx <= np .iinfo (ityp ).max :
755789 return ityp
756- for ityp in np . sctypes ['int' ]:
790+ for ityp in sctypes ['int' ]:
757791 info = np .iinfo (ityp )
758792 if mn >= info .min and mx <= info .max :
759793 return ityp
0 commit comments