dinse commented on issue #36753:
URL: https://github.com/apache/arrow/issues/36753#issuecomment-2945906520

   I have a change that implements this but I have a question about desired 
behavior. For values like 1, 1.25, etc there is no loss of precision. Other 
values like 1.2 or 12345 are not stored exactly as typed into np.float16. Right 
now printing them shows the slight change in value, but slicing said array and 
printing the HalfFloatScalar shows the original value. Is that alright?
   
   ```
   >>> import pyarrow as pa
   >>> import numpy as np
   >>> 
   >>> x = [0, 1, 1.2, 1.25, 1.5, 12345]
   >>> test = pa.array([np.float16(xi) for xi in x], type=pa.float16())
   >>> test
   <pyarrow.lib.HalfFloatArray object at 0x76f1a490c100>
   [
     0,
     1,
     1.2001953125,
     1.25,
     1.5,
     12344
   ]
   >>> test.view(pa.uint16())
   <pyarrow.lib.UInt16Array object at 0x76f1a48cfa00>
   [
     0,
     15360,
     15565,
     15616,
     15872,
     29191
   ]
   >>> test[2]
   <pyarrow.HalfFloatScalar: np.float16(1.2)>
    
   >>> pa.array([15565], pa.uint16())
   <pyarrow.lib.UInt16Array object at 0x76f1a490c280>
   [
     15565
   ]
   >>> pa.array([15565], pa.uint16()).view(pa.float16())
   <pyarrow.lib.HalfFloatArray object at 0x76f1a490c220>
   [
     1.2001953125
   ]
   >>> np.float16(1.2).view(np.uint16())
   np.uint16(15565)
    
   >>> x
   [0, 1, 1.2, 1.25, 1.5, 12345]
   >>> x2 = [0, .97, 1.2, 1.25, 1.5, 12344]
   >>> test2 = pa.array([np.float16(xi) for xi in x2], type=pa.float16())
   >>> 
   >>> test.diff(test2)
   '\n@@ -1, +1 @@\n-1\n+0.970215\n'
   >>> 
   >>> a = pa.array([np.float16(5.5), np.float16(5.6)], type=pa.float16())
   >>> b = pa.array([np.float16(5.5), np.float16(5.7)], type=pa.float16())
   >>> 
   >>> a.diff(b)
   '\n@@ -1, +1 @@\n-5.60156\n+5.69922\n'```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to