1
0

931_math.py 42 KB

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