Skip to content

Commit ef07ab2

Browse files
committed
-nan supported
1 parent 6613125 commit ef07ab2

4 files changed

Lines changed: 21 additions & 15 deletions

File tree

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ quad_to_quad_same_value_check(const quad_value *in_val, QuadBackendType backend_
8585
// Convert longdouble back to SLEEF for comparison
8686
long double ld = out_val->longdouble_value;
8787
if (std::isnan(ld)) {
88-
roundtrip.sleef_value = QUAD_PRECISION_NAN;
88+
// Preserve sign of NaN
89+
roundtrip.sleef_value = (!ld_signbit(&ld)) ? QUAD_PRECISION_NAN : QUAD_PRECISION_NEG_NAN;
8990
}
9091
else if (std::isinf(ld)) {
9192
roundtrip.sleef_value = (ld > 0) ? QUAD_PRECISION_INF : QUAD_PRECISION_NINF;
@@ -162,7 +163,7 @@ quad_to_quad_strided_loop(PyArrayMethod_Context *context, char *const data[],
162163
{
163164
long double ld = in_val.longdouble_value;
164165
if (std::isnan(ld)) {
165-
out_val.sleef_value = QUAD_PRECISION_NAN;
166+
out_val.sleef_value = (!ld_signbit(&ld)) ? QUAD_PRECISION_NAN : QUAD_PRECISION_NEG_NAN;
166167
}
167168
else if (std::isinf(ld)) {
168169
out_val.sleef_value = (ld > 0) ? QUAD_PRECISION_INF : QUAD_PRECISION_NINF;
@@ -441,7 +442,6 @@ quad_to_string_same_value_check(const quad_value *in_val, const char *str_buf, n
441442

442443
// Compare original and roundtripped values along with signbit
443444
if (backend == BACKEND_SLEEF) {
444-
// NaN == NaN for same_value purposes
445445
bool is_sign_preserved = (quad_signbit(&in_val->sleef_value) == quad_signbit(&roundtrip.sleef_value));
446446
if (Sleef_iunordq1(in_val->sleef_value, roundtrip.sleef_value) && is_sign_preserved)
447447
return 1;

quaddtype/numpy_quaddtype/src/constants.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern "C" {
1717
#define QUAD_PRECISION_INF sleef_q(+0x1000000000000LL, 0x0000000000000000ULL, 16384)
1818
#define QUAD_PRECISION_NINF sleef_q(-0x1000000000000LL, 0x0000000000000000ULL, 16384)
1919
#define QUAD_PRECISION_NAN sleef_q(+0x1ffffffffffffLL, 0xffffffffffffffffULL, 16384)
20+
#define QUAD_PRECISION_NEG_NAN sleef_q(-0x1ffffffffffffLL, 0xffffffffffffffffULL, 16384)
2021

2122
// Additional constants
2223
#define QUAD_PRECISION_MAX_FINITE SLEEF_QUAD_MAX

quaddtype/numpy_quaddtype/src/dragon4.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -974,22 +974,22 @@ PrintInfNan(char *buffer, npy_uint32 bufferSize, npy_uint64 mantissa, npy_uint32
974974

975975
DEBUG_ASSERT(bufferSize > 0);
976976

977+
/* Print sign for both inf and nan values */
978+
if (signbit == '+') {
979+
if (pos < maxPrintLen - 1) {
980+
buffer[pos++] = '+';
981+
}
982+
}
983+
else if (signbit == '-') {
984+
if (pos < maxPrintLen - 1) {
985+
buffer[pos++] = '-';
986+
}
987+
}
988+
977989
/* Check for infinity */
978990
if (mantissa == 0) {
979991
npy_uint32 printLen;
980992

981-
/* only print sign for inf values (though nan can have a sign set) */
982-
if (signbit == '+') {
983-
if (pos < maxPrintLen - 1) {
984-
buffer[pos++] = '+';
985-
}
986-
}
987-
else if (signbit == '-') {
988-
if (pos < maxPrintLen - 1) {
989-
buffer[pos++] = '-';
990-
}
991-
}
992-
993993
/* copy and make sure the buffer is terminated */
994994
printLen = (3 < maxPrintLen - pos) ? 3 : maxPrintLen - pos;
995995
memcpy(buffer + pos, "inf", printLen);

quaddtype/numpy_quaddtype/src/scalar.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
186186
self->value.sleef_value = Sleef_cast_from_doubleq1(dval);
187187
}
188188
else {
189+
// np.longdouble does not preserve sign of while nan construction`
190+
// if (isnan(dval)) {
191+
// self->value.longdouble_value = copysignl(NAN, dval);
192+
// }
193+
// else
189194
self->value.longdouble_value = (long double)dval;
190195
}
191196
}

0 commit comments

Comments
 (0)