Quoting from a numpy discussion list:

That information is available via numpy.finfo() and numpy.iinfo():

In [12]: finfo('d').max
Out[12]: 1.7976931348623157e+308

In [13]: iinfo('i').max
Out[13]: 2147483647

In [14]: iinfo('uint8').max
Out[14]: 255

Link here.

Answer from perimosocordiae on Stack Overflow
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
np.zeros(10, dtype='int16') Or using the associated NumPy object: np.zeros(10, dtype=np.int16) More advanced type specification is possible, such as specifying big or little endian numbers; for more information, refer to the NumPy documentation. NumPy also supports compound data types, which will be covered in Structured Data: NumPy's Structured Arrays.
🌐
NumPy
numpy.org › devdocs › user › basics.types.html
Data types — NumPy v2.5.dev0 Manual
>>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) If 64-bit integers are still too small the result may be cast to a floating point number. Floating point numbers offer a larger, but inexact, range of possible values.
🌐
NumPy
numpy.org › doc › stable › user › basics.types.html
Data types — NumPy v2.4 Manual
>>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) If 64-bit integers are still too small the result may be cast to a floating point number. Floating point numbers offer a larger, but inexact, range of possible values.
🌐
w3resource
w3resource.com › numpy › data-types.php
NumPy: Data types - w3resource
>>> import numpy as np >>> c = np.arange(5, dtype=np.uint8) >>> print(c.dtype) uint8
🌐
Pythoninformer
pythoninformer.com › python-libraries › numpy › data-types
PythonInformer - Data types
September 14, 2019 - All of the functions available for created numpy arrays have an optional parameter dtype that allows you to specify the data type (such as np.uint8 or np.float64 etc).
🌐
Hyperskill
hyperskill.org › learn › step › 10095
Data types in NumPy
Hyperskill is an educational platform for learning programming and software development through project-based courses, that helps you secure a job in tech. Master Python, Java, Kotlin, and more with real-world coding challenges.
Find elsewhere
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.iinfo.html
numpy.iinfo — NumPy v2.3 Manual
>>> import numpy as np >>> ii16 = np.iinfo(np.int16) >>> ii16.min -32768 >>> ii16.max 32767 >>> ii32 = np.iinfo(np.int32) >>> ii32.min -2147483648 >>> ii32.max 2147483647
🌐
Sling Academy
slingacademy.com › article › understanding-numpy-int16-and-numpy-uint16-types-6-examples
Understanding numpy.int16 and numpy.uint16 types (6 examples) - Sling Academy
Here lies their primary difference – the range of values they can represent. numpy.int16 can represent values from -32768 to 32767, whereas numpy.uint16 can represent values from 0 to 65535. import numpy as np # Creating arrays with numpy.int16 and numpy.uint16 types data_int16 = np.array([32767, ...
🌐
Real Python
realpython.com › how-to-use-numpy-arange
NumPy arange(): How to Use np.arange() – Real Python
July 31, 2023 - np.int16: 16-bit signed integer (from -32768 to 32767) np.uint16: 16-bit unsigned integer (from 0 to 65535) np.int32: 32-bit signed integer (from -2**31 to 2**31-1) np.uint32: 32-bit unsigned integer (from 0 to 2**32-1) np.int64: 64-bit signed ...
🌐
NumPy
numpy.org › doc › 1.22 › user › basics.types.html
Data types — NumPy v1.22 Manual
>>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) If 64-bit integers are still too small the result may be cast to a floating point number. Floating point numbers offer a larger, but inexact, range of possible values.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_data_types.htm
NumPy - Data Types
import numpy as np # Creating an array with large float values b = np.array([1.1e10, 2.2e10, 3.3e10]) print("Original array:", b) print("Original dtype:", b.dtype) try: # Attempting to convert to integer b_int = b.astype(np.int32) print("Converted array:", b_int) print("Converted dtype:", b_int.dtype) except OverflowError as e: print("Error:", e) Here, the large float values cannot be converted to int32 without overflow − · Original array: [1.1e+10 2.2e+10 3.3e+10] Original dtype: float64 Error: OverflowError: (34, 'Numerical result out of range')
🌐
Runebook
runebook.dev › english › articles › numpy
Working with 16-Bit Integers in NumPy: numpy.int16 Scalars
This code will print the overflow error message because 32769 is outside the representable range of int16. # Create an int16 scalar and a float scalar int16_value = np.int16(10) float_value = 3.14 # Direct addition might lead to precision loss direct_sum = int16_value + float_value print(f"Direct sum (might lose precision): {direct_sum}") # Output: 13.0 (float) # Explicit casting for better control explicit_sum = np.int16(int16_value + int(float_value)) print(f"Explicit casting (maintains int16): {explicit_sum}") # Output: 13 (int16)
Top answer
1 of 2
7

The MATLAB output has saturated at intmin('int16') = -32768 (docs), i.e. the most negative value it can represent as an int16 variable.

Python has the same range for int16 (docs), but instead of saturating at the most negative value it has experienced underflow, wrapping around to the top of the range

k = round(-3.406578165491512e+04) = -34066
k = k + (32768*2) = 31470

You could get around this by enforcing whichever of these is your preferred behaviour when the inputs are still floating point values, and then when the input is within the range -32768 to 32767 you can cast it to int16.

2 of 2
5

Wolfie gets at the difference, this is about how to solve it. If you're OK with clipping, then you can use iinfo to get the min and max values of an integer type (or hard-code it, if you know you won't be changing it from int16 ever) and then use clip to constrain the float to be within those bounds before casting it.

n = -3.406578165491512e+04

ii = np.iinfo(np.int16)
print(f"min = {ii.min}") # min = -32768
print(f"max = {ii.max}") # max = 32767

np.int16(np.clip(n, ii.min, ii.max))
# -32768

IMPORTANT NOTE: This is only reliable if the size if your float is larger than the size of the int, because it relies upon being able to represent ii.max exactly as a float. See here for a discussion of when this is not true.

Here's an example of that failing

n = np.float64(1e100)

ii = np.iinfo(np.int64)
print(f"max: {ii.max}") # max = 9223372036854775807

clipped = np.clip(n, ii.min, ii.max)
print(f"clipped to: {int(clipped)}") # clipped to: 9223372036854775808
print(f"as int: {np.int64(clipped)}") # as int: -9223372036854775808

(This happens because ii.max cannot be represented as a float. Past 9007199254740992, we lose the 1's place of precision and can only specify even integers, so the bounds of the clipping become incorrect.)

🌐
NumPy
numpy.org › doc › 1.18 › user › basics.types.html
Data types — NumPy v1.18 Manual
>>> np.iinfo(int) # Bounds of the default integer on this system. iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) >>> np.iinfo(np.int32) # Bounds of a 32-bit integer iinfo(min=-2147483648, max=2147483647, dtype=int32) >>> np.iinfo(np.int64) # Bounds of a 64-bit integer iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64) If 64-bit integers are still too small the result may be cast to a floating point number. Floating point numbers offer a larger, but inexact, range of possible values.
🌐
Medium
medium.com › @pritioli › essential-numpy-data-types-a-must-know-guide-ad3657f708b7
Essential NumPy Data Types: A Must-Know Guide | by Code & Cognition | Medium
December 22, 2023 - Python’s default floating-point numbers are typically 64-bit, equivalent to np.float64, but in cases where extra precision is needed, higher precision floating-point numbers can be employed. Numpy Data Type according to the different bit length · Integer Types: int8, int16, int32, int64: Signed integers with varying bit lengths.
🌐
TutorialsPoint
tutorialspoint.com › article › get-the-machine-limits-information-for-integer-types-in-python
Get the Machine limits information for integer types in Python
import numpy as np # To get the machine limits information for integer types, use the numpy.iinfo() method in Python Numpy # The first parameter is the int_type i.e. the kind of integer data type to get information about. # Checking for int16 type # The min is the minimum value of given dtype.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.iinfo.html
numpy.iinfo — NumPy v2.2 Manual
>>> import numpy as np >>> ii16 = np.iinfo(np.int16) >>> ii16.min -32768 >>> ii16.max 32767 >>> ii32 = np.iinfo(np.int32) >>> ii32.min -2147483648 >>> ii32.max 2147483647