Skip to content

Commit 534a64e

Browse files
committed
handling nan in same_value
1 parent 3382565 commit 534a64e

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1234,14 +1234,28 @@ static inline int quad_to_numpy_same_value_check(quad_value x, QuadBackendType b
12341234
*y = from_quad<T>(x, backend);
12351235
quad_value roundtrip = to_quad<T>(*y, backend);
12361236
if(backend == BACKEND_SLEEF) {
1237-
if(Sleef_icmpeqq1(x.sleef_value, roundtrip.sleef_value))
1237+
if(Sleef_iunordq1(x.sleef_value, roundtrip.sleef_value))
1238+
return 1;
1239+
else if(Sleef_icmpeqq1(x.sleef_value, roundtrip.sleef_value))
12381240
return 1;
12391241
}
12401242
else {
1241-
if(x.longdouble_value == roundtrip.longdouble_value)
1243+
if(std::isnan(x.longdouble_value) && std::isnan(roundtrip.longdouble_value))
1244+
return 1;
1245+
else if(x.longdouble_value == roundtrip.longdouble_value)
12421246
return 1;
12431247
}
1244-
PyErr_SetString(PyExc_ValueError, "could not cast 'same_value' to QuadType");
1248+
Sleef_quad sleef_val = quad_to_sleef_quad(&x, backend);
1249+
const char *val_str = quad_to_string_adaptive_cstr(&sleef_val, QUAD_STR_WIDTH);
1250+
if (val_str != NULL) {
1251+
PyErr_Format(PyExc_ValueError,
1252+
"QuadPrecision value '%s' cannot be represented exactly in the target dtype",
1253+
val_str);
1254+
}
1255+
else {
1256+
PyErr_SetString(PyExc_ValueError,
1257+
"QuadPrecision value cannot be represented exactly in the target dtype");
1258+
}
12451259
return -1;
12461260
}
12471261

0 commit comments

Comments
 (0)