-1

Why when I perform

g = np.float16([4.4,4.6])

I get something different

g  = [4.3984..., 4.6015...]. 

Will it somehow affect calculations with the matrix? Can I change this behavior without changing dtype? Thanks!

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 2
    You chose to use a more limited precision than usual floats, so you get the closest value that can be represented by you data type. And so yes, this will affect calculations. – Thierry Lathuille Nov 01 '22 at 11:26
  • 1
    See also https://stackoverflow.com/questions/588004/is-floating-point-math-broken – Thierry Lathuille Nov 01 '22 at 11:26
  • 1
    I can't reproduce this. `>>> np.float16([4.4,4.6])` returns `array([4.4, 4.6], dtype=float16)`. I had to use `float(g[0])` to get `4.3984375`. – Panagiotis Kanavos Nov 01 '22 at 11:26
  • 1
    In any case, floating point numbers can't represent every rational number. `4.4` can't be represented exactly. Neither can `0.3`. Numpy's `float16` does extra work to handle this when printing. Internally though, the number is still `4.3984375`. If you try `np.float16(4.3984375)` you'll get `4.4` – Panagiotis Kanavos Nov 01 '22 at 11:31
  • `np.float16(4.3984375)==4.3984375 ` is True, while `np.float16(4.3984375)==4.4` is False because `float16` is storing `4.3984375` internally – Panagiotis Kanavos Nov 01 '22 at 11:32

1 Answers1

0

First of all, you have to cast float16 back to a float to get that result. You aren't getting a different result though, that's the actual value.

You can't represent 4.4 or 0.3 and many other rational numbers with a floating point number, no matter the precision. Python itself doesn't store 4.4 :

>>> f"{4.4}" 
'4.4'
>>> f"{4.4:.15f}" 
'4.400000000000000'
>>> f"{4.4:.20f}" 
'4.40000000000000035527'

The reason that 4.4 prints as 4.4 is that 15 is the default print precision according to the IEEE 754 floating point number standard.

Using a smaller type increases the error :

>>> f"{np.float64(4.4):.20f}" 
'4.40000000000000035527'
>>> f"{np.float32(4.4):.20f}" 
'4.40000009536743164062'
>>> f"{np.float16(4.4):.20f}" 
'4.39843750000000000000'

NumPy

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236