@@ -76,39 +76,42 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
76
76
self -> value .longdouble_value = (long double )val ;
77
77
}
78
78
}
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
82
81
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 );
85
85
if (quad_obj -> backend == BACKEND_SLEEF ) {
86
86
self -> value .sleef_value = quad_obj -> value .sleef_value ;
87
87
}
88
88
else {
89
89
self -> value .longdouble_value = quad_obj -> value .longdouble_value ;
90
90
}
91
+
92
+ return self ;
91
93
}
92
94
else {
93
95
PyObject * type_str = PyObject_Str ((PyObject * )Py_TYPE (value ));
94
96
if (type_str != NULL ) {
95
97
const char * type_cstr = PyUnicode_AsUTF8 (type_str );
96
98
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 );
101
103
}
102
104
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" );
106
109
}
107
110
Py_DECREF (type_str );
108
111
}
109
112
else {
110
113
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 "
112
115
"unknown type instead" );
113
116
}
114
117
Py_DECREF (self );
@@ -267,9 +270,44 @@ PyTypeObject QuadPrecision_Type = {
267
270
.tp_getset = QuadPrecision_getset ,
268
271
};
269
272
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
+
270
286
int
271
287
init_quadprecision_scalar (void )
272
288
{
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
+
274
312
return PyType_Ready (& QuadPrecision_Type );
275
313
}
0 commit comments