Numpy - Difference Between Two Floats vs Float Type Accuracy

I looked at numpy.finfo

and did the following:

In [14]: np.finfo(np.float16).resolution
Out[14]: 0.0010004
In [16]: np.array([0., 0.0001], dtype=np.float16)
Out[16]: array([ 0.        ,  0.00010002], dtype=float16)

      

The vector seems to be capable of storing two numbers, so their difference is 10 times less than the type resolution. Did I miss something?

+3


source to share


2 answers


Floating point numbers have a fixed resolution after the start digit. What does this number tell you, when the first digit is at position 1.0, what is the resolution of the number. You can see this by trying to add smaller amounts in 1.0:

In [8]: np.float16(1) + np.float16(0.001)
Out[8]: 1.001

In [9]: np.float16(1) + np.float16(0.0001)
Out[9]: 1.0

      



This is due to the function nextafter

that gives the next representable number after the given one. Taking this distinction, we get approximately the following resolution:

In [10]: np.nextafter(np.float16(1), np.float16(np.inf)) - np.float16(1)
Out[10]: 0.00097656

      

+2


source


From what I understand, precision is the number of decimal places you can have. But since floats are stored with exponents, you can have a number less than the resolution. try it np.finfo(np.float16).tiny

, it should give you 6.1035e-05

which is less than the resolution. But the base part of this number has a resolution of ~ 0.001. Note that all limits in finfo are approximated because the binary representation is not directly correlated with the exact decimal limit.



+2


source







All Articles