@@ -76,39 +76,42 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
7676 self -> value .longdouble_value = (long double )val ;
7777 }
7878 }
79- else if (Py_TYPE (value ) == & QuadPrecision_Type )
80- {
81- // todo: not working for ld backend, getting garbage value not sure why?
79+ else if (Py_TYPE (value ) == & QuadPrecision_Type ) {
80+ Py_DECREF (self ); // discard the default one
8281 QuadPrecisionObject * quad_obj = (QuadPrecisionObject * )value ;
83- // printf("%d %d\n", quad_obj->backend, backend);
84- // printf("%Lf\n", quad_obj->value.longdouble_value);
82+
83+ // create a new one with the same backend
84+ QuadPrecisionObject * self = QuadPrecision_raw_new (quad_obj -> backend );
8585 if (quad_obj -> backend == BACKEND_SLEEF ) {
8686 self -> value .sleef_value = quad_obj -> value .sleef_value ;
8787 }
8888 else {
8989 self -> value .longdouble_value = quad_obj -> value .longdouble_value ;
9090 }
91+
92+ return self ;
9193 }
9294 else {
9395 PyObject * type_str = PyObject_Str ((PyObject * )Py_TYPE (value ));
9496 if (type_str != NULL ) {
9597 const char * type_cstr = PyUnicode_AsUTF8 (type_str );
9698 if (type_cstr != NULL ) {
97- PyErr_Format (
98- PyExc_TypeError ,
99- "QuadPrecision value must be a float, int or string, but got %s instead" ,
100- type_cstr );
99+ PyErr_Format (PyExc_TypeError ,
100+ "QuadPrecision value must be a quad, float, int or string, but got %s "
101+ " instead" ,
102+ type_cstr );
101103 }
102104 else {
103- PyErr_SetString (PyExc_TypeError ,
104- "QuadPrecision value must be a float, int or string, but got an "
105- "unknown type instead" );
105+ PyErr_SetString (
106+ PyExc_TypeError ,
107+ "QuadPrecision value must be a quad, float, int or string, but got an "
108+ "unknown type instead" );
106109 }
107110 Py_DECREF (type_str );
108111 }
109112 else {
110113 PyErr_SetString (PyExc_TypeError ,
111- "QuadPrecision value must be a float, int or string, but got an "
114+ "QuadPrecision value must be a quad, float, int or string, but got an "
112115 "unknown type instead" );
113116 }
114117 Py_DECREF (self );
@@ -267,9 +270,44 @@ PyTypeObject QuadPrecision_Type = {
267270 .tp_getset = QuadPrecision_getset ,
268271};
269272
273+ QuadPrecisionObject * initialize_constants (const Sleef_quad value , QuadBackendType backend )
274+ {
275+ QuadPrecisionObject * obj = QuadPrecision_raw_new (backend );
276+ if (backend == BACKEND_SLEEF ) {
277+ obj -> value .sleef_value = value ;
278+ }
279+ else {
280+ obj -> value .longdouble_value = Sleef_cast_to_doubleq1 (value );
281+ }
282+
283+ return obj ;
284+ }
285+
270286int
271287init_quadprecision_scalar (void )
272288{
273- // QuadPrecision_Type.tp_base = &PyFloatingArrType_Type;
289+ QuadPrecisionObject * QuadPrecision_pi = initialize_constants (SLEEF_M_PIq , BACKEND_SLEEF );
290+ QuadPrecisionObject * QuadPrecision_e = initialize_constants (SLEEF_M_Eq , BACKEND_SLEEF );
291+ QuadPrecisionObject * QuadPrecision_log2e = initialize_constants (SLEEF_M_LOG2Eq , BACKEND_SLEEF );
292+ QuadPrecisionObject * QuadPrecision_log10e = initialize_constants (SLEEF_M_LOG10Eq , BACKEND_SLEEF );
293+ QuadPrecisionObject * QuadPrecision_ln2 = initialize_constants (SLEEF_M_LN2q , BACKEND_SLEEF );
294+ QuadPrecisionObject * QuadPrecision_ln10 = initialize_constants (SLEEF_M_LN10q , BACKEND_SLEEF );
295+ QuadPrecisionObject * QuadPrecision_sqrt2 = initialize_constants (SLEEF_M_SQRT2q , BACKEND_SLEEF );
296+ QuadPrecisionObject * QuadPrecision_sqrt3 = initialize_constants (SLEEF_M_SQRT3q , BACKEND_SLEEF );
297+ QuadPrecisionObject * QuadPrecision_egamma = initialize_constants (SLEEF_M_EGAMMAq , BACKEND_SLEEF );
298+ QuadPrecisionObject * QuadPrecision_phi = initialize_constants (SLEEF_M_PHIq , BACKEND_SLEEF );
299+ QuadPrecisionObject * QuadPrecision_quad_max = initialize_constants (SLEEF_QUAD_MAX , BACKEND_SLEEF );
300+ QuadPrecisionObject * QuadPrecision_quad_min = initialize_constants (SLEEF_QUAD_MIN , BACKEND_SLEEF );
301+ QuadPrecisionObject * QuadPrecision_quad_epsilon = initialize_constants (SLEEF_QUAD_EPSILON , BACKEND_SLEEF );
302+ QuadPrecisionObject * QuadPrecision_quad_denorm_min = initialize_constants (SLEEF_QUAD_DENORM_MIN , BACKEND_SLEEF );
303+
304+ if (!QuadPrecision_pi || !QuadPrecision_e || !QuadPrecision_log2e || !QuadPrecision_log10e ||
305+ !QuadPrecision_ln2 || !QuadPrecision_ln10 || !QuadPrecision_sqrt2 || !QuadPrecision_sqrt3 ||
306+ !QuadPrecision_egamma || !QuadPrecision_phi || !QuadPrecision_quad_max || !QuadPrecision_quad_min ||
307+ !QuadPrecision_quad_epsilon || !QuadPrecision_quad_denorm_min ) {
308+ PyErr_SetString (PyExc_RuntimeError , "Failed to initialize QuadPrecision constants" );
309+ return -1 ;
310+ }
311+
274312 return PyType_Ready (& QuadPrecision_Type );
275313}
0 commit comments