You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "emkornfield (via GitHub)" <gi...@apache.org> on 2023/05/15 08:20:17 UTC

[GitHub] [arrow] emkornfield commented on issue #35576: Unexpected float32 to decimal128 cast result

emkornfield commented on issue #35576:
URL: https://github.com/apache/arrow/issues/35576#issuecomment-1547404763

   the first part of the error is down-casting from float64 (python default representation to)->float32
   ```
   a = pa.array([545803904.0], type=pa.float32())
   a
   <pyarrow.lib.FloatArray object at 0x3ebb737bb400>
   [
     545803900
   ]
   ```
   Same happens with numpy:
   ```
   numpy.float32(545803904.0)
   545803900.0
   ```
   
   The second part of the error I think is likely due to implementation which looks like we somehow might do an extra cast through an intermediate value:
   
   ```
   a = pa.array([545803900.0], type=pa.float64())
   print(a.cast(pa.decimal128(38, 18)))
   print(a.cast(pa.float32()).cast(pa.decimal128(38, 18)))
   ```
   gives:
   [
     545803899.999999976169013248
   ]
   [
     545803886.966396699654750208
   ]
   
   I think the second source of error might be: https://github.com/apache/arrow/blob/cd6e2a4d2b9373b942da18b4cc82cb41431764d9/cpp/src/arrow/util/decimal.cc#L158
   since this looks like it it is done in float space (instead of casting to double) which potentially causes further loss of precision.
   
   
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org