pitrou commented on code in PR #46618:
URL: https://github.com/apache/arrow/pull/46618#discussion_r2120327400
##########
python/pyarrow/src/arrow/python/python_to_arrow.cc:
##########
@@ -226,9 +226,16 @@ class PyValue {
}
static Result<uint16_t> Convert(const HalfFloatType*, const O&, I obj) {
- uint16_t value;
- RETURN_NOT_OK(PyFloat_AsHalf(obj, &value));
- return value;
+ if (internal::PyFloatScalar_Check(obj)) {
+ return PyFloat_AsHalf(obj);
+ } else if (internal::PyIntScalar_Check(obj)) {
+ float float_val{};
+ RETURN_NOT_OK(internal::IntegerScalarToFloat32Safe(obj, &float_val));
Review Comment:
Similarly, let's use `IntegerScalarToDoubleSafe`?
##########
python/pyarrow/src/arrow/python/helpers.cc:
##########
@@ -73,21 +75,22 @@ std::shared_ptr<DataType> GetPrimitiveType(Type::type type)
{
}
}
-PyObject* PyHalf_FromHalf(npy_half value) {
- PyObject* result = PyArrayScalar_New(Half);
- if (result != NULL) {
- PyArrayScalar_ASSIGN(result, Half, value);
- }
- return result;
+PyObject* PyFloat_FromHalf(uint16_t value) {
+ // Convert the uint16_t Float16 value to a PyFloat object
+ arrow::util::Float16 half_val = arrow::util::Float16::FromBits(value);
+ return PyFloat_FromDouble(half_val.ToDouble());
}
-Status PyFloat_AsHalf(PyObject* obj, npy_half* out) {
- if (PyArray_IsScalar(obj, Half)) {
- *out = PyArrayScalar_VAL(obj, Half);
- return Status::OK();
+Result<uint16_t> PyFloat_AsHalf(PyObject* obj) {
+ if (PyFloat_Check(obj)) {
+ float float_val = static_cast<float>(PyFloat_AsDouble(obj));
Review Comment:
Please let's keep this `double` to potential avoid double-rounding issues
(that might not be a problem here, I'm not a specialist).
(in case you don't know, in C/C++ `float` is a single-precision
floating-point, while Python's `float` is a double-precision floating-point
like C/C++'s `double`)
--
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]