931_math.py 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. print('sandbox mode, module is disabled')
  2. exit()
  3. # https://github.com/python/cpython/blob/v3.4.10/Lib/test/test_math.py
  4. # Python test set -- math module
  5. # XXXX Should not do tests around zero only
  6. import math
  7. import os
  8. import sys
  9. requires_IEEE_754 = lambda f: f
  10. eps = 1e-5
  11. NAN = float('nan')
  12. INF = float('inf')
  13. NINF = float('-inf')
  14. # detect evidence of double-rounding: fsum is not always correctly
  15. # rounded on machines that suffer from double rounding.
  16. x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
  17. HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
  18. print("HAVE_DOUBLE_ROUNDING =", HAVE_DOUBLE_ROUNDING)
  19. # locate file with test values
  20. # if __name__ == '__main__':
  21. # file = sys.argv[0]
  22. # else:
  23. # file = __file__
  24. math_testcases = 'tests/math_testcases.txt'
  25. test_file = 'tests/cmath_testcases.txt'
  26. def to_ulps(x):
  27. """Convert a non-NaN float x to an integer, in such a way that
  28. adjacent floats are converted to adjacent integers. Then
  29. abs(ulps(x) - ulps(y)) gives the difference in ulps between two
  30. floats.
  31. The results from this function will only make sense on platforms
  32. where C doubles are represented in IEEE 754 binary64 format.
  33. """
  34. n = struct.unpack('<q', struct.pack('<d', x))[0] # type: ignore
  35. if n < 0:
  36. n = ~(n+2**63)
  37. return n
  38. def ulps_check(expected, got, ulps=20):
  39. if abs(expected - got) > eps:
  40. return "error = {}; permitted error = {}".format(got - expected, eps)
  41. return
  42. """Given non-NaN floats `expected` and `got`,
  43. check that they're equal to within the given number of ulps.
  44. Returns None on success and an error message on failure."""
  45. ulps_error = to_ulps(got) - to_ulps(expected)
  46. if abs(ulps_error) <= ulps:
  47. return None
  48. return "error = {} ulps; permitted error = {} ulps".format(ulps_error,
  49. ulps)
  50. # Here's a pure Python version of the math.factorial algorithm, for
  51. # documentation and comparison purposes.
  52. #
  53. # Formula:
  54. #
  55. # factorial(n) = factorial_odd_part(n) << (n - count_set_bits(n))
  56. #
  57. # where
  58. #
  59. # factorial_odd_part(n) = product_{i >= 0} product_{0 < j <= n >> i; j odd} j
  60. #
  61. # The outer product above is an infinite product, but once i >= n.bit_length,
  62. # (n >> i) < 1 and the corresponding term of the product is empty. So only the
  63. # finitely many terms for 0 <= i < n.bit_length() contribute anything.
  64. #
  65. # We iterate downwards from i == n.bit_length() - 1 to i == 0. The inner
  66. # product in the formula above starts at 1 for i == n.bit_length(); for each i
  67. # < n.bit_length() we get the inner product for i from that for i + 1 by
  68. # multiplying by all j in {n >> i+1 < j <= n >> i; j odd}. In Python terms,
  69. # this set is range((n >> i+1) + 1 | 1, (n >> i) + 1 | 1, 2).
  70. def count_set_bits(n):
  71. """Number of '1' bits in binary expansion of a nonnnegative integer."""
  72. return 1 + count_set_bits(n & n - 1) if n else 0
  73. def partial_product(start, stop):
  74. """Product of integers in range(start, stop, 2), computed recursively.
  75. start and stop should both be odd, with start <= stop.
  76. """
  77. numfactors = (stop - start) >> 1
  78. if not numfactors:
  79. return 1
  80. elif numfactors == 1:
  81. return start
  82. else:
  83. mid = (start + numfactors) | 1
  84. return partial_product(start, mid) * partial_product(mid, stop)
  85. def py_factorial(n):
  86. """Factorial of nonnegative integer n, via "Binary Split Factorial Formula"
  87. described at http://www.luschny.de/math/factorial/binarysplitfact.html
  88. """
  89. inner = outer = 1
  90. for i in reversed(range(n.bit_length())):
  91. inner *= partial_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1)
  92. outer *= inner
  93. return outer << (n - count_set_bits(n))
  94. def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323):
  95. """Determine whether non-NaN floats a and b are equal to within a
  96. (small) rounding error. The default values for rel_err and
  97. abs_err are chosen to be suitable for platforms where a float is
  98. represented by an IEEE 754 double. They allow an error of between
  99. 9 and 19 ulps."""
  100. # need to special case infinities, since inf - inf gives nan
  101. if math.isinf(expected) and got == expected:
  102. return None
  103. error = got - expected
  104. permitted_error = max(abs_err, rel_err * abs(expected))
  105. if abs(error) < permitted_error:
  106. return None
  107. return "error = {}; permitted error = {}".format(error,
  108. permitted_error)
  109. def parse_mtestfile(fname):
  110. """Parse a file with test values
  111. -- starts a comment
  112. blank lines, or lines containing only a comment, are ignored
  113. other lines are expected to have the form
  114. id fn arg -> expected [flag]*
  115. """
  116. with open(fname, 'rt') as fp:
  117. for line in fp.read().split('\n'):
  118. # strip comments, and skip blank lines
  119. if '--' in line:
  120. line = line[:line.index('--')]
  121. if not line.strip():
  122. continue
  123. lhs, rhs = line.split('->')
  124. id, fn, arg = lhs.split()
  125. rhs_pieces = rhs.split()
  126. exp = rhs_pieces[0]
  127. flags = rhs_pieces[1:]
  128. yield (id, fn, float(arg), float(exp), flags)
  129. def parse_testfile(fname):
  130. """Parse a file with test values
  131. Empty lines or lines starting with -- are ignored
  132. yields id, fn, arg_real, arg_imag, exp_real, exp_imag
  133. """
  134. with open(fname, 'rt') as fp:
  135. for line in fp.read().split('\n'):
  136. # skip comment lines and blank lines
  137. if line.startswith('--') or not line.strip():
  138. continue
  139. lhs, rhs = line.split('->')
  140. id, fn, arg_real, arg_imag = lhs.split()
  141. rhs_pieces = rhs.split()
  142. exp_real, exp_imag = rhs_pieces[0], rhs_pieces[1]
  143. flags = rhs_pieces[2:]
  144. yield (id, fn,
  145. float(arg_real), float(arg_imag),
  146. float(exp_real), float(exp_imag),
  147. flags
  148. )
  149. class TestCase:
  150. def fail(self, msg):
  151. print(msg)
  152. # assert False
  153. exit(1)
  154. def assertEqual(self, a, b):
  155. if a != b:
  156. self.fail(f'{a!r} != {b!r}')
  157. def assertAlmostEqual(self, a, b):
  158. tol = eps
  159. if abs(a-b) > tol:
  160. self.fail(f'{a!r} != {b!r} within {tol!r}')
  161. def assertRaises(self, exc, func, *args, **kwargs):
  162. try:
  163. func(*args, **kwargs)
  164. self.fail(f'Expected {exc} but no exception was raised')
  165. except exc:
  166. return
  167. except Exception as e:
  168. self.fail(f'Expected {exc} but got {type(e)}: {e}')
  169. def assertNaN(self, x):
  170. if not math.isnan(x):
  171. self.fail(f'{x!r} is not NaN')
  172. def assertTrue(self, x):
  173. if not x:
  174. self.fail(f'{x!r} is not true')
  175. def assertFalse(self, x):
  176. if x:
  177. self.fail(f'{x!r} is not false')
  178. def assertIs(self, a, b):
  179. if a is not b:
  180. self.fail(f'{a!r} is not {b!r}')
  181. class TestCeil:
  182. def __ceil__(self):
  183. return 42
  184. class TestNoCeil:
  185. pass
  186. class TestFloor:
  187. def __floor__(self):
  188. return 42
  189. class TestNoFloor:
  190. pass
  191. class TestTrunc(object):
  192. def __trunc__(self):
  193. return 23
  194. class TestNoTrunc(object):
  195. pass
  196. class MathTests(TestCase):
  197. def ftest(self, name, value, expected):
  198. if abs(value-expected) > eps:
  199. # Use %r instead of %f so the error message
  200. # displays full precision. Otherwise discrepancies
  201. # in the last few bits will lead to very confusing
  202. # error messages
  203. self.fail('%s returned %r, expected %r' %
  204. (name, value, expected))
  205. def testConstants(self):
  206. self.ftest('pi', math.pi, 3.1415926)
  207. self.ftest('e', math.e, 2.7182818)
  208. def testAcos(self):
  209. self.assertRaises(TypeError, math.acos)
  210. self.ftest('acos(-1)', math.acos(-1), math.pi)
  211. self.ftest('acos(0)', math.acos(0), math.pi/2)
  212. self.ftest('acos(1)', math.acos(1), 0)
  213. self.assertNaN(math.acos(INF))
  214. self.assertNaN(math.acos(NINF))
  215. self.assertTrue(math.isnan(math.acos(NAN)))
  216. def testAcosh(self):
  217. return
  218. self.assertRaises(TypeError, math.acosh)
  219. self.ftest('acosh(1)', math.acosh(1), 0)
  220. self.ftest('acosh(2)', math.acosh(2), 1.3169578969248168)
  221. self.assertRaises(ValueError, math.acosh, 0)
  222. self.assertRaises(ValueError, math.acosh, -1)
  223. self.assertEqual(math.acosh(INF), INF)
  224. self.assertRaises(ValueError, math.acosh, NINF)
  225. self.assertTrue(math.isnan(math.acosh(NAN)))
  226. def testAsin(self):
  227. self.assertRaises(TypeError, math.asin)
  228. self.ftest('asin(-1)', math.asin(-1), -math.pi/2)
  229. self.ftest('asin(0)', math.asin(0), 0)
  230. self.ftest('asin(1)', math.asin(1), math.pi/2)
  231. self.assertNaN(math.asin(INF))
  232. self.assertNaN(math.asin(NINF))
  233. self.assertTrue(math.isnan(math.asin(NAN)))
  234. def testAsinh(self):
  235. return
  236. self.assertRaises(TypeError, math.asinh)
  237. self.ftest('asinh(0)', math.asinh(0), 0)
  238. self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
  239. self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
  240. self.assertEqual(math.asinh(INF), INF)
  241. self.assertEqual(math.asinh(NINF), NINF)
  242. self.assertTrue(math.isnan(math.asinh(NAN)))
  243. def testAtan(self):
  244. self.assertRaises(TypeError, math.atan)
  245. self.ftest('atan(-1)', math.atan(-1), -math.pi/4)
  246. self.ftest('atan(0)', math.atan(0), 0)
  247. self.ftest('atan(1)', math.atan(1), math.pi/4)
  248. self.ftest('atan(inf)', math.atan(INF), math.pi/2)
  249. self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
  250. self.assertTrue(math.isnan(math.atan(NAN)))
  251. def testAtanh(self):
  252. return
  253. self.assertRaises(TypeError, math.atan)
  254. self.ftest('atanh(0)', math.atanh(0), 0)
  255. self.ftest('atanh(0.5)', math.atanh(0.5), 0.54930614433405489)
  256. self.ftest('atanh(-0.5)', math.atanh(-0.5), -0.54930614433405489)
  257. self.assertRaises(ValueError, math.atanh, 1)
  258. self.assertRaises(ValueError, math.atanh, -1)
  259. self.assertRaises(ValueError, math.atanh, INF)
  260. self.assertRaises(ValueError, math.atanh, NINF)
  261. self.assertTrue(math.isnan(math.atanh(NAN)))
  262. def testAtan2(self):
  263. self.assertRaises(TypeError, math.atan2)
  264. self.ftest('atan2(-1, 0)', math.atan2(-1, 0), -math.pi/2)
  265. self.ftest('atan2(-1, 1)', math.atan2(-1, 1), -math.pi/4)
  266. self.ftest('atan2(0, 1)', math.atan2(0, 1), 0)
  267. self.ftest('atan2(1, 1)', math.atan2(1, 1), math.pi/4)
  268. self.ftest('atan2(1, 0)', math.atan2(1, 0), math.pi/2)
  269. # math.atan2(0, x)
  270. self.ftest('atan2(0., -inf)', math.atan2(0., NINF), math.pi)
  271. self.ftest('atan2(0., -2.3)', math.atan2(0., -2.3), math.pi)
  272. self.ftest('atan2(0., -0.)', math.atan2(0., -0.), math.pi)
  273. self.assertEqual(math.atan2(0., 0.), 0.)
  274. self.assertEqual(math.atan2(0., 2.3), 0.)
  275. self.assertEqual(math.atan2(0., INF), 0.)
  276. self.assertTrue(math.isnan(math.atan2(0., NAN)))
  277. # math.atan2(-0, x)
  278. self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
  279. self.ftest('atan2(-0., -2.3)', math.atan2(-0., -2.3), -math.pi)
  280. self.ftest('atan2(-0., -0.)', math.atan2(-0., -0.), -math.pi)
  281. self.assertEqual(math.atan2(-0., 0.), -0.)
  282. self.assertEqual(math.atan2(-0., 2.3), -0.)
  283. self.assertEqual(math.atan2(-0., INF), -0.)
  284. self.assertTrue(math.isnan(math.atan2(-0., NAN)))
  285. # math.atan2(INF, x)
  286. self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
  287. self.ftest('atan2(inf, -2.3)', math.atan2(INF, -2.3), math.pi/2)
  288. self.ftest('atan2(inf, -0.)', math.atan2(INF, -0.0), math.pi/2)
  289. self.ftest('atan2(inf, 0.)', math.atan2(INF, 0.0), math.pi/2)
  290. self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
  291. self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
  292. self.assertTrue(math.isnan(math.atan2(INF, NAN)))
  293. # math.atan2(NINF, x)
  294. self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
  295. self.ftest('atan2(-inf, -2.3)', math.atan2(NINF, -2.3), -math.pi/2)
  296. self.ftest('atan2(-inf, -0.)', math.atan2(NINF, -0.0), -math.pi/2)
  297. self.ftest('atan2(-inf, 0.)', math.atan2(NINF, 0.0), -math.pi/2)
  298. self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
  299. self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
  300. self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
  301. # math.atan2(+finite, x)
  302. self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
  303. self.ftest('atan2(2.3, -0.)', math.atan2(2.3, -0.), math.pi/2)
  304. self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
  305. self.assertEqual(math.atan2(2.3, INF), 0.)
  306. self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
  307. # math.atan2(-finite, x)
  308. self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
  309. self.ftest('atan2(-2.3, -0.)', math.atan2(-2.3, -0.), -math.pi/2)
  310. self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
  311. self.assertEqual(math.atan2(-2.3, INF), -0.)
  312. self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
  313. # math.atan2(NAN, x)
  314. self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
  315. self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
  316. self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
  317. self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
  318. self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
  319. self.assertTrue(math.isnan(math.atan2(NAN, INF)))
  320. self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
  321. def testCeil(self):
  322. self.assertRaises(TypeError, math.ceil)
  323. self.assertEqual(int, type(math.ceil(0.5)))
  324. self.ftest('ceil(0.5)', math.ceil(0.5), 1)
  325. self.ftest('ceil(1.0)', math.ceil(1.0), 1)
  326. self.ftest('ceil(1.5)', math.ceil(1.5), 2)
  327. self.ftest('ceil(-0.5)', math.ceil(-0.5), 0)
  328. self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
  329. self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
  330. #self.assertEqual(math.ceil(INF), INF)
  331. #self.assertEqual(math.ceil(NINF), NINF)
  332. #self.assertTrue(math.isnan(math.ceil(NAN)))
  333. if 0:
  334. self.ftest('ceil(TestCeil())', math.ceil(TestCeil()), 42)
  335. self.assertRaises(TypeError, math.ceil, TestNoCeil())
  336. t = TestNoCeil()
  337. t.__ceil__ = lambda *args: args # type: ignore
  338. self.assertRaises(TypeError, math.ceil, t)
  339. self.assertRaises(TypeError, math.ceil, t, 0)
  340. @requires_IEEE_754
  341. def testCopysign(self):
  342. self.assertEqual(math.copysign(1, 42), 1.0)
  343. self.assertEqual(math.copysign(0., 42), 0.0)
  344. self.assertEqual(math.copysign(1., -42), -1.0)
  345. self.assertEqual(math.copysign(3, 0.), 3.0)
  346. self.assertEqual(math.copysign(4., -0.), -4.0)
  347. self.assertRaises(TypeError, math.copysign)
  348. # copysign should let us distinguish signs of zeros
  349. self.assertEqual(math.copysign(1., 0.), 1.)
  350. self.assertEqual(math.copysign(1., -0.), -1.)
  351. self.assertEqual(math.copysign(INF, 0.), INF)
  352. self.assertEqual(math.copysign(INF, -0.), NINF)
  353. self.assertEqual(math.copysign(NINF, 0.), INF)
  354. self.assertEqual(math.copysign(NINF, -0.), NINF)
  355. # and of infinities
  356. self.assertEqual(math.copysign(1., INF), 1.)
  357. self.assertEqual(math.copysign(1., NINF), -1.)
  358. self.assertEqual(math.copysign(INF, INF), INF)
  359. self.assertEqual(math.copysign(INF, NINF), NINF)
  360. self.assertEqual(math.copysign(NINF, INF), INF)
  361. self.assertEqual(math.copysign(NINF, NINF), NINF)
  362. self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
  363. self.assertTrue(math.isnan(math.copysign(NAN, INF)))
  364. self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
  365. self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
  366. # copysign(INF, NAN) may be INF or it may be NINF, since
  367. # we don't know whether the sign bit of NAN is set on any
  368. # given platform.
  369. self.assertTrue(math.isinf(math.copysign(INF, NAN)))
  370. # similarly, copysign(2., NAN) could be 2. or -2.
  371. self.assertEqual(abs(math.copysign(2., NAN)), 2.)
  372. def testCos(self):
  373. self.assertRaises(TypeError, math.cos)
  374. self.ftest('cos(-pi/2)', math.cos(-math.pi/2), 0)
  375. self.ftest('cos(0)', math.cos(0), 1)
  376. self.ftest('cos(pi/2)', math.cos(math.pi/2), 0)
  377. self.ftest('cos(pi)', math.cos(math.pi), -1)
  378. try:
  379. self.assertTrue(math.isnan(math.cos(INF)))
  380. self.assertTrue(math.isnan(math.cos(NINF)))
  381. except ValueError:
  382. self.assertRaises(ValueError, math.cos, INF)
  383. self.assertRaises(ValueError, math.cos, NINF)
  384. self.assertTrue(math.isnan(math.cos(NAN)))
  385. def testCosh(self):
  386. return
  387. self.assertRaises(TypeError, math.cosh)
  388. self.ftest('cosh(0)', math.cosh(0), 1)
  389. self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
  390. self.assertEqual(math.cosh(INF), INF)
  391. self.assertEqual(math.cosh(NINF), INF)
  392. self.assertTrue(math.isnan(math.cosh(NAN)))
  393. def testDegrees(self):
  394. self.assertRaises(TypeError, math.degrees)
  395. self.ftest('degrees(pi)', math.degrees(math.pi), 180.0)
  396. self.ftest('degrees(pi/2)', math.degrees(math.pi/2), 90.0)
  397. self.ftest('degrees(-pi/4)', math.degrees(-math.pi/4), -45.0)
  398. def testExp(self):
  399. self.assertRaises(TypeError, math.exp)
  400. self.ftest('exp(-1)', math.exp(-1), 1/math.e)
  401. self.ftest('exp(0)', math.exp(0), 1)
  402. self.ftest('exp(1)', math.exp(1), math.e)
  403. self.assertEqual(math.exp(INF), INF)
  404. self.assertEqual(math.exp(NINF), 0.)
  405. self.assertTrue(math.isnan(math.exp(NAN)))
  406. def testFabs(self):
  407. self.assertRaises(TypeError, math.fabs)
  408. self.ftest('fabs(-1)', math.fabs(-1), 1)
  409. self.ftest('fabs(0)', math.fabs(0), 0)
  410. self.ftest('fabs(1)', math.fabs(1), 1)
  411. def testFactorial(self):
  412. self.assertEqual(math.factorial(0), 1)
  413. # self.assertEqual(math.factorial(0.0), 1)
  414. total = 1
  415. for i in range(1, 20):
  416. total *= i
  417. self.assertEqual(math.factorial(i), total)
  418. # self.assertEqual(math.factorial(float(i)), total)
  419. self.assertEqual(math.factorial(i), py_factorial(i))
  420. self.assertRaises(ValueError, math.factorial, -1)
  421. # self.assertRaises(ValueError, math.factorial, -1.0)
  422. # self.assertRaises(ValueError, math.factorial, math.pi)
  423. # self.assertRaises(OverflowError, math.factorial, sys.maxsize+1)
  424. # self.assertRaises(OverflowError, math.factorial, 10e100)
  425. def testFloor(self):
  426. self.assertRaises(TypeError, math.floor)
  427. self.assertEqual(int, type(math.floor(0.5)))
  428. self.ftest('floor(0.5)', math.floor(0.5), 0)
  429. self.ftest('floor(1.0)', math.floor(1.0), 1)
  430. self.ftest('floor(1.5)', math.floor(1.5), 1)
  431. self.ftest('floor(-0.5)', math.floor(-0.5), -1)
  432. self.ftest('floor(-1.0)', math.floor(-1.0), -1)
  433. self.ftest('floor(-1.5)', math.floor(-1.5), -2)
  434. # pow() relies on floor() to check for integers
  435. # This fails on some platforms - so check it here
  436. # self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
  437. # self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
  438. #self.assertEqual(math.ceil(INF), INF)
  439. #self.assertEqual(math.ceil(NINF), NINF)
  440. #self.assertTrue(math.isnan(math.floor(NAN)))
  441. if 0:
  442. self.ftest('floor(TestFloor())', math.floor(TestFloor()), 42)
  443. self.assertRaises(TypeError, math.floor, TestNoFloor())
  444. t = TestNoFloor()
  445. t.__floor__ = lambda *args: args # type: ignore
  446. self.assertRaises(TypeError, math.floor, t)
  447. self.assertRaises(TypeError, math.floor, t, 0)
  448. def testFmod(self):
  449. self.assertRaises(TypeError, math.fmod)
  450. self.ftest('fmod(10, 1)', math.fmod(10, 1), 0.0)
  451. self.ftest('fmod(10, 0.5)', math.fmod(10, 0.5), 0.0)
  452. self.ftest('fmod(10, 1.5)', math.fmod(10, 1.5), 1.0)
  453. self.ftest('fmod(-10, 1)', math.fmod(-10, 1), -0.0)
  454. self.ftest('fmod(-10, 0.5)', math.fmod(-10, 0.5), -0.0)
  455. self.ftest('fmod(-10, 1.5)', math.fmod(-10, 1.5), -1.0)
  456. self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
  457. self.assertTrue(math.isnan(math.fmod(1., NAN)))
  458. self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
  459. self.assertNaN(math.fmod(1., 0.))
  460. self.assertNaN(math.fmod(INF, 1.))
  461. self.assertNaN(math.fmod(NINF, 1.))
  462. self.assertNaN(math.fmod(INF, 0.))
  463. self.assertEqual(math.fmod(3.0, INF), 3.0)
  464. self.assertEqual(math.fmod(-3.0, INF), -3.0)
  465. self.assertEqual(math.fmod(3.0, NINF), 3.0)
  466. self.assertEqual(math.fmod(-3.0, NINF), -3.0)
  467. self.assertEqual(math.fmod(0.0, 3.0), 0.0)
  468. self.assertEqual(math.fmod(0.0, NINF), 0.0)
  469. def testFrexp(self):
  470. return
  471. self.assertRaises(TypeError, math.frexp)
  472. def testfrexp(name, result, expected):
  473. (mant, exp), (emant, eexp) = result, expected
  474. if abs(mant-emant) > eps or exp != eexp:
  475. self.fail('%s returned %r, expected %r'%\
  476. (name, result, expected))
  477. testfrexp('frexp(-1)', math.frexp(-1), (-0.5, 1))
  478. testfrexp('frexp(0)', math.frexp(0), (0, 0))
  479. testfrexp('frexp(1)', math.frexp(1), (0.5, 1))
  480. testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
  481. self.assertEqual(math.frexp(INF)[0], INF)
  482. self.assertEqual(math.frexp(NINF)[0], NINF)
  483. self.assertTrue(math.isnan(math.frexp(NAN)[0]))
  484. @requires_IEEE_754
  485. def testFsum(self):
  486. return
  487. def testHypot(self):
  488. return
  489. self.assertRaises(TypeError, math.hypot)
  490. self.ftest('hypot(0,0)', math.hypot(0,0), 0)
  491. self.ftest('hypot(3,4)', math.hypot(3,4), 5)
  492. self.assertEqual(math.hypot(NAN, INF), INF)
  493. self.assertEqual(math.hypot(INF, NAN), INF)
  494. self.assertEqual(math.hypot(NAN, NINF), INF)
  495. self.assertEqual(math.hypot(NINF, NAN), INF)
  496. self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
  497. self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
  498. def testLdexp(self):
  499. return
  500. self.assertRaises(TypeError, math.ldexp)
  501. self.ftest('ldexp(0,1)', math.ldexp(0,1), 0)
  502. self.ftest('ldexp(1,1)', math.ldexp(1,1), 2)
  503. self.ftest('ldexp(1,-1)', math.ldexp(1,-1), 0.5)
  504. self.ftest('ldexp(-1,1)', math.ldexp(-1,1), -2)
  505. self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
  506. self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
  507. self.assertEqual(math.ldexp(1., -1000000), 0.)
  508. self.assertEqual(math.ldexp(-1., -1000000), -0.)
  509. self.assertEqual(math.ldexp(INF, 30), INF)
  510. self.assertEqual(math.ldexp(NINF, -213), NINF)
  511. self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
  512. # large second argument
  513. for n in [10**5, 10**10, 10**20, 10**40]:
  514. self.assertEqual(math.ldexp(INF, -n), INF)
  515. self.assertEqual(math.ldexp(NINF, -n), NINF)
  516. self.assertEqual(math.ldexp(1., -n), 0.)
  517. self.assertEqual(math.ldexp(-1., -n), -0.)
  518. self.assertEqual(math.ldexp(0., -n), 0.)
  519. self.assertEqual(math.ldexp(-0., -n), -0.)
  520. self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
  521. self.assertRaises(OverflowError, math.ldexp, 1., n)
  522. self.assertRaises(OverflowError, math.ldexp, -1., n)
  523. self.assertEqual(math.ldexp(0., n), 0.)
  524. self.assertEqual(math.ldexp(-0., n), -0.)
  525. self.assertEqual(math.ldexp(INF, n), INF)
  526. self.assertEqual(math.ldexp(NINF, n), NINF)
  527. self.assertTrue(math.isnan(math.ldexp(NAN, n)))
  528. def testLog(self):
  529. self.assertRaises(TypeError, math.log)
  530. self.ftest('log(1/e)', math.log(1/math.e), -1)
  531. self.ftest('log(1)', math.log(1), 0)
  532. self.ftest('log(e)', math.log(math.e), 1)
  533. self.ftest('log(32,2)', math.log(32,2), 5)
  534. self.ftest('log(10**4, 10)', math.log(10**4, 10), 4)
  535. # self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
  536. # self.ftest('log(10**1000)', math.log(10**1000), 2302.5850929940457)
  537. self.assertNaN(math.log(-1.5))
  538. self.assertNaN(math.log(-10**10))
  539. self.assertNaN(math.log(NINF))
  540. self.assertEqual(math.log(INF), INF)
  541. self.assertTrue(math.isnan(math.log(NAN)))
  542. def testLog1p(self):
  543. return
  544. self.assertRaises(TypeError, math.log1p)
  545. n= 2**90
  546. self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
  547. @requires_IEEE_754
  548. def testLog2(self):
  549. self.assertRaises(TypeError, math.log2)
  550. # Check some integer values
  551. self.assertEqual(math.log2(1), 0.0)
  552. self.assertEqual(math.log2(2), 1.0)
  553. self.assertEqual(math.log2(4), 2.0)
  554. # Large integer values
  555. self.assertEqual(math.log2(2**23), 23.0)
  556. self.assertEqual(math.log2(2**24), 24.0)
  557. self.assertEqual(math.log2(2**20), 20.0)
  558. self.assertNaN(math.log2(-1.5))
  559. self.assertNaN(math.log2(NINF))
  560. self.assertNaN(math.log2(NAN))
  561. @requires_IEEE_754
  562. # log2() is not accurate enough on Mac OS X Tiger (10.4)
  563. # @support.requires_mac_ver(10, 5)
  564. def testLog2Exact(self):
  565. return
  566. # Check that we get exact equality for log2 of powers of 2.
  567. actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
  568. expected = [float(n) for n in range(-1074, 1024)]
  569. self.assertEqual(actual, expected)
  570. def testLog10(self):
  571. self.assertRaises(TypeError, math.log10)
  572. self.ftest('log10(0.1)', math.log10(0.1), -1)
  573. self.ftest('log10(1)', math.log10(1), 0)
  574. self.ftest('log10(10)', math.log10(10), 1)
  575. self.ftest('log10(10**4)', math.log10(10**4), 4)
  576. self.assertNaN(math.log10(-1.5))
  577. self.assertNaN(math.log10(-10**4))
  578. self.assertNaN(math.log10(NINF))
  579. self.assertEqual(math.log(INF), INF)
  580. self.assertTrue(math.isnan(math.log10(NAN)))
  581. def testModf(self):
  582. self.assertRaises(TypeError, math.modf)
  583. def testmodf(name, result, expected):
  584. (v1, v2), (e1, e2) = result, expected
  585. if abs(v1-e1) > eps or abs(v2-e2):
  586. self.fail('%s returned %r, expected %r'%\
  587. (name, result, expected))
  588. testmodf('modf(1.5)', math.modf(1.5), (0.5, 1.0))
  589. testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
  590. self.assertEqual(math.modf(INF), (0.0, INF))
  591. self.assertEqual(math.modf(NINF), (-0.0, NINF))
  592. modf_nan = math.modf(NAN)
  593. self.assertTrue(math.isnan(modf_nan[0]))
  594. self.assertTrue(math.isnan(modf_nan[1]))
  595. def testPow(self):
  596. self.assertRaises(TypeError, math.pow)
  597. self.ftest('pow(0,1)', math.pow(0,1), 0)
  598. self.ftest('pow(1,0)', math.pow(1,0), 1)
  599. self.ftest('pow(2,1)', math.pow(2,1), 2)
  600. self.ftest('pow(2,-1)', math.pow(2,-1), 0.5)
  601. self.assertEqual(math.pow(INF, 1), INF)
  602. self.assertEqual(math.pow(NINF, 1), NINF)
  603. self.assertEqual((math.pow(1, INF)), 1.)
  604. self.assertEqual((math.pow(1, NINF)), 1.)
  605. self.assertTrue(math.isnan(math.pow(NAN, 1)))
  606. self.assertTrue(math.isnan(math.pow(2, NAN)))
  607. self.assertTrue(math.isnan(math.pow(0, NAN)))
  608. self.assertEqual(math.pow(1, NAN), 1)
  609. # pow(0., x)
  610. self.assertEqual(math.pow(0., INF), 0.)
  611. self.assertEqual(math.pow(0., 3.), 0.)
  612. self.assertEqual(math.pow(0., 2.3), 0.)
  613. self.assertEqual(math.pow(0., 2.), 0.)
  614. self.assertEqual(math.pow(0., 0.), 1.)
  615. self.assertEqual(math.pow(0., -0.), 1.)
  616. # self.assertRaises(ValueError, math.pow, 0., -2.)
  617. # self.assertRaises(ValueError, math.pow, 0., -2.3)
  618. # self.assertRaises(ValueError, math.pow, 0., -3.)
  619. # self.assertRaises(ValueError, math.pow, 0., NINF)
  620. self.assertTrue(math.isnan(math.pow(0., -2.)))
  621. self.assertTrue(math.isnan(math.pow(0., -2.3)))
  622. self.assertTrue(math.isnan(math.pow(0., -3.)))
  623. self.assertTrue(math.isnan(math.pow(0., NINF)))
  624. self.assertTrue(math.isnan(math.pow(0., NAN)))
  625. # pow(INF, x)
  626. self.assertEqual(math.pow(INF, INF), INF)
  627. self.assertEqual(math.pow(INF, 3.), INF)
  628. self.assertEqual(math.pow(INF, 2.3), INF)
  629. self.assertEqual(math.pow(INF, 2.), INF)
  630. self.assertEqual(math.pow(INF, 0.), 1.)
  631. self.assertEqual(math.pow(INF, -0.), 1.)
  632. self.assertEqual(math.pow(INF, -2.), 0.)
  633. self.assertEqual(math.pow(INF, -2.3), 0.)
  634. self.assertEqual(math.pow(INF, -3.), 0.)
  635. self.assertEqual(math.pow(INF, NINF), 0.)
  636. self.assertTrue(math.isnan(math.pow(INF, NAN)))
  637. # pow(-0., x)
  638. self.assertEqual(math.pow(-0., INF), 0.)
  639. self.assertEqual(math.pow(-0., 3.), -0.)
  640. self.assertEqual(math.pow(-0., 2.3), 0.)
  641. self.assertEqual(math.pow(-0., 2.), 0.)
  642. self.assertEqual(math.pow(-0., 0.), 1.)
  643. self.assertEqual(math.pow(-0., -0.), 1.)
  644. # self.assertRaises(ValueError, math.pow, -0., -2.)
  645. # self.assertRaises(ValueError, math.pow, -0., -2.3)
  646. # self.assertRaises(ValueError, math.pow, -0., -3.)
  647. # self.assertRaises(ValueError, math.pow, -0., NINF)
  648. self.assertTrue(math.isnan(math.pow(-0., NAN)))
  649. # pow(NINF, x)
  650. if 0:
  651. self.assertEqual(math.pow(NINF, INF), INF)
  652. self.assertEqual(math.pow(NINF, 3.), NINF)
  653. self.assertEqual(math.pow(NINF, 2.3), INF)
  654. self.assertEqual(math.pow(NINF, 2.), INF)
  655. self.assertEqual(math.pow(NINF, 0.), 1.)
  656. self.assertEqual(math.pow(NINF, -0.), 1.)
  657. self.assertEqual(math.pow(NINF, -2.), 0.)
  658. self.assertEqual(math.pow(NINF, -2.3), 0.)
  659. self.assertEqual(math.pow(NINF, -3.), -0.)
  660. self.assertEqual(math.pow(NINF, NINF), 0.)
  661. self.assertTrue(math.isnan(math.pow(NINF, NAN)))
  662. # pow(-1, x)
  663. self.assertEqual(math.pow(-1., 3.), -1.)
  664. # self.assertRaises(ValueError, math.pow, -1., 2.3)
  665. self.assertTrue(math.isnan(math.pow(-1., 2.3)))
  666. self.assertEqual(math.pow(-1., 2.), 1.)
  667. self.assertEqual(math.pow(-1., 0.), 1.)
  668. self.assertEqual(math.pow(-1., -0.), 1.)
  669. self.assertEqual(math.pow(-1., -2.), 1.)
  670. # self.assertRaises(ValueError, math.pow, -1., -2.3)
  671. self.assertTrue(math.isnan(math.pow(-1., -2.3)))
  672. self.assertEqual(math.pow(-1., -3.), -1.)
  673. self.assertTrue(math.isnan(math.pow(-1., NAN)))
  674. # pow(1, x)
  675. self.assertEqual(math.pow(1., INF), 1.)
  676. self.assertEqual(math.pow(1., 3.), 1.)
  677. self.assertEqual(math.pow(1., 2.3), 1.)
  678. self.assertEqual(math.pow(1., 2.), 1.)
  679. self.assertEqual(math.pow(1., 0.), 1.)
  680. self.assertEqual(math.pow(1., -0.), 1.)
  681. self.assertEqual(math.pow(1., -2.), 1.)
  682. self.assertEqual(math.pow(1., -2.3), 1.)
  683. self.assertEqual(math.pow(1., -3.), 1.)
  684. self.assertEqual(math.pow(1., NINF), 1.)
  685. self.assertEqual(math.pow(1., NAN), 1.)
  686. # pow(x, 0) should be 1 for any x
  687. self.assertEqual(math.pow(2.3, 0.), 1.)
  688. self.assertEqual(math.pow(-2.3, 0.), 1.)
  689. self.assertEqual(math.pow(NAN, 0.), 1.)
  690. self.assertEqual(math.pow(2.3, -0.), 1.)
  691. self.assertEqual(math.pow(-2.3, -0.), 1.)
  692. self.assertEqual(math.pow(NAN, -0.), 1.)
  693. # pow(x, y) is invalid if x is negative and y is not integral
  694. # self.assertRaises(ValueError, math.pow, -1., 2.3)
  695. # self.assertRaises(ValueError, math.pow, -15., -3.1)
  696. self.assertTrue(math.isnan(math.pow(-1., 2.3)))
  697. self.assertTrue(math.isnan(math.pow(-15., -3.1)))
  698. # pow(x, NINF)
  699. self.assertEqual(math.pow(1.9, NINF), 0.)
  700. self.assertEqual(math.pow(1.1, NINF), 0.)
  701. self.assertEqual(math.pow(0.9, NINF), INF)
  702. self.assertEqual(math.pow(0.1, NINF), INF)
  703. # self.assertEqual(math.pow(-0.1, NINF), INF)
  704. # self.assertEqual(math.pow(-0.9, NINF), INF)
  705. # self.assertEqual(math.pow(-1.1, NINF), 0.)
  706. # self.assertEqual(math.pow(-1.9, NINF), 0.)
  707. # pow(x, INF)
  708. self.assertEqual(math.pow(1.9, INF), INF)
  709. self.assertEqual(math.pow(1.1, INF), INF)
  710. self.assertEqual(math.pow(0.9, INF), 0.)
  711. self.assertEqual(math.pow(0.1, INF), 0.)
  712. # self.assertEqual(math.pow(-0.1, INF), 0.)
  713. # self.assertEqual(math.pow(-0.9, INF), 0.)
  714. # self.assertEqual(math.pow(-1.1, INF), INF)
  715. # self.assertEqual(math.pow(-1.9, INF), INF)
  716. # pow(x, y) should work for x negative, y an integer
  717. self.ftest('(-2.)**3.', math.pow(-2.0, 3.0), -8.0)
  718. self.ftest('(-2.)**2.', math.pow(-2.0, 2.0), 4.0)
  719. self.ftest('(-2.)**1.', math.pow(-2.0, 1.0), -2.0)
  720. self.ftest('(-2.)**0.', math.pow(-2.0, 0.0), 1.0)
  721. self.ftest('(-2.)**-0.', math.pow(-2.0, -0.0), 1.0)
  722. self.ftest('(-2.)**-1.', math.pow(-2.0, -1.0), -0.5)
  723. self.ftest('(-2.)**-2.', math.pow(-2.0, -2.0), 0.25)
  724. self.ftest('(-2.)**-3.', math.pow(-2.0, -3.0), -0.125)
  725. # self.assertRaises(ValueError, math.pow, -2.0, -0.5)
  726. # self.assertRaises(ValueError, math.pow, -2.0, 0.5)
  727. # the following tests have been commented out since they don't
  728. # really belong here: the implementation of ** for floats is
  729. # independent of the implementation of math.pow
  730. #self.assertEqual(1**NAN, 1)
  731. #self.assertEqual(1**INF, 1)
  732. #self.assertEqual(1**NINF, 1)
  733. #self.assertEqual(1**0, 1)
  734. #self.assertEqual(1.**NAN, 1)
  735. #self.assertEqual(1.**INF, 1)
  736. #self.assertEqual(1.**NINF, 1)
  737. #self.assertEqual(1.**0, 1)
  738. def testRadians(self):
  739. self.assertRaises(TypeError, math.radians)
  740. self.ftest('radians(180)', math.radians(180), math.pi)
  741. self.ftest('radians(90)', math.radians(90), math.pi/2)
  742. self.ftest('radians(-45)', math.radians(-45), -math.pi/4)
  743. def testSin(self):
  744. self.assertRaises(TypeError, math.sin)
  745. self.ftest('sin(0)', math.sin(0), 0)
  746. self.ftest('sin(pi/2)', math.sin(math.pi/2), 1)
  747. self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
  748. try:
  749. self.assertTrue(math.isnan(math.sin(INF)))
  750. self.assertTrue(math.isnan(math.sin(NINF)))
  751. except ValueError:
  752. self.assertRaises(ValueError, math.sin, INF)
  753. self.assertRaises(ValueError, math.sin, NINF)
  754. self.assertTrue(math.isnan(math.sin(NAN)))
  755. def testSinh(self):
  756. return
  757. self.assertRaises(TypeError, math.sinh)
  758. self.ftest('sinh(0)', math.sinh(0), 0)
  759. self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
  760. self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
  761. self.assertEqual(math.sinh(INF), INF)
  762. self.assertEqual(math.sinh(NINF), NINF)
  763. self.assertTrue(math.isnan(math.sinh(NAN)))
  764. def testSqrt(self):
  765. self.assertRaises(TypeError, math.sqrt)
  766. self.ftest('sqrt(0)', math.sqrt(0), 0)
  767. self.ftest('sqrt(1)', math.sqrt(1), 1)
  768. self.ftest('sqrt(4)', math.sqrt(4), 2)
  769. self.assertEqual(math.sqrt(INF), INF)
  770. self.assertNaN(math.sqrt(NINF))
  771. self.assertNaN(math.sqrt(NAN))
  772. def testTan(self):
  773. self.assertRaises(TypeError, math.tan)
  774. self.ftest('tan(0)', math.tan(0), 0)
  775. self.ftest('tan(pi/4)', math.tan(math.pi/4), 1)
  776. self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
  777. try:
  778. self.assertTrue(math.isnan(math.tan(INF)))
  779. self.assertTrue(math.isnan(math.tan(NINF)))
  780. except:
  781. self.assertRaises(ValueError, math.tan, INF)
  782. self.assertRaises(ValueError, math.tan, NINF)
  783. self.assertTrue(math.isnan(math.tan(NAN)))
  784. def testTanh(self):
  785. return
  786. self.assertRaises(TypeError, math.tanh)
  787. self.ftest('tanh(0)', math.tanh(0), 0)
  788. self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
  789. self.ftest('tanh(inf)', math.tanh(INF), 1)
  790. self.ftest('tanh(-inf)', math.tanh(NINF), -1)
  791. self.assertTrue(math.isnan(math.tanh(NAN)))
  792. @requires_IEEE_754
  793. # @unittest.skipIf(sysconfig.get_config_var('TANH_PRESERVES_ZERO_SIGN') == 0,
  794. # "system tanh() function doesn't copy the sign")
  795. def testTanhSign(self):
  796. return
  797. # check that tanh(-0.) == -0. on IEEE 754 systems
  798. self.assertEqual(math.tanh(-0.), -0.)
  799. self.assertEqual(math.copysign(1., math.tanh(-0.)),
  800. math.copysign(1., -0.))
  801. def test_trunc(self):
  802. self.assertEqual(math.trunc(1), 1)
  803. self.assertEqual(math.trunc(-1), -1)
  804. self.assertEqual(type(math.trunc(1)), int)
  805. self.assertEqual(type(math.trunc(1.5)), int)
  806. self.assertEqual(math.trunc(1.5), 1)
  807. self.assertEqual(math.trunc(-1.5), -1)
  808. self.assertEqual(math.trunc(1.999999), 1)
  809. self.assertEqual(math.trunc(-1.999999), -1)
  810. self.assertEqual(math.trunc(-0.999999), -0)
  811. self.assertEqual(math.trunc(-100.999), -100)
  812. if 0:
  813. self.assertEqual(math.trunc(TestTrunc()), 23)
  814. self.assertRaises(TypeError, math.trunc)
  815. self.assertRaises(TypeError, math.trunc, 1, 2)
  816. self.assertRaises(TypeError, math.trunc, TestNoTrunc())
  817. def testIsfinite(self):
  818. self.assertTrue(math.isfinite(0.0))
  819. self.assertTrue(math.isfinite(-0.0))
  820. self.assertTrue(math.isfinite(1.0))
  821. self.assertTrue(math.isfinite(-1.0))
  822. self.assertFalse(math.isfinite(float("nan")))
  823. self.assertFalse(math.isfinite(float("inf")))
  824. self.assertFalse(math.isfinite(float("-inf")))
  825. def testIsnan(self):
  826. self.assertTrue(math.isnan(float("nan")))
  827. self.assertTrue(math.isnan(float("inf")* 0.))
  828. self.assertFalse(math.isnan(float("inf")))
  829. self.assertFalse(math.isnan(0.))
  830. self.assertFalse(math.isnan(1.))
  831. def testIsinf(self):
  832. self.assertTrue(math.isinf(float("inf")))
  833. self.assertTrue(math.isinf(float("-inf")))
  834. self.assertTrue(math.isinf(1E400))
  835. self.assertTrue(math.isinf(-1E400))
  836. self.assertFalse(math.isinf(float("nan")))
  837. self.assertFalse(math.isinf(0.))
  838. self.assertFalse(math.isinf(1.))
  839. @requires_IEEE_754
  840. def test_testfile(self):
  841. blacklist = {'acosh', 'asinh', 'atanh', 'cosh', 'sinh', 'tanh'}
  842. for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
  843. if fn in blacklist:
  844. continue
  845. # Skip if either the input or result is complex, or if
  846. # flags is nonempty
  847. if ai != 0. or ei != 0. or flags:
  848. continue
  849. if fn in ['rect', 'polar']:
  850. # no real versions of rect, polar
  851. continue
  852. func = getattr(math, fn)
  853. try:
  854. result = func(ar)
  855. except ValueError as exc:
  856. message = (("Unexpected ValueError: %s\n " +
  857. "in test %s:%s(%r)\n") % (exc.args[0], id, fn, ar))
  858. self.fail(message)
  859. except OverflowError:
  860. message = ("Unexpected OverflowError in " +
  861. "test %s:%s(%r)\n" % (id, fn, ar))
  862. self.fail(message)
  863. title = "%s:%s(%r)" % (id, fn, ar)
  864. if abs(result-er) > eps:
  865. print(f'{title}: expected {er}, got {result}')
  866. @requires_IEEE_754
  867. def test_mtestfile(self):
  868. fail_fmt = "{}:{}({}): expected {}, got {}"
  869. failures = []
  870. blacklist = {'erf', 'erfc', 'lgamma', 'gamma', 'log1p', 'expm1'}
  871. for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
  872. if fn in blacklist:
  873. continue
  874. func = getattr(math, fn)
  875. if 'invalid' in flags or 'divide-by-zero' in flags:
  876. # expected = 'ValueError'
  877. expected = float('nan')
  878. elif 'overflow' in flags:
  879. expected = 'OverflowError'
  880. try:
  881. got = func(arg)
  882. except ValueError:
  883. got = 'ValueError'
  884. except OverflowError:
  885. got = 'OverflowError'
  886. accuracy_failure = None
  887. if isinstance(got, float) and isinstance(expected, float):
  888. if math.isnan(expected) and math.isnan(got):
  889. continue
  890. if not math.isnan(expected) and not math.isnan(got):
  891. if fn == 'lgamma':
  892. # we use a weaker accuracy test for lgamma;
  893. # lgamma only achieves an absolute error of
  894. # a few multiples of the machine accuracy, in
  895. # general.
  896. accuracy_failure = acc_check(expected, got,
  897. rel_err = 5e-15,
  898. abs_err = 5e-15)
  899. elif fn == 'erfc':
  900. # erfc has less-than-ideal accuracy for large
  901. # arguments (x ~ 25 or so), mainly due to the
  902. # error involved in computing exp(-x*x).
  903. #
  904. # XXX Would be better to weaken this test only
  905. # for large x, instead of for all x.
  906. accuracy_failure = ulps_check(expected, got, 2000)
  907. else:
  908. accuracy_failure = ulps_check(expected, got, 20)
  909. if accuracy_failure is None:
  910. continue
  911. if isinstance(got, str) and isinstance(expected, str):
  912. if got == expected:
  913. continue
  914. fail_msg = fail_fmt.format(id, fn, arg, expected, got)
  915. if accuracy_failure is not None:
  916. fail_msg += ' ({})'.format(accuracy_failure)
  917. failures.append(fail_msg)
  918. if failures:
  919. print('Failures in test_mtestfile:\n ' +
  920. '\n '.join(failures))
  921. if __name__ == '__main__':
  922. c = MathTests()
  923. tests = list(MathTests.__dict__.items())
  924. tests.sort(key=lambda x: x[0])
  925. for k, v in tests:
  926. if k.startswith('test'):
  927. print(f'==> {k}...')
  928. getattr(c, k)()