Skip to content
Merged
Prev Previous commit
Next Next commit
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.
  • Loading branch information
seberg committed May 7, 2023
commit 230882ca8cdde12618b3a8b729b5d6a9f7fa3d96
16 changes: 9 additions & 7 deletions Include/pymath.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@
// Return 1 if float or double arg is neither infinite nor NAN, else 0.
#define Py_IS_FINITE(X) isfinite(X)

/* HUGE_VAL is supposed to expand to a positive double infinity. Python
* uses Py_HUGE_VAL instead because some platforms are broken in this
* respect. We used to embed code in pyport.h to try to worm around that,
* but different platforms are broken in conflicting ways. If you're on
* a platform where HUGE_VAL is defined incorrectly, fiddle your Python
* config to #define Py_HUGE_VAL to something that works on your platform.
// Py_INFINITY: Value that evaluates to a positive double infinity.
#ifndef Py_INFINITY
# define Py_INFINITY ((double)INFINITY)
#endif

/* Py_HUGE_VAL should always be the same as Py_INFINITY. But historically
* this was not reliable and Python did not require IEEE floats and C99
* conformity. Prefer Py_INFINITY for new code.
*/
#ifndef Py_HUGE_VAL
# define Py_HUGE_VAL HUGE_VAL
#endif

// Py_NAN: Value that evaluates to a quiet Not-a-Number (NaN).
// Py_NAN: Value that evaluates to a quiet and positive Not-a-Number (NaN).
#if !defined(Py_NAN)
# if _Py__has_builtin(__builtin_nan)
// Built-in implementation of the ISO C99 function nan(): quiet NaN.
Expand Down
11 changes: 2 additions & 9 deletions Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1230,25 +1230,18 @@ cmath_exec(PyObject *mod)
return -1;
}

Py_complex infj = {0.0, Py_HUGE_VAL};
Py_complex infj = {0.0, Py_INFINITY};
if (PyModule_AddObject(mod, "infj",
PyComplex_FromCComplex(infj)) < 0) {
return -1;
}
#if _PY_SHORT_FLOAT_REPR == 1
/*
* NaN exposure is guarded by having IEEE doubles via _PY_SHORT_FLOAT_REPR.
* This is probably an overly restrictive guard.
*/
if (PyModule_AddObject(mod, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
return -1;
}
Py_complex nanj = {0.0, Py_NAN};
if (PyModule_AddObject(mod, "nanj",
PyComplex_FromCComplex(nanj)) < 0) {
if (PyModule_AddObject(mod, "nanj", PyComplex_FromCComplex(nanj)) < 0) {
return -1;
}
#endif

/* initialize special value tables */

Expand Down
8 changes: 1 addition & 7 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3946,18 +3946,12 @@ math_exec(PyObject *module)
if (PyModule_AddObject(module, "tau", PyFloat_FromDouble(Py_MATH_TAU)) < 0) {
return -1;
}
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_HUGE_VAL)) < 0) {
if (PyModule_AddObject(module, "inf", PyFloat_FromDouble(Py_INFINITY)) < 0) {
return -1;
}
#if _PY_SHORT_FLOAT_REPR == 1
/*
* NaN exposure is guarded by having IEEE doubles via _PY_SHORT_FLOAT_REPR.
* This is probably an overly restrictive guard.
*/
if (PyModule_AddObject(module, "nan", PyFloat_FromDouble(Py_NAN)) < 0) {
return -1;
}
#endif
return 0;
}

Expand Down