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
8 changes: 8 additions & 0 deletions quaddtype/numpy_quaddtype/src/scalar.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,15 @@ static PyMethodDef QuadPrecision_methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};

static PyObject *
QuadPrecision_get_dtype(QuadPrecisionObject *self, void *NPY_UNUSED(closure))
{
QuadPrecDTypeObject *dtype = new_quaddtype_instance(self->backend);
return (PyObject *)dtype;
}

static PyGetSetDef QuadPrecision_getset[] = {
{"dtype", (getter)QuadPrecision_get_dtype, NULL, "Data type descriptor for this scalar", NULL},
{"real", (getter)QuadPrecision_get_real, NULL, "Real part of the scalar", NULL},
{"imag", (getter)QuadPrecision_get_imag, NULL, "Imaginary part of the scalar (always 0 for real types)", NULL},
{NULL} /* Sentinel */
Expand Down
17 changes: 16 additions & 1 deletion quaddtype/tests/test_quaddtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -4292,4 +4292,19 @@ def test_as_integer_ratio_compatibility_with_float(self, value):
# The ratios should be equal
quad_ratio = quad_num / quad_denom
float_ratio = float_num / float_denom
assert abs(quad_ratio - float_ratio) < 1e-15
assert abs(quad_ratio - float_ratio) < 1e-15

def test_quadprecision_scalar_dtype_expose():
quad_ld = QuadPrecision("1e100", backend="longdouble")
quad_sleef = QuadPrecision("1e100", backend="sleef")
assert quad_ld.dtype == QuadPrecDType(backend='longdouble')
assert np.dtype(quad_ld) == QuadPrecDType(backend='longdouble')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course scalar.dtype should work and I guess since you are parametric you have to make it work.

Butt the protocol is the exact opposite of this assert. It specifically wants to disable this in the future because it makes no sense to say np.ones(..., dtype=scalar), the user should write dtype=scalar.dtype.

So in other words: The attribute is right, the __numpy_dtype__ protocol is just unrelated.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I see, so we don't have to implement __numpy_dtype__
I misinterpret that, I thought in future had to expose both .dtype and __numpy_dtype__

In current scenarios atleast for testing, I just set np.longdouble = QuadPrecision #(scalar) as we have different names for scalar and dtype and this works correctly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What we could try to have is a function to map from the scalar type to the DType class cleanly, I guess (this exists in C, but not Python).
I used to want np.dtype[scalar_type] for this, but while what typing does is conceptually the same, it's not compatible :(.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can always add np.dtype.from_scalar.


#todo: Uncomment them when 232 is merged
# assert quad_ld.dtype.backend == 1
# assert np.dtype(quad_ld).backend == 1

assert quad_sleef.dtype == QuadPrecDType(backend='sleef')
assert np.dtype(quad_sleef) == QuadPrecDType(backend='sleef')
# assert quad_sleef.dtype.backend == 0
# assert np.dtype(quad_sleef).backend == 0