Skip to content

Commit a35ae19

Browse files
committed
Rely on C99 NAN being defined and ensure NaN sign for creation functions
1 parent e8c36a2 commit a35ae19

File tree

5 files changed

+8
-13
lines changed

5 files changed

+8
-13
lines changed

Include/pymath.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,11 @@
5252
# define Py_HUGE_VAL HUGE_VAL
5353
#endif
5454

55-
// Py_NAN: Value that evaluates to a quiet and positive Not-a-Number (NaN).
55+
/* Py_NAN: Value that evaluates to a quiet Not-a-Number (NaN). The sign is
56+
* undefined and normally not relevant, but e.g. fixed for float("nan").
57+
*/
5658
#if !defined(Py_NAN)
57-
# if _Py__has_builtin(__builtin_nan)
58-
// Built-in implementation of the ISO C99 function nan(): quiet NaN.
59-
# define Py_NAN (__builtin_nan(""))
60-
#else
61-
// Use C99 NAN constant: quiet Not-A-Number.
62-
// NAN is a float, Py_NAN is a double: cast to double.
6359
# define Py_NAN ((double)NAN)
64-
# endif
6560
#endif
6661

6762
#endif /* Py_PYMATH_H */

Modules/cmathmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,10 +1235,10 @@ cmath_exec(PyObject *mod)
12351235
PyComplex_FromCComplex(infj)) < 0) {
12361236
return -1;
12371237
}
1238-
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
1238+
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
12391239
return -1;
12401240
}
1241-
Py_complex nanj = {0.0, Py_NAN};
1241+
Py_complex nanj = {0.0, fabs(Py_NAN)};
12421242
if (PyModule_AddObject(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
12431243
return -1;
12441244
}

Modules/mathmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3949,7 +3949,7 @@ math_exec(PyObject *module)
39493949
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
39503950
return -1;
39513951
}
3952-
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
3952+
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(fabs(Py_NAN))) < 0) {
39533953
return -1;
39543954
}
39553955
return 0;

Objects/floatobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ PyFloat_Unpack2(const char *data, int le)
24302430
}
24312431
else {
24322432
/* NaN */
2433-
return sign ? -Py_NAN : Py_NAN;
2433+
return sign ? -fabs(Py_NAN) : fabs(Py_NAN);
24342434
}
24352435
}
24362436

Python/pystrtod.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ _Py_parse_inf_or_nan(const char *p, char **endptr)
4646
}
4747
else if (case_insensitive_match(s, "nan")) {
4848
s += 3;
49-
retval = negate ? -Py_NAN : Py_NAN;
49+
retval = negate ? -fabs(Py_NAN) : fabs(Py_NAN);
5050
}
5151
else {
5252
s = p;

0 commit comments

Comments
 (0)