Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b433aa

Browse files
authored
gh-122681: merge m_atan2() and c_atan2() helper functions (#122682)
1 parent 6ff82fd commit 0b433aa

File tree

3 files changed

+41
-69
lines changed

3 files changed

+41
-69
lines changed

Modules/_math.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,42 @@ _Py_log1p(double x)
2323
}
2424

2525
#define m_log1p _Py_log1p
26+
27+
/*
28+
wrapper for atan2 that deals directly with special cases before
29+
delegating to the platform libm for the remaining cases. This
30+
is necessary to get consistent behaviour across platforms.
31+
Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
32+
always follow C99. Windows screws up atan2 for inf and nan, and
33+
alpha Tru64 5.1 doesn't follow C99 for atan2(0., 0.).
34+
*/
35+
36+
static double
37+
_Py_atan2(double y, double x)
38+
{
39+
if (isnan(x) || isnan(y))
40+
return Py_NAN;
41+
if (isinf(y)) {
42+
if (isinf(x)) {
43+
if (copysign(1., x) == 1.)
44+
/* atan2(+-inf, +inf) == +-pi/4 */
45+
return copysign(0.25*Py_MATH_PI, y);
46+
else
47+
/* atan2(+-inf, -inf) == +-pi*3/4 */
48+
return copysign(0.75*Py_MATH_PI, y);
49+
}
50+
/* atan2(+-inf, x) == +-pi/2 for finite x */
51+
return copysign(0.5*Py_MATH_PI, y);
52+
}
53+
if (isinf(x) || y == 0.) {
54+
if (copysign(1., x) == 1.)
55+
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
56+
return copysign(0., y);
57+
else
58+
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
59+
return copysign(Py_MATH_PI, y);
60+
}
61+
return atan2(y, x);
62+
}
63+
64+
#define m_atan2 _Py_atan2

Modules/cmathmodule.c

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -324,36 +324,6 @@ cmath_atan_impl(PyObject *module, Py_complex z)
324324
return r;
325325
}
326326

327-
/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
328-
C99 for atan2(0., 0.). */
329-
static double
330-
c_atan2(Py_complex z)
331-
{
332-
if (isnan(z.real) || isnan(z.imag))
333-
return Py_NAN;
334-
if (isinf(z.imag)) {
335-
if (isinf(z.real)) {
336-
if (copysign(1., z.real) == 1.)
337-
/* atan2(+-inf, +inf) == +-pi/4 */
338-
return copysign(0.25*Py_MATH_PI, z.imag);
339-
else
340-
/* atan2(+-inf, -inf) == +-pi*3/4 */
341-
return copysign(0.75*Py_MATH_PI, z.imag);
342-
}
343-
/* atan2(+-inf, x) == +-pi/2 for finite x */
344-
return copysign(0.5*Py_MATH_PI, z.imag);
345-
}
346-
if (isinf(z.real) || z.imag == 0.) {
347-
if (copysign(1., z.real) == 1.)
348-
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
349-
return copysign(0., z.imag);
350-
else
351-
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
352-
return copysign(Py_MATH_PI, z.imag);
353-
}
354-
return atan2(z.imag, z.real);
355-
}
356-
357327

358328
static Py_complex atanh_special_values[7][7];
359329

@@ -966,7 +936,7 @@ cmath_phase_impl(PyObject *module, Py_complex z)
966936
double phi;
967937

968938
errno = 0;
969-
phi = c_atan2(z); /* should not cause any exception */
939+
phi = m_atan2(z.imag, z.real); /* should not cause any exception */
970940
if (errno != 0)
971941
return math_error();
972942
else
@@ -991,7 +961,7 @@ cmath_polar_impl(PyObject *module, Py_complex z)
991961
double r, phi;
992962

993963
errno = 0;
994-
phi = c_atan2(z); /* should not cause any exception */
964+
phi = m_atan2(z.imag, z.real); /* should not cause any exception */
995965
r = _Py_c_abs(z); /* sets errno to ERANGE on overflow */
996966
if (errno != 0)
997967
return math_error();

Modules/mathmodule.c

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -535,43 +535,6 @@ m_lgamma(double x)
535535
return r;
536536
}
537537

538-
/*
539-
wrapper for atan2 that deals directly with special cases before
540-
delegating to the platform libm for the remaining cases. This
541-
is necessary to get consistent behaviour across platforms.
542-
Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
543-
always follow C99.
544-
*/
545-
546-
static double
547-
m_atan2(double y, double x)
548-
{
549-
if (isnan(x) || isnan(y))
550-
return Py_NAN;
551-
if (isinf(y)) {
552-
if (isinf(x)) {
553-
if (copysign(1., x) == 1.)
554-
/* atan2(+-inf, +inf) == +-pi/4 */
555-
return copysign(0.25*Py_MATH_PI, y);
556-
else
557-
/* atan2(+-inf, -inf) == +-pi*3/4 */
558-
return copysign(0.75*Py_MATH_PI, y);
559-
}
560-
/* atan2(+-inf, x) == +-pi/2 for finite x */
561-
return copysign(0.5*Py_MATH_PI, y);
562-
}
563-
if (isinf(x) || y == 0.) {
564-
if (copysign(1., x) == 1.)
565-
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
566-
return copysign(0., y);
567-
else
568-
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
569-
return copysign(Py_MATH_PI, y);
570-
}
571-
return atan2(y, x);
572-
}
573-
574-
575538
/* IEEE 754-style remainder operation: x - n*y where n*y is the nearest
576539
multiple of y to x, taking n even in the case of a tie. Assuming an IEEE 754
577540
binary floating-point format, the result is always exact. */

0 commit comments

Comments
 (0)