Reputation: 705
I want to understand the actual difference between float16
and float32
in terms of the result precision. For instance, NumPy allows you to choose the range of the datatype you want (np.float16, np.float32, np.float64)
. My concern is that if I decide to go with float16 to reserve memory and avoid possible overflow, would that create a loss of the final results comparing with float32 for instance?
Upvotes: 61
Views: 128393
Reputation: 47
float32 is less accurate but faster than float64, and float64 is more accurate than float32 but consumes more memory. If accuracy is more important than speed, you can use float64. and if speed is more important than accuracy, you can use float32.
Upvotes: 3
Reputation: 1600
a = np.array([0.123456789121212,2,3], dtype=np.float16)
print("16bit: ", a[0])
a = np.array([0.123456789121212,2,3], dtype=np.float32)
print("32bit: ", a[0])
b = np.array([0.123456789121212121212,2,3], dtype=np.float64)
print("64bit: ", b[0])
Upvotes: 88
Reputation: 813
float32 is a 32 bit number - float64 uses 64 bits.
That means that float64’s take up twice as much memory - and doing operations on them may be a lot slower in some machine architectures.
However, float64’s can represent numbers much more accurately than 32 bit floats.
They also allow much larger numbers to be stored.
For your Python-Numpy project I'm sure you know the input variables and their nature.
To make a decision we as programmers need to ask ourselves
A naive example would be if I store weather data of my city as [12.3, 14.5, 11.1, 9.9, 12.2, 8.2]
Next day Predicted Output could be of 11.5 or 11.5164374
do your think storing float 32 or float 64 would be necessary?
Upvotes: 45