Why does float have a bigger range than int32?
floating point - What range of numbers can be represented in 16-, 32-, and 64-bit IEEE-754 systems? - Stack Overflow
Range behavior for Float32
python - The real difference between float32 and float64 - Stack Overflow
float (32 Bit): -3,4E+38 to +3,4E+38
int (32 Bit): -2.147.483.648 to +2.147.483.647
Why is it like that? Shouldn't float have an even smaller range because it can also hold decimal places?
For a given IEEE-754 floating point number X, if
2^E <= abs(X) < 2^(E+1)
then the distance from X to the next largest representable floating point number (epsilon) is:
epsilon = 2^(E-52) % For a 64-bit float (double precision)
epsilon = 2^(E-23) % For a 32-bit float (single precision)
epsilon = 2^(E-10) % For a 16-bit float (half precision)
The above equations allow us to compute the solutions to the following:
If you want an accuracy of +/-0.5 (or 2^-1), the maximum size that the number can be is S1. Any larger than this and the distance between floating point numbers is greater than 0.5.
If you want an accuracy of +/-0.0005 (about 2^-11), the maximum size that the number can be is S4. Any larger than this and the distance between floating point numbers is greater than 0.0005.
- For double precision, S1 = 2^52, S4 = 2^42
- For single precision, S1 = 2^23, S4 = 2^13
- For half precision, S1 = 2^10, S4 = 1
For floating-point integers (I'll give my answer in terms of IEEE double-precision), every integer between 1 and 2^53 is exactly representable. Beyond 2^53, integers that are exactly representable are spaced apart by increasing powers of two. For example:
- Every 2nd integer between 2^53 + 2 and 2^54 can be represented exactly.
- Every 4th integer between 2^54 + 4 and 2^55 can be represented exactly.
- Every 8th integer between 2^55 + 8 and 2^56 can be represented exactly.
- Every 16th integer between 2^56 + 16 and 2^57 can be represented exactly.
- Every 32nd integer between 2^57 + 32 and 2^58 can be represented exactly.
- Every 64th integer between 2^58 + 64 and 2^59 can be represented exactly.
- Every 128th integer between 2^59 + 128 and 2^60 can be represented exactly.
- Every 256th integer between 2^60 + 256 and 2^61 can be represented exactly.
- Every 512th integer between 2^61 + 512 and 2^62 can be represented exactly. . . .
Integers that are not exactly representable are rounded to the nearest representable integer, so the worst case rounding is 1/2 the spacing between representable integers.
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])
- 16bit: 0.1235
- 32bit: 0.12345679
- 64bit: 0.12345678912121212
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
- What kind of precision does my output need?
- Is speed not an issue at all?
- what precision is needed in parts per million?
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?
