Reputation: 671
For example, I understand float
is usually represented by a C double
, and integers have unlimited precision, per the docs. NumPy of course offers more specific types. Does, for example, the type of float
in NumPy in any way impact the number's precision in memory? Or are numbers for various types stored with the same precision, and the type just implements the number differently at a higher level?
Upvotes: 3
Views: 94
Reputation: 95927
Note, you can see this for yourself by modifying the underlying memory, since numpy
arrays implement the buffer protocol, we can create a memoryview
:
>>> arr32 = np.array([1,2,3], dtype=np.int32)
>>> arr64 = np.array([1,2,3], dtype=np.int64)
>>> arr32
array([1, 2, 3], dtype=int32)
>>> arr64
array([1, 2, 3])
>>> buff32 = memoryview(arr32)
>>> buff64 = memoryview(arr64)
Now, let's cast the memory to unsigned char type
>>> casted32 = buff32.cast('B')
>>> casted64 = buff64.cast('B')
>>> list(casted32)
[1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0]
>>> list(casted64)
[1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0]
Looks like it's actually different sizes in-memory. Look what I can do!:
>>> casted32[1] = 1
>>> casted64[2] = 1
>>> arr32
array([257, 2, 3], dtype=int32)
>>> arr64
array([65537, 2, 3])
Upvotes: 3
Reputation: 33522
The above comment is completely right and also mentions some form of checking.
But let's do some tiny demo where we read out the consumed memory:
import numpy as np
a = np.array([1,2,3], dtype=float)
print(a.nbytes)
b = np.array([1,2,3], dtype=np.float32)
print(b.nbytes)
c = np.array([1,2,3], dtype=np.longfloat) # this one: OS/Build-dependent
print(c.nbytes)
Out:
24
12
48
You can also read out the size-per-item:
c.itemsize
# 16
In general this is quite intuitive for most types if you know some C/C++-types, with one exception: bool will take a full byte for each entry (despite being a bit!), which is also documented in the links below!
The most import doc-entry for this is probably Data types for the type-overview and ndarray for attributes you can query.
Upvotes: 3