Skip to content

Commit 082b64b

Browse files
committed
exposing constants
1 parent 0052e16 commit 082b64b

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

quaddtype/numpy_quaddtype/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
)
66

77
__all__ = ['QuadPrecision', 'QuadPrecDType', 'SleefQuadPrecision', 'LongDoubleQuadPrecision',
8-
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128']
8+
'SleefQuadPrecDType', 'LongDoubleQuadPrecDType', 'is_longdouble_128', 'pi', 'e']
9+
10+
11+
pi = QuadPrecision.pi
12+
e = QuadPrecision.e
913

1014

1115
def SleefQuadPrecision(value):

quaddtype/numpy_quaddtype/src/scalar.c

+22
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,27 @@ QuadPrecision_dealloc(QuadPrecisionObject *self)
233233
Py_TYPE(self)->tp_free((PyObject *)self);
234234
}
235235

236+
PyObject* QuadPrecision_get_pi(PyObject* self, void* closure) {
237+
QuadPrecisionObject* new = QuadPrecision_raw_new(BACKEND_SLEEF);
238+
if (new == NULL) return NULL;
239+
new->value.sleef_value = SLEEF_M_PIq;
240+
return (PyObject*)new;
241+
}
242+
243+
PyObject* QuadPrecision_get_e(PyObject* self, void* closure) {
244+
QuadPrecisionObject* new = QuadPrecision_raw_new(BACKEND_SLEEF);
245+
if (new == NULL) return NULL;
246+
new->value.sleef_value = SLEEF_M_Eq;
247+
return (PyObject*)new;
248+
}
249+
250+
// Add this to the existing QuadPrecision_Type definition
251+
static PyGetSetDef QuadPrecision_getset[] = {
252+
{"pi", (getter)QuadPrecision_get_pi, NULL, "Pi constant", NULL},
253+
{"e", (getter)QuadPrecision_get_e, NULL, "Euler's number", NULL},
254+
{NULL} /* Sentinel */
255+
};
256+
236257
PyTypeObject QuadPrecision_Type = {
237258
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "numpy_quaddtype.QuadPrecision",
238259
.tp_basicsize = sizeof(QuadPrecisionObject),
@@ -243,6 +264,7 @@ PyTypeObject QuadPrecision_Type = {
243264
.tp_str = (reprfunc)QuadPrecision_str_dragon4,
244265
.tp_as_number = &quad_as_scalar,
245266
.tp_richcompare = (richcmpfunc)quad_richcompare,
267+
.tp_getset = QuadPrecision_getset,
246268
};
247269

248270
int

quaddtype/numpy_quaddtype/src/scalar.h

+3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend);
3131
int
3232
init_quadprecision_scalar(void);
3333

34+
PyObject* QuadPrecision_get_pi(PyObject* self, void* closure);
35+
PyObject* QuadPrecision_get_e(PyObject* self, void* closure);
36+
3437
#define PyArray_IsScalar(obj, QuadPrecDType) PyObject_TypeCheck(obj, &QuadPrecision_Type)
3538
#define PyArrayScalar_VAL(obj, QuadPrecDType) (((QuadPrecisionObject *)obj)->value)
3639

0 commit comments

Comments
 (0)