Skip to content

Commit

Permalink
pythongh-119613: Use C99+ functions instead of Py_IS_NAN/INFINITY/FIN…
Browse files Browse the repository at this point in the history
…ITE (python#119619)
  • Loading branch information
skirpichev authored and estyxx committed Jul 17, 2024
1 parent b38bdb4 commit 6153c61
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 142 deletions.
4 changes: 2 additions & 2 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2425,12 +2425,12 @@ PyDecType_FromFloatExact(PyTypeObject *type, PyObject *v,
}
sign = (copysign(1.0, x) == 1.0) ? 0 : 1;

if (Py_IS_NAN(x) || Py_IS_INFINITY(x)) {
if (isnan(x) || isinf(x)) {
dec = PyDecType_New(type);
if (dec == NULL) {
return NULL;
}
if (Py_IS_NAN(x)) {
if (isnan(x)) {
/* decimal.py calls repr(float(+-nan)),
* which always gives a positive result. */
mpd_setspecial(MPD(dec), MPD_POS, MPD_NAN);
Expand Down
2 changes: 1 addition & 1 deletion Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1326,7 +1326,7 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
{
/* Return the JSON representation of a PyFloat. */
double i = PyFloat_AS_DOUBLE(obj);
if (!Py_IS_FINITE(i)) {
if (!isfinite(i)) {
if (!s->allow_nan) {
PyErr_Format(
PyExc_ValueError,
Expand Down
72 changes: 35 additions & 37 deletions Modules/cmathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ enum special_types {
static enum special_types
special_type(double d)
{
if (Py_IS_FINITE(d)) {
if (isfinite(d)) {
if (d != 0) {
if (copysign(1., d) == 1.)
return ST_POS;
Expand All @@ -131,19 +131,19 @@ special_type(double d)
return ST_NZERO;
}
}
if (Py_IS_NAN(d))
if (isnan(d))
return ST_NAN;
if (copysign(1., d) == 1.)
return ST_PINF;
else
return ST_NINF;
}

#define SPECIAL_VALUE(z, table) \
if (!Py_IS_FINITE((z).real) || !Py_IS_FINITE((z).imag)) { \
errno = 0; \
return table[special_type((z).real)] \
[special_type((z).imag)]; \
#define SPECIAL_VALUE(z, table) \
if (!isfinite((z).real) || !isfinite((z).imag)) { \
errno = 0; \
return table[special_type((z).real)] \
[special_type((z).imag)]; \
}

#define P Py_MATH_PI
Expand Down Expand Up @@ -329,10 +329,10 @@ cmath_atan_impl(PyObject *module, Py_complex z)
static double
c_atan2(Py_complex z)
{
if (Py_IS_NAN(z.real) || Py_IS_NAN(z.imag))
if (isnan(z.real) || isnan(z.imag))
return Py_NAN;
if (Py_IS_INFINITY(z.imag)) {
if (Py_IS_INFINITY(z.real)) {
if (isinf(z.imag)) {
if (isinf(z.real)) {
if (copysign(1., z.real) == 1.)
/* atan2(+-inf, +inf) == +-pi/4 */
return copysign(0.25*Py_MATH_PI, z.imag);
Expand All @@ -343,7 +343,7 @@ c_atan2(Py_complex z)
/* atan2(+-inf, x) == +-pi/2 for finite x */
return copysign(0.5*Py_MATH_PI, z.imag);
}
if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
if (isinf(z.real) || z.imag == 0.) {
if (copysign(1., z.real) == 1.)
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
return copysign(0., z.imag);
Expand Down Expand Up @@ -448,8 +448,8 @@ cmath_cosh_impl(PyObject *module, Py_complex z)
double x_minus_one;

/* special treatment for cosh(+/-inf + iy) if y is not a NaN */
if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag) &&
if (!isfinite(z.real) || !isfinite(z.imag)) {
if (isinf(z.real) && isfinite(z.imag) &&
(z.imag != 0.)) {
if (z.real > 0) {
r.real = copysign(INF, cos(z.imag));
Expand All @@ -466,7 +466,7 @@ cmath_cosh_impl(PyObject *module, Py_complex z)
}
/* need to set errno = EDOM if y is +/- infinity and x is not
a NaN */
if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
if (isinf(z.imag) && !isnan(z.real))
errno = EDOM;
else
errno = 0;
Expand All @@ -484,7 +484,7 @@ cmath_cosh_impl(PyObject *module, Py_complex z)
r.imag = sin(z.imag) * sinh(z.real);
}
/* detect overflow, and set errno accordingly */
if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
if (isinf(r.real) || isinf(r.imag))
errno = ERANGE;
else
errno = 0;
Expand All @@ -509,8 +509,8 @@ cmath_exp_impl(PyObject *module, Py_complex z)
Py_complex r;
double l;

if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
if (!isfinite(z.real) || !isfinite(z.imag)) {
if (isinf(z.real) && isfinite(z.imag)
&& (z.imag != 0.)) {
if (z.real > 0) {
r.real = copysign(INF, cos(z.imag));
Expand All @@ -527,9 +527,9 @@ cmath_exp_impl(PyObject *module, Py_complex z)
}
/* need to set errno = EDOM if y is +/- infinity and x is not
a NaN and not -infinity */
if (Py_IS_INFINITY(z.imag) &&
(Py_IS_FINITE(z.real) ||
(Py_IS_INFINITY(z.real) && z.real > 0)))
if (isinf(z.imag) &&
(isfinite(z.real) ||
(isinf(z.real) && z.real > 0)))
errno = EDOM;
else
errno = 0;
Expand All @@ -546,7 +546,7 @@ cmath_exp_impl(PyObject *module, Py_complex z)
r.imag = l*sin(z.imag);
}
/* detect overflow, and set errno accordingly */
if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
if (isinf(r.real) || isinf(r.imag))
errno = ERANGE;
else
errno = 0;
Expand Down Expand Up @@ -686,8 +686,8 @@ cmath_sinh_impl(PyObject *module, Py_complex z)

/* special treatment for sinh(+/-inf + iy) if y is finite and
nonzero */
if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
if (!isfinite(z.real) || !isfinite(z.imag)) {
if (isinf(z.real) && isfinite(z.imag)
&& (z.imag != 0.)) {
if (z.real > 0) {
r.real = copysign(INF, cos(z.imag));
Expand All @@ -704,7 +704,7 @@ cmath_sinh_impl(PyObject *module, Py_complex z)
}
/* need to set errno = EDOM if y is +/- infinity and x is not
a NaN */
if (Py_IS_INFINITY(z.imag) && !Py_IS_NAN(z.real))
if (isinf(z.imag) && !isnan(z.real))
errno = EDOM;
else
errno = 0;
Expand All @@ -720,7 +720,7 @@ cmath_sinh_impl(PyObject *module, Py_complex z)
r.imag = sin(z.imag) * cosh(z.real);
}
/* detect overflow, and set errno accordingly */
if (Py_IS_INFINITY(r.real) || Py_IS_INFINITY(r.imag))
if (isinf(r.real) || isinf(r.imag))
errno = ERANGE;
else
errno = 0;
Expand Down Expand Up @@ -856,8 +856,8 @@ cmath_tanh_impl(PyObject *module, Py_complex z)

/* special treatment for tanh(+/-inf + iy) if y is finite and
nonzero */
if (!Py_IS_FINITE(z.real) || !Py_IS_FINITE(z.imag)) {
if (Py_IS_INFINITY(z.real) && Py_IS_FINITE(z.imag)
if (!isfinite(z.real) || !isfinite(z.imag)) {
if (isinf(z.real) && isfinite(z.imag)
&& (z.imag != 0.)) {
if (z.real > 0) {
r.real = 1.0;
Expand All @@ -876,7 +876,7 @@ cmath_tanh_impl(PyObject *module, Py_complex z)
}
/* need to set errno = EDOM if z.imag is +/-infinity and
z.real is finite */
if (Py_IS_INFINITY(z.imag) && Py_IS_FINITE(z.real))
if (isinf(z.imag) && isfinite(z.real))
errno = EDOM;
else
errno = 0;
Expand Down Expand Up @@ -1030,11 +1030,11 @@ cmath_rect_impl(PyObject *module, double r, double phi)
errno = 0;

/* deal with special values */
if (!Py_IS_FINITE(r) || !Py_IS_FINITE(phi)) {
if (!isfinite(r) || !isfinite(phi)) {
/* if r is +/-infinity and phi is finite but nonzero then
result is (+-INF +-INF i), but we need to compute cos(phi)
and sin(phi) to figure out the signs. */
if (Py_IS_INFINITY(r) && (Py_IS_FINITE(phi)
if (isinf(r) && (isfinite(phi)
&& (phi != 0.))) {
if (r > 0) {
z.real = copysign(INF, cos(phi));
Expand All @@ -1051,7 +1051,7 @@ cmath_rect_impl(PyObject *module, double r, double phi)
}
/* need to set errno = EDOM if r is a nonzero number and phi
is infinite */
if (r != 0. && !Py_IS_NAN(r) && Py_IS_INFINITY(phi))
if (r != 0. && !isnan(r) && isinf(phi))
errno = EDOM;
else
errno = 0;
Expand Down Expand Up @@ -1085,7 +1085,7 @@ static PyObject *
cmath_isfinite_impl(PyObject *module, Py_complex z)
/*[clinic end generated code: output=ac76611e2c774a36 input=848e7ee701895815]*/
{
return PyBool_FromLong(Py_IS_FINITE(z.real) && Py_IS_FINITE(z.imag));
return PyBool_FromLong(isfinite(z.real) && isfinite(z.imag));
}

/*[clinic input]
Expand All @@ -1098,7 +1098,7 @@ static PyObject *
cmath_isnan_impl(PyObject *module, Py_complex z)
/*[clinic end generated code: output=e7abf6e0b28beab7 input=71799f5d284c9baf]*/
{
return PyBool_FromLong(Py_IS_NAN(z.real) || Py_IS_NAN(z.imag));
return PyBool_FromLong(isnan(z.real) || isnan(z.imag));
}

/*[clinic input]
Expand All @@ -1111,8 +1111,7 @@ static PyObject *
cmath_isinf_impl(PyObject *module, Py_complex z)
/*[clinic end generated code: output=502a75a79c773469 input=363df155c7181329]*/
{
return PyBool_FromLong(Py_IS_INFINITY(z.real) ||
Py_IS_INFINITY(z.imag));
return PyBool_FromLong(isinf(z.real) || isinf(z.imag));
}

/*[clinic input]
Expand Down Expand Up @@ -1167,8 +1166,7 @@ cmath_isclose_impl(PyObject *module, Py_complex a, Py_complex b,
above.
*/

if (Py_IS_INFINITY(a.real) || Py_IS_INFINITY(a.imag) ||
Py_IS_INFINITY(b.real) || Py_IS_INFINITY(b.imag)) {
if (isinf(a.real) || isinf(a.imag) || isinf(b.real) || isinf(b.imag)) {
return 0;
}

Expand Down
Loading

0 comments on commit 6153c61

Please sign in to comment.