Skip to content

Commit 230882c

Browse files
committed
MAINT: Introduce and use Py_INFINIY; rely on NaN being defined
We can rely on both as Python now forces IEEE compliance and C99 so that both should always be well defined and there is no need for `math.nan` not being defined.
1 parent c88a6fb commit 230882c

File tree

3 files changed

+12
-23
lines changed

3 files changed

+12
-23
lines changed

Include/pymath.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@
3939
// Return 1 if float or double arg is neither infinite nor NAN, else 0.
4040
#define Py_IS_FINITE(X) isfinite(X)
4141

42-
/* HUGE_VAL is supposed to expand to a positive double infinity. Python
43-
* uses Py_HUGE_VAL instead because some platforms are broken in this
44-
* respect. We used to embed code in pyport.h to try to worm around that,
45-
* but different platforms are broken in conflicting ways. If you're on
46-
* a platform where HUGE_VAL is defined incorrectly, fiddle your Python
47-
* config to #define Py_HUGE_VAL to something that works on your platform.
42+
// Py_INFINITY: Value that evaluates to a positive double infinity.
43+
#ifndef Py_INFINITY
44+
# define Py_INFINITY ((double)INFINITY)
45+
#endif
46+
47+
/* Py_HUGE_VAL should always be the same as Py_INFINITY. But historically
48+
* this was not reliable and Python did not require IEEE floats and C99
49+
* conformity. Prefer Py_INFINITY for new code.
4850
*/
4951
#ifndef Py_HUGE_VAL
5052
# define Py_HUGE_VAL HUGE_VAL
5153
#endif
5254

53-
// Py_NAN: Value that evaluates to a quiet Not-a-Number (NaN).
55+
// Py_NAN: Value that evaluates to a quiet and positive Not-a-Number (NaN).
5456
#if !defined(Py_NAN)
5557
# if _Py__has_builtin(__builtin_nan)
5658
// Built-in implementation of the ISO C99 function nan(): quiet NaN.

Modules/cmathmodule.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,25 +1230,18 @@ cmath_exec(PyObject *mod)
12301230
return -1;
12311231
}
12321232

1233-
Py_complex infj = {0.0, Py_HUGE_VAL};
1233+
Py_complex infj = {0.0, Py_INFINITY};
12341234
if (PyModule_AddObject(mod, "infj",
12351235
PyComplex_FromCComplex(infj)) < 0) {
12361236
return -1;
12371237
}
1238-
#if _PY_SHORT_FLOAT_REPR == 1
1239-
/*
1240-
* NaN exposure is guarded by having IEEE doubles via _PY_SHORT_FLOAT_REPR.
1241-
* This is probably an overly restrictive guard.
1242-
*/
12431238
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
12441239
return -1;
12451240
}
12461241
Py_complex nanj = {0.0, Py_NAN};
1247-
if (PyModule_AddObject(mod, "nanj",
1248-
PyComplex_FromCComplex(nanj)) < 0) {
1242+
if (PyModule_AddObject(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
12491243
return -1;
12501244
}
1251-
#endif
12521245

12531246
/* initialize special value tables */
12541247

Modules/mathmodule.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,18 +3946,12 @@ math_exec(PyObject *module)
39463946
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
39473947
return -1;
39483948
}
3949-
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_HUGE_VAL)) < 0) {
3949+
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
39503950
return -1;
39513951
}
3952-
#if _PY_SHORT_FLOAT_REPR == 1
3953-
/*
3954-
* NaN exposure is guarded by having IEEE doubles via _PY_SHORT_FLOAT_REPR.
3955-
* This is probably an overly restrictive guard.
3956-
*/
39573952
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
39583953
return -1;
39593954
}
3960-
#endif
39613955
return 0;
39623956
}
39633957

0 commit comments

Comments
 (0)