Skip to content

Commit ed64045

Browse files
authored
Merge pull request #202 from SwayamInSync/bool
2 parents 7e1242a + c0b7bf6 commit ed64045

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,24 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
8787
}
8888
return self;
8989
}
90+
// Try as boolean
91+
else if (PyArray_IsScalar(value, Bool)) {
92+
PyObject *py_int = PyNumber_Long(value);
93+
if (py_int == NULL) {
94+
Py_DECREF(self);
95+
return NULL;
96+
}
97+
long long lval = PyLong_AsLongLong(py_int);
98+
Py_DECREF(py_int);
99+
100+
if (backend == BACKEND_SLEEF) {
101+
self->value.sleef_value = Sleef_cast_from_int64q1(lval);
102+
}
103+
else {
104+
self->value.longdouble_value = (long double)lval;
105+
}
106+
return self;
107+
}
90108
// For other scalar types, fall through to error handling
91109
Py_DECREF(self);
92110
}

quaddtype/tests/test_quaddtype.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ def test_create_from_numpy_float_scalars(self):
134134
assert isinstance(result, QuadPrecision)
135135
assert abs(float(result) - 1.5) < 1e-3
136136

137+
def test_create_from_numpy_bool_scalars(self):
138+
"""Test that QuadPrecision can create scalars from numpy boolean types."""
139+
# Test np.bool_(True) converts to 1.0
140+
result = QuadPrecision(np.bool_(True))
141+
assert isinstance(result, QuadPrecision)
142+
assert float(result) == 1.0
143+
144+
# Test np.bool_(False) converts to 0.0
145+
result = QuadPrecision(np.bool_(False))
146+
assert isinstance(result, QuadPrecision)
147+
assert float(result) == 0.0
148+
137149
def test_create_from_zero_dimensional_array(self):
138150
"""Test that QuadPrecision can create from 0-d numpy arrays."""
139151
# 0-d array from scalar

0 commit comments

Comments
 (0)