diff --git a/quaddtype/numpy_quaddtype/src/scalar.c b/quaddtype/numpy_quaddtype/src/scalar.c index 7e3e33de..421cde9f 100644 --- a/quaddtype/numpy_quaddtype/src/scalar.c +++ b/quaddtype/numpy_quaddtype/src/scalar.c @@ -87,6 +87,24 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend) } return self; } + // Try as boolean + else if (PyArray_IsScalar(value, Bool)) { + PyObject *py_int = PyNumber_Long(value); + if (py_int == NULL) { + Py_DECREF(self); + return NULL; + } + long long lval = PyLong_AsLongLong(py_int); + Py_DECREF(py_int); + + if (backend == BACKEND_SLEEF) { + self->value.sleef_value = Sleef_cast_from_int64q1(lval); + } + else { + self->value.longdouble_value = (long double)lval; + } + return self; + } // For other scalar types, fall through to error handling Py_DECREF(self); } diff --git a/quaddtype/tests/test_quaddtype.py b/quaddtype/tests/test_quaddtype.py index 0ceb56c6..b3dafc00 100644 --- a/quaddtype/tests/test_quaddtype.py +++ b/quaddtype/tests/test_quaddtype.py @@ -134,6 +134,18 @@ def test_create_from_numpy_float_scalars(self): assert isinstance(result, QuadPrecision) assert abs(float(result) - 1.5) < 1e-3 + def test_create_from_numpy_bool_scalars(self): + """Test that QuadPrecision can create scalars from numpy boolean types.""" + # Test np.bool_(True) converts to 1.0 + result = QuadPrecision(np.bool_(True)) + assert isinstance(result, QuadPrecision) + assert float(result) == 1.0 + + # Test np.bool_(False) converts to 0.0 + result = QuadPrecision(np.bool_(False)) + assert isinstance(result, QuadPrecision) + assert float(result) == 0.0 + def test_create_from_zero_dimensional_array(self): """Test that QuadPrecision can create from 0-d numpy arrays.""" # 0-d array from scalar