According to the documentation, np.float16 uses 5 bits exponents and 10 bits mantissa.
Since the exponent 11111 is reserved for inf/Nan, the maximal exponent is 11110;taking into account the 0 offset, this means an exponent of value 30 - 15 = 15.
The largest number corresponds to a mantissa of (1.1111111111)_2 = 1 + 1023/1024
Hence the largest number should be 2**15 * (1 + 1023/1024) = 65504.0
However np.finfo(np.float16).max (quoting the doc: "The largest representable number.") is equal to 65500. What explains the difference ?
Edit: Note that 65504.0 can be represented exactly as a float16 (bit sign=0, exponent=11110, mantissa=1111111111), it is not an approximation and thus not linked to usual "0.1 cannot be represented exactly as a float" questions.
You know how a 64-bit 0.3 shows up as 0.3 instead of its actual value, 0.299999999999999988897769753748434595763683319091796875?
It's a similar story here. numpy.finfo(numpy.float16).max is a float16, and those have very little precision. The exact mathematical value of the float16 is 65504, but a float16 doesn't have 5 decimal digits of precision, and by default, NumPy doesn't want to show you false precision.
It prints 65500.0, because '65500.0' is the shortest decimal representation (in terms of significant digits) such that converting the string back to float16 reproduces the original number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With