Skip to content

Commit 4c32b91

Browse files
committed
defining LDBL_DECIMAL_DIG for MSVC
1 parent 9cd5674 commit 4c32b91

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

src/csrc/scalar.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <sleef.h>
33
#include <sleefquad.h>
44
#include <stdlib.h>
5-
#include <float.h>
65

76
#define PY_ARRAY_UNIQUE_SYMBOL QuadPrecType_ARRAY_API
87
#define NPY_NO_DEPRECATED_API NPY_2_0_API_VERSION
@@ -332,13 +331,15 @@ QuadPrecision_str_dragon4(QuadPrecisionObject *self)
332331

333332
static PyObject *
334333
QuadPrecision_str(QuadPrecisionObject *self)
334+
// This is just define here for debugging, we actually use QuadPrecision_str_dragon4 for __str__ method.
335335
{
336336
char buffer[128];
337337
if (self->backend == BACKEND_SLEEF) {
338338
Sleef_snprintf(buffer, sizeof(buffer), "%.*Qe", SLEEF_QUAD_DIG, self->value.sleef_value);
339339
}
340340
else {
341-
snprintf(buffer, sizeof(buffer), "%.35Le", self->value.longdouble_value);
341+
snprintf(buffer, sizeof(buffer), "%.*Le", LDBL_DECIMAL_DIG - 1,
342+
self->value.longdouble_value);
342343
}
343344
return PyUnicode_FromString(buffer);
344345
}

src/include/constants.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ extern "C" {
99
#include <sleefquad.h>
1010
#include <stdint.h>
1111
#include <string.h>
12+
#include <float.h>
13+
14+
/* LDBL_DECIMAL_DIG: minimum decimal digits needed for a lossless
15+
* long-double → string → long-double round-trip. Standard C11 constant in
16+
* <float.h>, but MSVC omits it. On MSVC long double is the same width as
17+
* double, so DBL_DECIMAL_DIG (17) is the exact correct fallback. */
18+
#ifndef LDBL_DECIMAL_DIG
19+
# define LDBL_DECIMAL_DIG DBL_DECIMAL_DIG
20+
#endif
1221

1322
// Quad precision constants using sleef_q macro
1423
#define QUAD_PRECISION_ZERO sleef_q(+0x0000000000000LL, 0x0000000000000000ULL, -16383)

0 commit comments

Comments
 (0)