NumPy numerical types

Python has three data numerical types: integer type, float type, and complex type. In scientific computation these three types are not sufficient. There is need for more numerical types. For each data type there is a matching convention function. The table below presents the NumPy numerical types (you can find it on the NumPy user guide as well), with a short description for each of them, and the convention function:

TypeDescriptionMatching convention function
boolBoolean (True or False) stored as a bit
import numpy as np
np.bool(16)

#out
True
intiPlatform integer (normally either int32 or int64)
int8Byte (-128 to 127)
import numpy as np
np.int8(16)

#out
16
int16Integer (-32768 to 32767)
import numpy as np
np.int16(16)

#out
16
int32Integer (-2 ** 31 to 2 ** 31 – 1)
import numpy as np
np.int32(16)

#out
16
int64Integer (-2 ** 63 to 2 ** 63 – 1)
import numpy as np
np.int64(16)

#out
16
uint8Unsigned integer (0 to 255)
import numpy as np
np.uint8(16)

#out
16

Example 2

import numpy as np
np.uint8(315)

#out
59
uint16Unsigned integer (0 to 65535)
import numpy as np
np.uint16(16)

#out
16

Example 2

import numpy as np
np.uint16(66546)

#out
1010
uint32Unsigned integer (0 to 2 ** 32 – 1)
import numpy as np
np.uint32(16)

#out
16

Example 2

import numpy as np
np.uint32(6654675468901)

#out
1771127397
uint64Unsigned integer (0 to 2 ** 64 – 1)
import numpy as np
np.uint64(16)

#out
16

Example 2

import numpy as np
np.uint64(6654675468901785431)

#out
6654675468901785431
float16Half precision float; sign bit, 5 bits exponent, and 10 bits mantissa
import numpy as np
np.float16(16)

#out
16

Example 2

import numpy as np
np.float16(160000)

#out
inf
float32Single precision float; sign bit, 8 bits exponent, and 23 bits mantissa
import numpy as np
np.float32(16)

#out
32

Example 2

import numpy as np
np.float32(160000000)

#out
1.6e+08
float64 or floatDouble precision float; sign bit, 11 bits exponent, and 52 bits mantissa
import numpy as np
np.float64(16)

#out
16

Example 2

import numpy as np
np.float64(160000000000000000)

#out
1.6e+17
complex64Complex number, represented by two 32 bit floats (real and imaginary components)
import numpy as np
np.complex64(16)

#out
(16+0j)
complex128 or complexComplex number, represented by two 64 bit floats (real and imaginary components)
import numpy as np
np.complex128(16)

#out
(16+0j)

Note:
You can’t change a complex number into an integer!

A function can have a data type argument (which can be optional):

import numpy as np

np.arange(5, dtype = np.uint16)

#out
array([0, 1, 2, 3, 4], dtype=uint16)

Example 2

import numpy as np

np.arange(5, dtype = np.float32)

#out
array([0., 1., 2., 3., 4.], dtype=float32)

You can convert a floating point number to a complex number. The j component is the imaginary coefficient of the complex number. You can pull out the real and imaginary pieces of a complex number using the real() and imag() functions.

Example:

import numpy as np

a = np.complex(16.0)
a

out#
(16 + 0j)

a.real

#out
16.0

a.imag

#out
0.0

Leave a Reply