/Lib/test/test_math.py

http://unladen-swallow.googlecode.com/ · Python · 894 lines · 716 code · 91 blank · 87 comment · 56 complexity · ae6ab4ed74f11a85c9805a612cbd17f0 MD5 · raw file

  1. # Python test set -- math module
  2. # XXXX Should not do tests around zero only
  3. from test.test_support import run_unittest, verbose
  4. import unittest
  5. import math
  6. import os
  7. import sys
  8. import random
  9. eps = 1E-05
  10. NAN = float('nan')
  11. INF = float('inf')
  12. NINF = float('-inf')
  13. # locate file with test values
  14. if __name__ == '__main__':
  15. file = sys.argv[0]
  16. else:
  17. file = __file__
  18. test_dir = os.path.dirname(file) or os.curdir
  19. test_file = os.path.join(test_dir, 'cmath_testcases.txt')
  20. def parse_testfile(fname):
  21. """Parse a file with test values
  22. Empty lines or lines starting with -- are ignored
  23. yields id, fn, arg_real, arg_imag, exp_real, exp_imag
  24. """
  25. with open(fname) as fp:
  26. for line in fp:
  27. # skip comment lines and blank lines
  28. if line.startswith('--') or not line.strip():
  29. continue
  30. lhs, rhs = line.split('->')
  31. id, fn, arg_real, arg_imag = lhs.split()
  32. rhs_pieces = rhs.split()
  33. exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
  34. flags = rhs_pieces[2:]
  35. yield (id, fn,
  36. float(arg_real), float(arg_imag),
  37. float(exp_real), float(exp_imag),
  38. flags
  39. )
  40. class MathTests(unittest.TestCase):
  41. def ftest(self, name, value, expected):
  42. if abs(value-expected) > eps:
  43. # Use %r instead of %f so the error message
  44. # displays full precision. Otherwise discrepancies
  45. # in the last few bits will lead to very confusing
  46. # error messages
  47. self.fail('%s returned %r, expected %r' %
  48. (name, value, expected))
  49. def testConstants(self):
  50. self.ftest('pi', math.pi, 3.1415926)
  51. self.ftest('e', math.e, 2.7182818)
  52. def testAcos(self):
  53. self.assertRaises(TypeError, math.acos)
  54. self.ftest('acos(-1)', math.acos(-1), math.pi)
  55. self.ftest('acos(0)', math.acos(0), math.pi/2)
  56. self.ftest('acos(1)', math.acos(1), 0)
  57. self.assertRaises(ValueError, math.acos, INF)
  58. self.assertRaises(ValueError, math.acos, NINF)
  59. self.assert_(math.isnan(math.acos(NAN)))
  60. def testAcosh(self):
  61. self.assertRaises(TypeError, math.acosh)
  62. self.ftest('acosh(1)', math.acosh(1), 0)
  63. self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
  64. self.assertRaises(ValueError, math.acosh, 0)
  65. self.assertRaises(ValueError, math.acosh, -1)
  66. self.assertEquals(math.acosh(INF), INF)
  67. self.assertRaises(ValueError, math.acosh, NINF)
  68. self.assert_(math.isnan(math.acosh(NAN)))
  69. def testAsin(self):
  70. self.assertRaises(TypeError, math.asin)
  71. self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
  72. self.ftest('asin(0)', math.asin(0), 0)
  73. self.ftest('asin(1)', math.asin(1), math.pi/2)
  74. self.assertRaises(ValueError, math.asin, INF)
  75. self.assertRaises(ValueError, math.asin, NINF)
  76. self.assert_(math.isnan(math.asin(NAN)))
  77. def testAsinh(self):
  78. self.assertRaises(TypeError, math.asinh)
  79. self.ftest('asinh(0)', math.asinh(0), 0)
  80. self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
  81. self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
  82. self.assertEquals(math.asinh(INF), INF)
  83. self.assertEquals(math.asinh(NINF), NINF)
  84. self.assert_(math.isnan(math.asinh(NAN)))
  85. def testAtan(self):
  86. self.assertRaises(TypeError, math.atan)
  87. self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
  88. self.ftest('atan(0)', math.atan(0), 0)
  89. self.ftest('atan(1)', math.atan(1), math.pi/4)
  90. self.ftest('atan(inf)', math.atan(INF), math.pi/2)
  91. self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
  92. self.assert_(math.isnan(math.atan(NAN)))
  93. def testAtanh(self):
  94. self.assertRaises(TypeError, math.atan)
  95. self.ftest('atanh(0)', math.atanh(0), 0)
  96. self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
  97. self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
  98. self.assertRaises(ValueError, math.atanh, 1)
  99. self.assertRaises(ValueError, math.atanh, -1)
  100. self.assertRaises(ValueError, math.atanh, INF)
  101. self.assertRaises(ValueError, math.atanh, NINF)
  102. self.assert_(math.isnan(math.atanh(NAN)))
  103. def testAtan2(self):
  104. self.assertRaises(TypeError, math.atan2)
  105. self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
  106. self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
  107. self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
  108. self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
  109. self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
  110. # math.atan2(0, x)
  111. self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
  112. self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
  113. self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
  114. self.assertEqual(math.atan2(0., 0.), 0.)
  115. self.assertEqual(math.atan2(0., 2.3), 0.)
  116. self.assertEqual(math.atan2(0., INF), 0.)
  117. self.assert_(math.isnan(math.atan2(0., NAN)))
  118. # math.atan2(-0, x)
  119. self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
  120. self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
  121. self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
  122. self.assertEqual(math.atan2(-0., 0.), -0.)
  123. self.assertEqual(math.atan2(-0., 2.3), -0.)
  124. self.assertEqual(math.atan2(-0., INF), -0.)
  125. self.assert_(math.isnan(math.atan2(-0., NAN)))
  126. # math.atan2(INF, x)
  127. self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
  128. self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
  129. self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
  130. self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
  131. self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
  132. self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
  133. self.assert_(math.isnan(math.atan2(INF, NAN)))
  134. # math.atan2(NINF, x)
  135. self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
  136. self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
  137. self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
  138. self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
  139. self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
  140. self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
  141. self.assert_(math.isnan(math.atan2(NINF, NAN)))
  142. # math.atan2(+finite, x)
  143. self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
  144. self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
  145. self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
  146. self.assertEqual(math.atan2(2.3, INF), 0.)
  147. self.assert_(math.isnan(math.atan2(2.3, NAN)))
  148. # math.atan2(-finite, x)
  149. self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
  150. self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
  151. self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
  152. self.assertEqual(math.atan2(-2.3, INF), -0.)
  153. self.assert_(math.isnan(math.atan2(-2.3, NAN)))
  154. # math.atan2(NAN, x)
  155. self.assert_(math.isnan(math.atan2(NAN, NINF)))
  156. self.assert_(math.isnan(math.atan2(NAN, -2.3)))
  157. self.assert_(math.isnan(math.atan2(NAN, -0.)))
  158. self.assert_(math.isnan(math.atan2(NAN, 0.)))
  159. self.assert_(math.isnan(math.atan2(NAN, 2.3)))
  160. self.assert_(math.isnan(math.atan2(NAN, INF)))
  161. self.assert_(math.isnan(math.atan2(NAN, NAN)))
  162. def testCeil(self):
  163. self.assertRaises(TypeError, math.ceil)
  164. # These types will be int in py3k.
  165. self.assertEquals(float, type(math.ceil(1)))
  166. self.assertEquals(float, type(math.ceil(1L)))
  167. self.assertEquals(float, type(math.ceil(1.0)))
  168. self.ftest('ceil(0.5)', math.ceil(0.5), 1)
  169. self.ftest('ceil(1.0)', math.ceil(1.0), 1)
  170. self.ftest('ceil(1.5)', math.ceil(1.5), 2)
  171. self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
  172. self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
  173. self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
  174. self.assertEquals(math.ceil(INF), INF)
  175. self.assertEquals(math.ceil(NINF), NINF)
  176. self.assert_(math.isnan(math.ceil(NAN)))
  177. class TestCeil(object):
  178. def __float__(self):
  179. return 41.3
  180. class TestNoCeil(object):
  181. pass
  182. self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
  183. self.assertRaises(TypeError, math.ceil, TestNoCeil())
  184. t = TestNoCeil()
  185. t.__ceil__ = lambda *args: args
  186. self.assertRaises(TypeError, math.ceil, t)
  187. self.assertRaises(TypeError, math.ceil, t, 0)
  188. if float.__getformat__("double").startswith("IEEE"):
  189. def testCopysign(self):
  190. self.assertRaises(TypeError, math.copysign)
  191. # copysign should let us distinguish signs of zeros
  192. self.assertEquals(copysign(1., 0.), 1.)
  193. self.assertEquals(copysign(1., -0.), -1.)
  194. self.assertEquals(copysign(INF, 0.), INF)
  195. self.assertEquals(copysign(INF, -0.), NINF)
  196. self.assertEquals(copysign(NINF, 0.), INF)
  197. self.assertEquals(copysign(NINF, -0.), NINF)
  198. # and of infinities
  199. self.assertEquals(copysign(1., INF), 1.)
  200. self.assertEquals(copysign(1., NINF), -1.)
  201. self.assertEquals(copysign(INF, INF), INF)
  202. self.assertEquals(copysign(INF, NINF), NINF)
  203. self.assertEquals(copysign(NINF, INF), INF)
  204. self.assertEquals(copysign(NINF, NINF), NINF)
  205. self.assert_(math.isnan(copysign(NAN, 1.)))
  206. self.assert_(math.isnan(copysign(NAN, INF)))
  207. self.assert_(math.isnan(copysign(NAN, NINF)))
  208. self.assert_(math.isnan(copysign(NAN, NAN)))
  209. # copysign(INF, NAN) may be INF or it may be NINF, since
  210. # we don't know whether the sign bit of NAN is set on any
  211. # given platform.
  212. self.assert_(math.isinf(copysign(INF, NAN)))
  213. # similarly, copysign(2., NAN) could be 2. or -2.
  214. self.assertEquals(abs(copysign(2., NAN)), 2.)
  215. def testCos(self):
  216. self.assertRaises(TypeError, math.cos)
  217. self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
  218. self.ftest('cos(0)', math.cos(0), 1)
  219. self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
  220. self.ftest('cos(pi)', math.cos(math.pi), -1)
  221. try:
  222. self.assert_(math.isnan(math.cos(INF)))
  223. self.assert_(math.isnan(math.cos(NINF)))
  224. except ValueError:
  225. self.assertRaises(ValueError, math.cos, INF)
  226. self.assertRaises(ValueError, math.cos, NINF)
  227. self.assert_(math.isnan(math.cos(NAN)))
  228. def testCosh(self):
  229. self.assertRaises(TypeError, math.cosh)
  230. self.ftest('cosh(0)', math.cosh(0), 1)
  231. self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
  232. self.assertEquals(math.cosh(INF), INF)
  233. self.assertEquals(math.cosh(NINF), INF)
  234. self.assert_(math.isnan(math.cosh(NAN)))
  235. def testDegrees(self):
  236. self.assertRaises(TypeError, math.degrees)
  237. self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
  238. self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
  239. self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
  240. def testExp(self):
  241. self.assertRaises(TypeError, math.exp)
  242. self.ftest('exp(-1)', math.exp(-1), 1/math.e)
  243. self.ftest('exp(0)', math.exp(0), 1)
  244. self.ftest('exp(1)', math.exp(1), math.e)
  245. self.assertEquals(math.exp(INF), INF)
  246. self.assertEquals(math.exp(NINF), 0.)
  247. self.assert_(math.isnan(math.exp(NAN)))
  248. def testFabs(self):
  249. self.assertRaises(TypeError, math.fabs)
  250. self.ftest('fabs(-1)', math.fabs(-1), 1)
  251. self.ftest('fabs(0)', math.fabs(0), 0)
  252. self.ftest('fabs(1)', math.fabs(1), 1)
  253. def testFactorial(self):
  254. def fact(n):
  255. result = 1
  256. for i in range(1, int(n)+1):
  257. result *= i
  258. return result
  259. values = range(10) + [50, 100, 500]
  260. random.shuffle(values)
  261. for x in range(10):
  262. for cast in (int, long, float):
  263. self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
  264. self.assertRaises(ValueError, math.factorial, -1)
  265. self.assertRaises(ValueError, math.factorial, math.pi)
  266. def testFloor(self):
  267. self.assertRaises(TypeError, math.floor)
  268. # These types will be int in py3k.
  269. self.assertEquals(float, type(math.floor(1)))
  270. self.assertEquals(float, type(math.floor(1L)))
  271. self.assertEquals(float, type(math.floor(1.0)))
  272. self.ftest('floor(0.5)', math.floor(0.5), 0)
  273. self.ftest('floor(1.0)', math.floor(1.0), 1)
  274. self.ftest('floor(1.5)', math.floor(1.5), 1)
  275. self.ftest('floor(-0.5)', math.floor(-0.5), -1)
  276. self.ftest('floor(-1.0)', math.floor(-1.0), -1)
  277. self.ftest('floor(-1.5)', math.floor(-1.5), -2)
  278. # pow() relies on floor() to check for integers
  279. # This fails on some platforms - so check it here
  280. self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
  281. self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
  282. self.assertEquals(math.ceil(INF), INF)
  283. self.assertEquals(math.ceil(NINF), NINF)
  284. self.assert_(math.isnan(math.floor(NAN)))
  285. class TestFloor(object):
  286. def __float__(self):
  287. return 42.3
  288. class TestNoFloor(object):
  289. pass
  290. self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
  291. self.assertRaises(TypeError, math.floor, TestNoFloor())
  292. t = TestNoFloor()
  293. t.__floor__ = lambda *args: args
  294. self.assertRaises(TypeError, math.floor, t)
  295. self.assertRaises(TypeError, math.floor, t, 0)
  296. def testFmod(self):
  297. self.assertRaises(TypeError, math.fmod)
  298. self.ftest('fmod(10,1)', math.fmod(10,1), 0)
  299. self.ftest('fmod(10,0.5)', math.fmod(10,0.5), 0)
  300. self.ftest('fmod(10,1.5)', math.fmod(10,1.5), 1)
  301. self.ftest('fmod(-10,1)', math.fmod(-10,1), 0)
  302. self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
  303. self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
  304. self.assert_(math.isnan(math.fmod(NAN, 1.)))
  305. self.assert_(math.isnan(math.fmod(1., NAN)))
  306. self.assert_(math.isnan(math.fmod(NAN, NAN)))
  307. self.assertRaises(ValueError, math.fmod, 1., 0.)
  308. self.assertRaises(ValueError, math.fmod, INF, 1.)
  309. self.assertRaises(ValueError, math.fmod, NINF, 1.)
  310. self.assertRaises(ValueError, math.fmod, INF, 0.)
  311. self.assertEquals(math.fmod(3.0, INF), 3.0)
  312. self.assertEquals(math.fmod(-3.0, INF), -3.0)
  313. self.assertEquals(math.fmod(3.0, NINF), 3.0)
  314. self.assertEquals(math.fmod(-3.0, NINF), -3.0)
  315. self.assertEquals(math.fmod(0.0, 3.0), 0.0)
  316. self.assertEquals(math.fmod(0.0, NINF), 0.0)
  317. def testFrexp(self):
  318. self.assertRaises(TypeError, math.frexp)
  319. def testfrexp(name, (mant, exp), (emant, eexp)):
  320. if abs(mant-emant) > eps or exp != eexp:
  321. self.fail('%s returned %r, expected %r'%\
  322. (name, (mant, exp), (emant,eexp)))
  323. testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
  324. testfrexp('frexp(0)', math.frexp(0), (0, 0))
  325. testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
  326. testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
  327. self.assertEquals(math.frexp(INF)[0], INF)
  328. self.assertEquals(math.frexp(NINF)[0], NINF)
  329. self.assert_(math.isnan(math.frexp(NAN)[0]))
  330. def testFsum(self):
  331. # math.fsum relies on exact rounding for correct operation.
  332. # There's a known problem with IA32 floating-point that causes
  333. # inexact rounding in some situations, and will cause the
  334. # math.fsum tests below to fail; see issue #2937. On non IEEE
  335. # 754 platforms, and on IEEE 754 platforms that exhibit the
  336. # problem described in issue #2937, we simply skip the whole
  337. # test.
  338. if not float.__getformat__("double").startswith("IEEE"):
  339. return
  340. # on IEEE 754 compliant machines, both of the expressions
  341. # below should round to 10000000000000002.0.
  342. if 1e16+2.0 != 1e16+2.9999:
  343. return
  344. # Python version of math.fsum, for comparison. Uses a
  345. # different algorithm based on frexp, ldexp and integer
  346. # arithmetic.
  347. from sys import float_info
  348. mant_dig = float_info.mant_dig
  349. etiny = float_info.min_exp - mant_dig
  350. def msum(iterable):
  351. """Full precision summation. Compute sum(iterable) without any
  352. intermediate accumulation of error. Based on the 'lsum' function
  353. at http://code.activestate.com/recipes/393090/
  354. """
  355. tmant, texp = 0, 0
  356. for x in iterable:
  357. mant, exp = math.frexp(x)
  358. mant, exp = int(math.ldexp(mant, mant_dig)), exp - mant_dig
  359. if texp > exp:
  360. tmant <<= texp-exp
  361. texp = exp
  362. else:
  363. mant <<= exp-texp
  364. tmant += mant
  365. # Round tmant * 2**texp to a float. The original recipe
  366. # used float(str(tmant)) * 2.0**texp for this, but that's
  367. # a little unsafe because str -> float conversion can't be
  368. # relied upon to do correct rounding on all platforms.
  369. tail = max(len(bin(abs(tmant)))-2 - mant_dig, etiny - texp)
  370. if tail > 0:
  371. h = 1 << (tail-1)
  372. tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1)
  373. texp += tail
  374. return math.ldexp(tmant, texp)
  375. test_values = [
  376. ([], 0.0),
  377. ([0.0], 0.0),
  378. ([1e100, 1.0, -1e100, 1e-100, 1e50, -1.0, -1e50], 1e-100),
  379. ([2.0**53, -0.5, -2.0**-54], 2.0**53-1.0),
  380. ([2.0**53, 1.0, 2.0**-100], 2.0**53+2.0),
  381. ([2.0**53+10.0, 1.0, 2.0**-100], 2.0**53+12.0),
  382. ([2.0**53-4.0, 0.5, 2.0**-54], 2.0**53-3.0),
  383. ([1./n for n in range(1, 1001)],
  384. float.fromhex('0x1.df11f45f4e61ap+2')),
  385. ([(-1.)**n/n for n in range(1, 1001)],
  386. float.fromhex('-0x1.62a2af1bd3624p-1')),
  387. ([1.7**(i+1)-1.7**i for i in range(1000)] + [-1.7**1000], -1.0),
  388. ([1e16, 1., 1e-16], 10000000000000002.0),
  389. ([1e16-2., 1.-2.**-53, -(1e16-2.), -(1.-2.**-53)], 0.0),
  390. # exercise code for resizing partials array
  391. ([2.**n - 2.**(n+50) + 2.**(n+52) for n in range(-1074, 972, 2)] +
  392. [-2.**1022],
  393. float.fromhex('0x1.5555555555555p+970')),
  394. ]
  395. for i, (vals, expected) in enumerate(test_values):
  396. try:
  397. actual = math.fsum(vals)
  398. except OverflowError:
  399. self.fail("test %d failed: got OverflowError, expected %r "
  400. "for math.fsum(%.100r)" % (i, expected, vals))
  401. except ValueError:
  402. self.fail("test %d failed: got ValueError, expected %r "
  403. "for math.fsum(%.100r)" % (i, expected, vals))
  404. self.assertEqual(actual, expected)
  405. from random import random, gauss, shuffle
  406. for j in xrange(1000):
  407. vals = [7, 1e100, -7, -1e100, -9e-20, 8e-20] * 10
  408. s = 0
  409. for i in xrange(200):
  410. v = gauss(0, random()) ** 7 - s
  411. s += v
  412. vals.append(v)
  413. shuffle(vals)
  414. s = msum(vals)
  415. self.assertEqual(msum(vals), math.fsum(vals))
  416. def testHypot(self):
  417. self.assertRaises(TypeError, math.hypot)
  418. self.ftest('hypot(0,0)', math.hypot(0,0), 0)
  419. self.ftest('hypot(3,4)', math.hypot(3,4), 5)
  420. self.assertEqual(math.hypot(NAN, INF), INF)
  421. self.assertEqual(math.hypot(INF, NAN), INF)
  422. self.assertEqual(math.hypot(NAN, NINF), INF)
  423. self.assertEqual(math.hypot(NINF, NAN), INF)
  424. self.assert_(math.isnan(math.hypot(1.0, NAN)))
  425. self.assert_(math.isnan(math.hypot(NAN, -2.0)))
  426. def testLdexp(self):
  427. self.assertRaises(TypeError, math.ldexp)
  428. self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
  429. self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
  430. self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
  431. self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
  432. self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
  433. self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
  434. self.assertEquals(math.ldexp(1., -1000000), 0.)
  435. self.assertEquals(math.ldexp(-1., -1000000), -0.)
  436. self.assertEquals(math.ldexp(INF, 30), INF)
  437. self.assertEquals(math.ldexp(NINF, -213), NINF)
  438. self.assert_(math.isnan(math.ldexp(NAN, 0)))
  439. # large second argument
  440. for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
  441. self.assertEquals(math.ldexp(INF, -n), INF)
  442. self.assertEquals(math.ldexp(NINF, -n), NINF)
  443. self.assertEquals(math.ldexp(1., -n), 0.)
  444. self.assertEquals(math.ldexp(-1., -n), -0.)
  445. self.assertEquals(math.ldexp(0., -n), 0.)
  446. self.assertEquals(math.ldexp(-0., -n), -0.)
  447. self.assert_(math.isnan(math.ldexp(NAN, -n)))
  448. self.assertRaises(OverflowError, math.ldexp, 1., n)
  449. self.assertRaises(OverflowError, math.ldexp, -1., n)
  450. self.assertEquals(math.ldexp(0., n), 0.)
  451. self.assertEquals(math.ldexp(-0., n), -0.)
  452. self.assertEquals(math.ldexp(INF, n), INF)
  453. self.assertEquals(math.ldexp(NINF, n), NINF)
  454. self.assert_(math.isnan(math.ldexp(NAN, n)))
  455. def testLog(self):
  456. self.assertRaises(TypeError, math.log)
  457. self.ftest('log(1/e)', math.log(1/math.e), -1)
  458. self.ftest('log(1)', math.log(1), 0)
  459. self.ftest('log(e)', math.log(math.e), 1)
  460. self.ftest('log(32,2)', math.log(32,2), 5)
  461. self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
  462. self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
  463. self.assertEquals(math.log(INF), INF)
  464. self.assertRaises(ValueError, math.log, NINF)
  465. self.assert_(math.isnan(math.log(NAN)))
  466. def testLog1p(self):
  467. self.assertRaises(TypeError, math.log1p)
  468. self.ftest('log1p(1/e -1)', math.log1p(1/math.e-1), -1)
  469. self.ftest('log1p(0)', math.log1p(0), 0)
  470. self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
  471. self.ftest('log1p(1)', math.log1p(1), math.log(2))
  472. self.assertEquals(math.log1p(INF), INF)
  473. self.assertRaises(ValueError, math.log1p, NINF)
  474. self.assert_(math.isnan(math.log1p(NAN)))
  475. n= 2**90
  476. self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
  477. self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
  478. def testLog10(self):
  479. self.assertRaises(TypeError, math.log10)
  480. self.ftest('log10(0.1)', math.log10(0.1), -1)
  481. self.ftest('log10(1)', math.log10(1), 0)
  482. self.ftest('log10(10)', math.log10(10), 1)
  483. self.assertEquals(math.log(INF), INF)
  484. self.assertRaises(ValueError, math.log10, NINF)
  485. self.assert_(math.isnan(math.log10(NAN)))
  486. def testModf(self):
  487. self.assertRaises(TypeError, math.modf)
  488. def testmodf(name, (v1, v2), (e1, e2)):
  489. if abs(v1-e1) > eps or abs(v2-e2):
  490. self.fail('%s returned %r, expected %r'%\
  491. (name, (v1,v2), (e1,e2)))
  492. testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
  493. testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
  494. self.assertEquals(math.modf(INF), (0.0, INF))
  495. self.assertEquals(math.modf(NINF), (-0.0, NINF))
  496. modf_nan = math.modf(NAN)
  497. self.assert_(math.isnan(modf_nan[0]))
  498. self.assert_(math.isnan(modf_nan[1]))
  499. def testPow(self):
  500. self.assertRaises(TypeError, math.pow)
  501. self.ftest('pow(0,1)', math.pow(0,1), 0)
  502. self.ftest('pow(1,0)', math.pow(1,0), 1)
  503. self.ftest('pow(2,1)', math.pow(2,1), 2)
  504. self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
  505. self.assertEqual(math.pow(INF, 1), INF)
  506. self.assertEqual(math.pow(NINF, 1), NINF)
  507. self.assertEqual((math.pow(1, INF)), 1.)
  508. self.assertEqual((math.pow(1, NINF)), 1.)
  509. self.assert_(math.isnan(math.pow(NAN, 1)))
  510. self.assert_(math.isnan(math.pow(2, NAN)))
  511. self.assert_(math.isnan(math.pow(0, NAN)))
  512. self.assertEqual(math.pow(1, NAN), 1)
  513. # pow(0., x)
  514. self.assertEqual(math.pow(0., INF), 0.)
  515. self.assertEqual(math.pow(0., 3.), 0.)
  516. self.assertEqual(math.pow(0., 2.3), 0.)
  517. self.assertEqual(math.pow(0., 2.), 0.)
  518. self.assertEqual(math.pow(0., 0.), 1.)
  519. self.assertEqual(math.pow(0., -0.), 1.)
  520. self.assertRaises(ValueError, math.pow, 0., -2.)
  521. self.assertRaises(ValueError, math.pow, 0., -2.3)
  522. self.assertRaises(ValueError, math.pow, 0., -3.)
  523. self.assertRaises(ValueError, math.pow, 0., NINF)
  524. self.assert_(math.isnan(math.pow(0., NAN)))
  525. # pow(INF, x)
  526. self.assertEqual(math.pow(INF, INF), INF)
  527. self.assertEqual(math.pow(INF, 3.), INF)
  528. self.assertEqual(math.pow(INF, 2.3), INF)
  529. self.assertEqual(math.pow(INF, 2.), INF)
  530. self.assertEqual(math.pow(INF, 0.), 1.)
  531. self.assertEqual(math.pow(INF, -0.), 1.)
  532. self.assertEqual(math.pow(INF, -2.), 0.)
  533. self.assertEqual(math.pow(INF, -2.3), 0.)
  534. self.assertEqual(math.pow(INF, -3.), 0.)
  535. self.assertEqual(math.pow(INF, NINF), 0.)
  536. self.assert_(math.isnan(math.pow(INF, NAN)))
  537. # pow(-0., x)
  538. self.assertEqual(math.pow(-0., INF), 0.)
  539. self.assertEqual(math.pow(-0., 3.), -0.)
  540. self.assertEqual(math.pow(-0., 2.3), 0.)
  541. self.assertEqual(math.pow(-0., 2.), 0.)
  542. self.assertEqual(math.pow(-0., 0.), 1.)
  543. self.assertEqual(math.pow(-0., -0.), 1.)
  544. self.assertRaises(ValueError, math.pow, -0., -2.)
  545. self.assertRaises(ValueError, math.pow, -0., -2.3)
  546. self.assertRaises(ValueError, math.pow, -0., -3.)
  547. self.assertRaises(ValueError, math.pow, -0., NINF)
  548. self.assert_(math.isnan(math.pow(-0., NAN)))
  549. # pow(NINF, x)
  550. self.assertEqual(math.pow(NINF, INF), INF)
  551. self.assertEqual(math.pow(NINF, 3.), NINF)
  552. self.assertEqual(math.pow(NINF, 2.3), INF)
  553. self.assertEqual(math.pow(NINF, 2.), INF)
  554. self.assertEqual(math.pow(NINF, 0.), 1.)
  555. self.assertEqual(math.pow(NINF, -0.), 1.)
  556. self.assertEqual(math.pow(NINF, -2.), 0.)
  557. self.assertEqual(math.pow(NINF, -2.3), 0.)
  558. self.assertEqual(math.pow(NINF, -3.), -0.)
  559. self.assertEqual(math.pow(NINF, NINF), 0.)
  560. self.assert_(math.isnan(math.pow(NINF, NAN)))
  561. # pow(-1, x)
  562. self.assertEqual(math.pow(-1., INF), 1.)
  563. self.assertEqual(math.pow(-1., 3.), -1.)
  564. self.assertRaises(ValueError, math.pow, -1., 2.3)
  565. self.assertEqual(math.pow(-1., 2.), 1.)
  566. self.assertEqual(math.pow(-1., 0.), 1.)
  567. self.assertEqual(math.pow(-1., -0.), 1.)
  568. self.assertEqual(math.pow(-1., -2.), 1.)
  569. self.assertRaises(ValueError, math.pow, -1., -2.3)
  570. self.assertEqual(math.pow(-1., -3.), -1.)
  571. self.assertEqual(math.pow(-1., NINF), 1.)
  572. self.assert_(math.isnan(math.pow(-1., NAN)))
  573. # pow(1, x)
  574. self.assertEqual(math.pow(1., INF), 1.)
  575. self.assertEqual(math.pow(1., 3.), 1.)
  576. self.assertEqual(math.pow(1., 2.3), 1.)
  577. self.assertEqual(math.pow(1., 2.), 1.)
  578. self.assertEqual(math.pow(1., 0.), 1.)
  579. self.assertEqual(math.pow(1., -0.), 1.)
  580. self.assertEqual(math.pow(1., -2.), 1.)
  581. self.assertEqual(math.pow(1., -2.3), 1.)
  582. self.assertEqual(math.pow(1., -3.), 1.)
  583. self.assertEqual(math.pow(1., NINF), 1.)
  584. self.assertEqual(math.pow(1., NAN), 1.)
  585. # pow(x, 0) should be 1 for any x
  586. self.assertEqual(math.pow(2.3, 0.), 1.)
  587. self.assertEqual(math.pow(-2.3, 0.), 1.)
  588. self.assertEqual(math.pow(NAN, 0.), 1.)
  589. self.assertEqual(math.pow(2.3, -0.), 1.)
  590. self.assertEqual(math.pow(-2.3, -0.), 1.)
  591. self.assertEqual(math.pow(NAN, -0.), 1.)
  592. # pow(x, y) is invalid if x is negative and y is not integral
  593. self.assertRaises(ValueError, math.pow, -1., 2.3)
  594. self.assertRaises(ValueError, math.pow, -15., -3.1)
  595. # pow(x, NINF)
  596. self.assertEqual(math.pow(1.9, NINF), 0.)
  597. self.assertEqual(math.pow(1.1, NINF), 0.)
  598. self.assertEqual(math.pow(0.9, NINF), INF)
  599. self.assertEqual(math.pow(0.1, NINF), INF)
  600. self.assertEqual(math.pow(-0.1, NINF), INF)
  601. self.assertEqual(math.pow(-0.9, NINF), INF)
  602. self.assertEqual(math.pow(-1.1, NINF), 0.)
  603. self.assertEqual(math.pow(-1.9, NINF), 0.)
  604. # pow(x, INF)
  605. self.assertEqual(math.pow(1.9, INF), INF)
  606. self.assertEqual(math.pow(1.1, INF), INF)
  607. self.assertEqual(math.pow(0.9, INF), 0.)
  608. self.assertEqual(math.pow(0.1, INF), 0.)
  609. self.assertEqual(math.pow(-0.1, INF), 0.)
  610. self.assertEqual(math.pow(-0.9, INF), 0.)
  611. self.assertEqual(math.pow(-1.1, INF), INF)
  612. self.assertEqual(math.pow(-1.9, INF), INF)
  613. # pow(x, y) should work for x negative, y an integer
  614. self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
  615. self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
  616. self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
  617. self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
  618. self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
  619. self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
  620. self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
  621. self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
  622. self.assertRaises(ValueError, math.pow, -2.0, -0.5)
  623. self.assertRaises(ValueError, math.pow, -2.0, 0.5)
  624. # the following tests have been commented out since they don't
  625. # really belong here: the implementation of ** for floats is
  626. # independent of the implemention of math.pow
  627. #self.assertEqual(1**NAN, 1)
  628. #self.assertEqual(1**INF, 1)
  629. #self.assertEqual(1**NINF, 1)
  630. #self.assertEqual(1**0, 1)
  631. #self.assertEqual(1.**NAN, 1)
  632. #self.assertEqual(1.**INF, 1)
  633. #self.assertEqual(1.**NINF, 1)
  634. #self.assertEqual(1.**0, 1)
  635. def testRadians(self):
  636. self.assertRaises(TypeError, math.radians)
  637. self.ftest('radians(180)', math.radians(180), math.pi)
  638. self.ftest('radians(90)', math.radians(90), math.pi/2)
  639. self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
  640. def testSin(self):
  641. self.assertRaises(TypeError, math.sin)
  642. self.ftest('sin(0)', math.sin(0), 0)
  643. self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
  644. self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
  645. try:
  646. self.assert_(math.isnan(math.sin(INF)))
  647. self.assert_(math.isnan(math.sin(NINF)))
  648. except ValueError:
  649. self.assertRaises(ValueError, math.sin, INF)
  650. self.assertRaises(ValueError, math.sin, NINF)
  651. self.assert_(math.isnan(math.sin(NAN)))
  652. def testSinh(self):
  653. self.assertRaises(TypeError, math.sinh)
  654. self.ftest('sinh(0)', math.sinh(0), 0)
  655. self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
  656. self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
  657. self.assertEquals(math.sinh(INF), INF)
  658. self.assertEquals(math.sinh(NINF), NINF)
  659. self.assert_(math.isnan(math.sinh(NAN)))
  660. def testSqrt(self):
  661. self.assertRaises(TypeError, math.sqrt)
  662. self.ftest('sqrt(0)', math.sqrt(0), 0)
  663. self.ftest('sqrt(1)', math.sqrt(1), 1)
  664. self.ftest('sqrt(4)', math.sqrt(4), 2)
  665. self.assertEquals(math.sqrt(INF), INF)
  666. self.assertRaises(ValueError, math.sqrt, NINF)
  667. self.assert_(math.isnan(math.sqrt(NAN)))
  668. def testTan(self):
  669. self.assertRaises(TypeError, math.tan)
  670. self.ftest('tan(0)', math.tan(0), 0)
  671. self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
  672. self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
  673. try:
  674. self.assert_(math.isnan(math.tan(INF)))
  675. self.assert_(math.isnan(math.tan(NINF)))
  676. except:
  677. self.assertRaises(ValueError, math.tan, INF)
  678. self.assertRaises(ValueError, math.tan, NINF)
  679. self.assert_(math.isnan(math.tan(NAN)))
  680. def testTanh(self):
  681. self.assertRaises(TypeError, math.tanh)
  682. self.ftest('tanh(0)', math.tanh(0), 0)
  683. self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
  684. self.ftest('tanh(inf)', math.tanh(INF), 1)
  685. self.ftest('tanh(-inf)', math.tanh(NINF), -1)
  686. self.assert_(math.isnan(math.tanh(NAN)))
  687. # check that tanh(-0.) == -0. on IEEE 754 systems
  688. if float.__getformat__("double").startswith("IEEE"):
  689. self.assertEqual(math.tanh(-0.), -0.)
  690. self.assertEqual(math.copysign(1., math.tanh(-0.)),
  691. math.copysign(1., -0.))
  692. def test_trunc(self):
  693. self.assertEqual(math.trunc(1), 1)
  694. self.assertEqual(math.trunc(-1), -1)
  695. self.assertEqual(type(math.trunc(1)), int)
  696. self.assertEqual(type(math.trunc(1.5)), int)
  697. self.assertEqual(math.trunc(1.5), 1)
  698. self.assertEqual(math.trunc(-1.5), -1)
  699. self.assertEqual(math.trunc(1.999999), 1)
  700. self.assertEqual(math.trunc(-1.999999), -1)
  701. self.assertEqual(math.trunc(-0.999999), -0)
  702. self.assertEqual(math.trunc(-100.999), -100)
  703. class TestTrunc(object):
  704. def __trunc__(self):
  705. return 23
  706. class TestNoTrunc(object):
  707. pass
  708. self.assertEqual(math.trunc(TestTrunc()), 23)
  709. self.assertRaises(TypeError, math.trunc)
  710. self.assertRaises(TypeError, math.trunc, 1, 2)
  711. # XXX: This is not ideal, but see the comment in math_trunc().
  712. self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
  713. t = TestNoTrunc()
  714. t.__trunc__ = lambda *args: args
  715. self.assertEquals((), math.trunc(t))
  716. self.assertRaises(TypeError, math.trunc, t, 0)
  717. def testCopysign(self):
  718. self.assertEqual(math.copysign(1, 42), 1.0)
  719. self.assertEqual(math.copysign(0., 42), 0.0)
  720. self.assertEqual(math.copysign(1., -42), -1.0)
  721. self.assertEqual(math.copysign(3, 0.), 3.0)
  722. self.assertEqual(math.copysign(4., -0.), -4.0)
  723. def testIsnan(self):
  724. self.assert_(math.isnan(float("nan")))
  725. self.assert_(math.isnan(float("inf")* 0.))
  726. self.failIf(math.isnan(float("inf")))
  727. self.failIf(math.isnan(0.))
  728. self.failIf(math.isnan(1.))
  729. def testIsinf(self):
  730. self.assert_(math.isinf(float("inf")))
  731. self.assert_(math.isinf(float("-inf")))
  732. self.assert_(math.isinf(1E400))
  733. self.assert_(math.isinf(-1E400))
  734. self.failIf(math.isinf(float("nan")))
  735. self.failIf(math.isinf(0.))
  736. self.failIf(math.isinf(1.))
  737. # RED_FLAG 16-Oct-2000 Tim
  738. # While 2.0 is more consistent about exceptions than previous releases, it
  739. # still fails this part of the test on some platforms. For now, we only
  740. # *run* test_exceptions() in verbose mode, so that this isn't normally
  741. # tested.
  742. if verbose:
  743. def test_exceptions(self):
  744. try:
  745. x = math.exp(-1000000000)
  746. except:
  747. # mathmodule.c is failing to weed out underflows from libm, or
  748. # we've got an fp format with huge dynamic range
  749. self.fail("underflowing exp() should not have raised "
  750. "an exception")
  751. if x != 0:
  752. self.fail("underflowing exp() should have returned 0")
  753. # If this fails, probably using a strict IEEE-754 conforming libm, and x
  754. # is +Inf afterwards. But Python wants overflows detected by default.
  755. try:
  756. x = math.exp(1000000000)
  757. except OverflowError:
  758. pass
  759. else:
  760. self.fail("overflowing exp() didn't trigger OverflowError")
  761. # If this fails, it could be a puzzle. One odd possibility is that
  762. # mathmodule.c's macros are getting confused while comparing
  763. # Inf (HUGE_VAL) to a NaN, and artificially setting errno to ERANGE
  764. # as a result (and so raising OverflowError instead).
  765. try:
  766. x = math.sqrt(-1.0)
  767. except ValueError:
  768. pass
  769. else:
  770. self.fail("sqrt(-1) didn't raise ValueError")
  771. def test_testfile(self):
  772. if not float.__getformat__("double").startswith("IEEE"):
  773. return
  774. for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
  775. # Skip if either the input or result is complex, or if
  776. # flags is nonempty
  777. if ai != 0. or ei != 0. or flags:
  778. continue
  779. if fn in ['rect', 'polar']:
  780. # no real versions of rect, polar
  781. continue
  782. func = getattr(math, fn)
  783. try:
  784. result = func(ar)
  785. except ValueError:
  786. message = ("Unexpected ValueError in " +
  787. "test %s:%s(%r)\n" % (id, fn, ar))
  788. self.fail(message)
  789. except OverflowError:
  790. message = ("Unexpected OverflowError in " +
  791. "test %s:%s(%r)\n" % (id, fn, ar))
  792. self.fail(message)
  793. self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
  794. def test_main():
  795. from doctest import DocFileSuite
  796. suite = unittest.TestSuite()
  797. suite.addTest(unittest.makeSuite(MathTests))
  798. suite.addTest(DocFileSuite("ieee754.txt"))
  799. run_unittest(suite)
  800. if __name__ == '__main__':
  801. test_main()