Skip to content

Commit a481648

Browse files
committed
fixing quad precsion printing issue
1 parent 3559232 commit a481648

File tree

3 files changed

+45
-39
lines changed

3 files changed

+45
-39
lines changed

quaddtype2/quaddtype/src/casts.cpp

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#define NO_IMPORT_ARRAY
66
#define NO_IMPORT_UFUNC
77

8-
#include<Python.h>
9-
#include<sleef.h>
10-
#include<sleefquad.h>
11-
#include<vector>
8+
#include <Python.h>
9+
#include <sleef.h>
10+
#include <sleefquad.h>
11+
#include <vector>
1212

1313
#include "numpy/arrayobject.h"
1414
#include "numpy/ndarraytypes.h"
@@ -18,12 +18,11 @@
1818
#include "casts.h"
1919
#include "dtype.h"
2020

21-
22-
static NPY_CASTING quad_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self),
23-
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
24-
QuadPrecDTypeObject *given_descrs[2],
25-
QuadPrecDTypeObject *loop_descrs[2],
26-
npy_intp *view_offset)
21+
static NPY_CASTING
22+
quad_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self),
23+
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
24+
QuadPrecDTypeObject *given_descrs[2],
25+
QuadPrecDTypeObject *loop_descrs[2], npy_intp *view_offset)
2726
{
2827
Py_INCREF(given_descrs[0]);
2928
loop_descrs[0] = given_descrs[0];
@@ -41,10 +40,10 @@ static NPY_CASTING quad_to_quad_resolve_descriptors(PyObject *NPY_UNUSED(self),
4140
return NPY_SAME_KIND_CASTING;
4241
}
4342

44-
static int quad_to_quad_strided_loop(
45-
PyArrayMethod_Context *context,
46-
char *const data[], npy_intp const dimensions[],
47-
npy_intp const strides[], void *NPY_UNUSED(auxdata))
43+
static int
44+
quad_to_quad_strided_loop(PyArrayMethod_Context *context, char *const data[],
45+
npy_intp const dimensions[], npy_intp const strides[],
46+
void *NPY_UNUSED(auxdata))
4847
{
4948
npy_intp N = dimensions[0];
5049
char *in_ptr = data[0];
@@ -62,45 +61,43 @@ static int quad_to_quad_strided_loop(
6261
return 0;
6362
}
6463

65-
static std::vector<PyArrayMethod_Spec *>specs;
66-
64+
static std::vector<PyArrayMethod_Spec *> specs;
6765

68-
PyArrayMethod_Spec ** init_casts_internal(void)
66+
PyArrayMethod_Spec **
67+
init_casts_internal(void)
6968
{
7069
PyArray_DTypeMeta **quad2quad_dtypes = new PyArray_DTypeMeta *[2]{nullptr, nullptr};
7170

72-
specs.push_back(new PyArrayMethod_Spec {
73-
.name = "cast_QuadPrec_to_QuadPrec",
74-
.nin = 1,
75-
.nout = 1,
76-
.casting = NPY_SAME_KIND_CASTING,
77-
.flags = NPY_METH_SUPPORTS_UNALIGNED,
78-
.dtypes = quad2quad_dtypes,
79-
.slots = new PyType_Slot[3]{
80-
{NPY_METH_resolve_descriptors, (void *)&quad_to_quad_resolve_descriptors},
81-
{NPY_METH_strided_loop, (void *)&quad_to_quad_strided_loop},
82-
{0, NULL}
83-
}});
84-
85-
71+
specs.push_back(new PyArrayMethod_Spec{
72+
.name = "cast_QuadPrec_to_QuadPrec",
73+
.nin = 1,
74+
.nout = 1,
75+
.casting = NPY_SAME_KIND_CASTING,
76+
.flags = NPY_METH_SUPPORTS_UNALIGNED,
77+
.dtypes = quad2quad_dtypes,
78+
.slots = new PyType_Slot[4]{
79+
{NPY_METH_resolve_descriptors, (void *)&quad_to_quad_resolve_descriptors},
80+
{NPY_METH_strided_loop, (void *)&quad_to_quad_strided_loop},
81+
{NPY_METH_unaligned_strided_loop, (void *)&quad_to_quad_strided_loop},
82+
{0, NULL}}});
83+
8684
return specs.data();
8785
}
8886

89-
PyArrayMethod_Spec ** init_casts(void)
87+
PyArrayMethod_Spec **
88+
init_casts(void)
9089
{
91-
try
92-
{
90+
try {
9391
return init_casts_internal();
9492
}
95-
catch(const std::exception& e)
96-
{
93+
catch (const std::exception &e) {
9794
PyErr_NoMemory();
9895
return nullptr;
9996
}
100-
10197
}
10298

103-
void free_casts(void)
99+
void
100+
free_casts(void)
104101
{
105102
for (auto cast : specs) {
106103
if (cast == nullptr) {

quaddtype2/quaddtype/src/scalar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ QuadPrecision_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
7171
static PyObject * QuadPrecision_str(QuadPrecisionObject * self)
7272
{
7373
char buffer[128];
74-
Sleef_snprintf(buffer, sizeof(buffer), "%.*Qe", self->quad.value);
74+
Sleef_snprintf(buffer, sizeof(buffer), "%.*Qe", SLEEF_QUAD_DIG, self->quad.value);
7575
return PyUnicode_FromString(buffer);
7676
}
7777

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pytest
2+
3+
import numpy as np
4+
from quaddtype import QuadPrecDType, QuadPrecision
5+
6+
7+
def test():
8+
a = QuadPrecision("1.63")
9+
assert f"{np.array([a], dtype=QuadPrecDType).dtype}" == "QuadPrecDType()"

0 commit comments

Comments
 (0)