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

Skip to content

Commit e57950f

Browse files
committed
Merged revisions 62420-62421,62423-62424 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r62420 | mark.dickinson | 2008-04-20 20:30:05 +0200 (Sun, 20 Apr 2008) | 3 lines Even more fixes for alpha Tru64, this time for the phase and polar methods. ........ r62421 | mark.dickinson | 2008-04-20 22:38:48 +0200 (Sun, 20 Apr 2008) | 2 lines Add test for tanh(-0.) == -0. on IEEE 754 systems ........ r62423 | amaury.forgeotdarc | 2008-04-20 23:02:21 +0200 (Sun, 20 Apr 2008) | 3 lines Correct an apparent refleak in test_pkgutil: zipimport._zip_directory_cache contains info for all processed zip files, even when they are no longer used. ........ r62424 | mark.dickinson | 2008-04-20 23:39:04 +0200 (Sun, 20 Apr 2008) | 4 lines math.atan2 is misbehaving on Windows; this patch should fix the problem in the same way that the cmath.phase problems were fixed. ........
1 parent 1a3239e commit e57950f

4 files changed

Lines changed: 108 additions & 2 deletions

File tree

Lib/test/test_math.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,59 @@ def testAtan2(self):
124124
self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
125125
self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
126126

127+
# math.atan2(0, x)
128+
self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
129+
self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
130+
self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
131+
self.assertEqual(math.atan2(0., 0.), 0.)
132+
self.assertEqual(math.atan2(0., 2.3), 0.)
133+
self.assertEqual(math.atan2(0., INF), 0.)
134+
self.assert_(math.isnan(math.atan2(0., NAN)))
135+
# math.atan2(-0, x)
136+
self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
137+
self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
138+
self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
139+
self.assertEqual(math.atan2(-0., 0.), -0.)
140+
self.assertEqual(math.atan2(-0., 2.3), -0.)
141+
self.assertEqual(math.atan2(-0., INF), -0.)
142+
self.assert_(math.isnan(math.atan2(-0., NAN)))
143+
# math.atan2(INF, x)
144+
self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
145+
self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
146+
self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
147+
self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
148+
self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
149+
self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
150+
self.assert_(math.isnan(math.atan2(INF, NAN)))
151+
# math.atan2(NINF, x)
152+
self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
153+
self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
154+
self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
155+
self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
156+
self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
157+
self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
158+
self.assert_(math.isnan(math.atan2(NINF, NAN)))
159+
# math.atan2(+finite, x)
160+
self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
161+
self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
162+
self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
163+
self.assertEqual(math.atan2(2.3, INF), 0.)
164+
self.assert_(math.isnan(math.atan2(2.3, NAN)))
165+
# math.atan2(-finite, x)
166+
self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
167+
self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
168+
self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
169+
self.assertEqual(math.atan2(-2.3, INF), -0.)
170+
self.assert_(math.isnan(math.atan2(-2.3, NAN)))
171+
# math.atan2(NAN, x)
172+
self.assert_(math.isnan(math.atan2(NAN, NINF)))
173+
self.assert_(math.isnan(math.atan2(NAN, -2.3)))
174+
self.assert_(math.isnan(math.atan2(NAN, -0.)))
175+
self.assert_(math.isnan(math.atan2(NAN, 0.)))
176+
self.assert_(math.isnan(math.atan2(NAN, 2.3)))
177+
self.assert_(math.isnan(math.atan2(NAN, INF)))
178+
self.assert_(math.isnan(math.atan2(NAN, NAN)))
179+
127180
def testCeil(self):
128181
self.assertRaises(TypeError, math.ceil)
129182
self.assertEquals(int, type(math.ceil(0.5)))
@@ -575,6 +628,11 @@ def testTanh(self):
575628
self.ftest('tanh(inf)', math.tanh(INF), 1)
576629
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
577630
self.assert_(math.isnan(math.tanh(NAN)))
631+
# check that tanh(-0.) == -0. on IEEE 754 systems
632+
if float.__getformat__("double").startswith("IEEE"):
633+
self.assertEqual(math.tanh(-0.), -0.)
634+
self.assertEqual(math.copysign(1., math.tanh(-0.)),
635+
math.copysign(1., -0.))
578636

579637
def test_trunc(self):
580638
self.assertEqual(math.trunc(1), 1)

Lib/test/test_pkgutil.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ def test_alreadyloaded(self):
122122

123123
def test_main():
124124
run_unittest(PkgutilTests, PkgutilPEP302Tests)
125+
# this is necessary if test is run repeated (like when finding leaks)
126+
import zipimport
127+
zipimport._zip_directory_cache.clear()
125128

126129
if __name__ == '__main__':
127130
test_main()

Modules/cmathmodule.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ c_atan(Py_complex z)
264264
return r;
265265
}
266266

267-
/* Windows screws up atan2 for inf and nan */
267+
/* Windows screws up atan2 for inf and nan, and alpha Tru64 5.1 doesn't follow
268+
C99 for atan2(0., 0.). */
268269
static double
269270
c_atan2(Py_complex z)
270271
{
@@ -282,6 +283,14 @@ c_atan2(Py_complex z)
282283
/* atan2(+-inf, x) == +-pi/2 for finite x */
283284
return copysign(0.5*Py_MATH_PI, z.imag);
284285
}
286+
if (Py_IS_INFINITY(z.real) || z.imag == 0.) {
287+
if (copysign(1., z.real) == 1.)
288+
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
289+
return copysign(0., z.imag);
290+
else
291+
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
292+
return copysign(Py_MATH_PI, z.imag);
293+
}
285294
return atan2(z.imag, z.real);
286295
}
287296

Modules/mathmodule.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,42 @@ is_error(double x)
9595
return result;
9696
}
9797

98+
/*
99+
wrapper for atan2 that deals directly with special cases before
100+
delegating to the platform libm for the remaining cases. This
101+
is necessary to get consistent behaviour across platforms.
102+
Windows, FreeBSD and alpha Tru64 are amongst platforms that don't
103+
always follow C99.
104+
*/
105+
106+
static double
107+
m_atan2(double y, double x)
108+
{
109+
if (Py_IS_NAN(x) || Py_IS_NAN(y))
110+
return Py_NAN;
111+
if (Py_IS_INFINITY(y)) {
112+
if (Py_IS_INFINITY(x)) {
113+
if (copysign(1., x) == 1.)
114+
/* atan2(+-inf, +inf) == +-pi/4 */
115+
return copysign(0.25*Py_MATH_PI, y);
116+
else
117+
/* atan2(+-inf, -inf) == +-pi*3/4 */
118+
return copysign(0.75*Py_MATH_PI, y);
119+
}
120+
/* atan2(+-inf, x) == +-pi/2 for finite x */
121+
return copysign(0.5*Py_MATH_PI, y);
122+
}
123+
if (Py_IS_INFINITY(x) || y == 0.) {
124+
if (copysign(1., x) == 1.)
125+
/* atan2(+-y, +inf) = atan2(+-0, +x) = +-0. */
126+
return copysign(0., y);
127+
else
128+
/* atan2(+-y, -inf) = atan2(+-0., -x) = +-pi. */
129+
return copysign(Py_MATH_PI, y);
130+
}
131+
return atan2(y, x);
132+
}
133+
98134
/*
99135
math_1 is used to wrap a libm function f that takes a double
100136
arguments and returns a double.
@@ -250,7 +286,7 @@ FUNC1(asinh, asinh, 0,
250286
"asinh(x)\n\nReturn the hyperbolic arc sine (measured in radians) of x.")
251287
FUNC1(atan, atan, 0,
252288
"atan(x)\n\nReturn the arc tangent (measured in radians) of x.")
253-
FUNC2(atan2, atan2,
289+
FUNC2(atan2, m_atan2,
254290
"atan2(y, x)\n\nReturn the arc tangent (measured in radians) of y/x.\n"
255291
"Unlike atan(y/x), the signs of both x and y are considered.")
256292
FUNC1(atanh, atanh, 0,

0 commit comments

Comments
 (0)