Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions quaddtype/numpy_quaddtype/src/scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down