/test2/gmpy_test_mpf.py

http://gmpy.googlecode.com/ · Python · 392 lines · 322 code · 15 blank · 55 comment · 6 complexity · e1552d608cbceee43107570c15d59047 MD5 · raw file

  1. # partial unit test for gmpy2 mpfr functionality
  2. # relies on Tim Peters' "doctest.py" test-driver
  3. r'''
  4. >>> filter(lambda x: not x.startswith('__'), dir(a))
  5. ['as_integer_ratio', 'as_mantissa_exp', 'as_simple_fraction', 'conjugate', 'digits', 'imag', 'is_integer', 'precision', 'rc', 'real']
  6. >>>
  7. '''
  8. import sys
  9. import gmpy2 as _g, doctest, sys
  10. __test__={}
  11. a=_g.mpfr('123.456')
  12. b=_g.mpfr('789.123')
  13. __test__['functions']=\
  14. r'''
  15. >>> _g.log(2)
  16. mpfr('0.69314718055994529')
  17. >>> _g.log(10)
  18. mpfr('2.3025850929940459')
  19. >>> _g.log('a')
  20. Traceback (most recent call last):
  21. File "<stdin>", line 1, in <module>
  22. TypeError: log() argument type not supported
  23. >>> _g.log(float('nan'))
  24. mpfr('nan')
  25. >>> _g.log(float('inf'))
  26. mpfr('inf')
  27. >>> _g.log(float('-inf'))
  28. mpfr('nan')
  29. >>> _g.log(_g.mpfr('12.3456'))
  30. mpfr('2.5132997242892183')
  31. >>> _g.log(_g.mpfr('12.3456'),7)
  32. Traceback (most recent call last):
  33. File "<stdin>", line 1, in <module>
  34. TypeError: log() takes exactly one argument (2 given)
  35. '''
  36. __test__['elemop']=\
  37. r'''
  38. >>> str(a+b)
  39. '912.57900000000006'
  40. >>> str(a-b)
  41. '-665.66700000000003'
  42. >>> str(a*b)
  43. '97421.969088000013'
  44. >>> str(a/b)
  45. '0.15644709379906555'
  46. >>> str(b+a)
  47. '912.57900000000006'
  48. >>> str(b-a)
  49. '665.66700000000003'
  50. >>> str(b*a)
  51. '97421.969088000013'
  52. >>> str(b/a)
  53. '6.3919372083981338'
  54. >>> str(-a)
  55. '-123.456'
  56. >>> _g.mpfr(2,200) + 3
  57. mpfr('5.0')
  58. >>> 3 + _g.mpfr(2,200)
  59. mpfr('5.0')
  60. >>> _g.mpfr(2,200) * 3
  61. mpfr('6.0')
  62. >>> 3 * _g.mpfr(2,200)
  63. mpfr('6.0')
  64. >>> str(abs(-a))
  65. '123.456'
  66. >>> _g.sign(b-a)
  67. 1
  68. >>> _g.sign(b-b)
  69. 0
  70. >>> _g.sign(a-b)
  71. -1
  72. >>> _g.sign(a)
  73. 1
  74. >>> _g.sign(-a)
  75. -1
  76. >>> z=b-b; _g.sign(z)
  77. 0
  78. >>> import math
  79. >>> math.ceil(a)
  80. 124.0
  81. >>> str(a.__ceil__())
  82. '124.0'
  83. >>> str(_g.ceil(a))
  84. '124.0'
  85. >>> math.floor(a)
  86. 123.0
  87. >>> str(a.__floor__())
  88. '123.0'
  89. >>> str(_g.floor(a))
  90. '123.0'
  91. >>> str(a.__trunc__())
  92. '123.0'
  93. >>> str(_g.trunc(a))
  94. '123.0'
  95. >>> x=-a
  96. >>> math.floor(x)
  97. -124.0
  98. >>> str(x.__floor__())
  99. '-124.0'
  100. >>> str(_g.floor(x))
  101. '-124.0'
  102. >>> str(x.__ceil__())
  103. '-123.0'
  104. >>> math.ceil(x)
  105. -123.0
  106. >>> str(_g.ceil(x))
  107. '-123.0'
  108. >>> str(x.__trunc__())
  109. '-123.0'
  110. >>> str(_g.trunc(x))
  111. '-123.0'
  112. >>> _g.ceil(12.3)==math.ceil(12.3)
  113. 1
  114. >>> _g.floor(12.3)==math.floor(12.3)
  115. 1
  116. >>> _g.ceil(-12.3)==math.ceil(-12.3)
  117. 1
  118. >>> _g.floor(-12.3)==math.floor(-12.3)
  119. 1
  120. >>> _g.reldiff(a**2,float(a)**2) < 1.03 * (2.0**-(a.precision-1))
  121. 1
  122. >>> _g.reldiff(a**2,a*a) < (2.0**-(a.precision-1))
  123. 1
  124. >>> _g.reldiff(b**2,float(b)**2) < 1.03 * (2.0**-(b.precision-1))
  125. 1
  126. >>> _g.reldiff(b**2,b*b) < (2.0**-(b.precision-1))
  127. 1
  128. >>> _g.reldiff(3.4)
  129. Traceback (innermost last):
  130. File "<pyshell#184>", line 1, in ?
  131. _g.reldiff(3.4)
  132. TypeError: reldiff() requires 'mpfr','mpfr' arguments
  133. >>> _g.sqrt(a)
  134. mpfr('11.111075555498667')
  135. >>> _g.sqrt(-1.0)
  136. mpfr('nan')
  137. >>> save=_g.get_context().precision
  138. >>> _g.const_catalan()
  139. mpfr('0.91596559417721901')
  140. >>> _g.const_euler()
  141. mpfr('0.57721566490153287')
  142. >>> _g.const_log2()
  143. mpfr('0.69314718055994529')
  144. >>> _g.const_pi()
  145. mpfr('3.1415926535897931')
  146. >>> _g.get_context().precision = 100
  147. >>> _g.const_catalan()
  148. mpfr('0.91596559417721901505460351493252',100)
  149. >>> _g.const_euler()
  150. mpfr('0.57721566490153286060651209008234',100)
  151. >>> _g.const_log2()
  152. mpfr('0.69314718055994530941723212145798',100)
  153. >>> _g.const_pi()
  154. mpfr('3.1415926535897932384626433832793',100)
  155. >>> _g.get_context().precision = save
  156. >>> del(save)
  157. >>> import pickle
  158. >>> flt = _g.mpfr(1234.6789)
  159. >>> flt == pickle.loads(pickle.dumps(flt))
  160. True
  161. >>> flt = _g.mpfr('1.1')
  162. >>> flt == pickle.loads(pickle.dumps(flt))
  163. True
  164. '''
  165. from gmpy_truediv import truediv
  166. __test__['newdiv']=\
  167. r'''
  168. >>>
  169. >>> a/b
  170. mpfr('0.15644709379906555')
  171. >>> a//b
  172. mpfr('0.0')
  173. >>> truediv(a,b)
  174. mpfr('0.15644709379906555')
  175. >>> b/a
  176. mpfr('6.3919372083981338')
  177. >>> b//a
  178. mpfr('6.0')
  179. >>> truediv(b,a)
  180. mpfr('6.3919372083981338')
  181. >>>
  182. '''
  183. __test__['cmpr']=\
  184. r'''
  185. >>> c=_g.mpfr(a)
  186. >>> c is a
  187. 1
  188. >>> c==a
  189. 1
  190. >>> c>a
  191. 0
  192. >>> c<a
  193. 0
  194. >>> cmp(a,c)
  195. 0
  196. >>> cmp(a,b)
  197. -1
  198. >>> a>b
  199. 0
  200. >>> a<b
  201. 1
  202. >>> not _g.mpfr(0)
  203. 1
  204. >>> not a
  205. 0
  206. >>> _g.f2q(a,0.1)
  207. mpz(123)
  208. >>> _g.f2q(a,0.01)
  209. mpz(123)
  210. >>> _g.f2q(a,0.001)
  211. mpq(247,2)
  212. >>> _g.f2q(a,0.0001)
  213. mpq(1358,11)
  214. >>> _g.f2q(a,0.00001)
  215. mpq(7037,57)
  216. >>> _g.f2q(a,0.000001)
  217. mpq(15432,125)
  218. >>> _g.f2q(a,0.0000001)
  219. mpq(15432,125)
  220. >>> _g.f2q(a)
  221. mpq(15432,125)
  222. >>> print _g.mpfr(_g.mpz(1234))
  223. 1234.0
  224. >>> x=1000*1000*1000*1000L
  225. >>> _g.mpfr(x)
  226. mpfr('1000000000000.0')
  227. >>> c=_g.mpfr(a)
  228. >>> a is c
  229. 1
  230. >>> c=_g.mpfr(a,99)
  231. >>> a is c
  232. 0
  233. >>> a==c
  234. 1
  235. >>> _g.mpfr('1.1') == _g.mpfr('1.1') * _g.mpfr(1)
  236. True
  237. >>> _g.mpfr('1.1',64) == _g.mpfr('1.1',128)
  238. False
  239. >>> _g.mpfr('1.1',64) == _g.mpfr(_g.mpfr('1.1',128),64)
  240. True
  241. >>> a = _g.mpfr('.123', 64)
  242. >>> b = _g.mpfr('.123', 128)
  243. >>> c = _g.mpfr('.123', 128) * _g.mpfr(1, 128)
  244. >>> a == b
  245. False
  246. >>> a == c
  247. False
  248. >>> b == c
  249. False
  250. >>> a == _g.round2(b,64)
  251. True
  252. >>> _g.round2(_g.mpfr('ffffffffffffffffe8000000000000000', 256, 16),64).digits(16)
  253. ('10000000000000000', 34, 64)
  254. >>> _g.round2(_g.mpfr('fffffffffffffffff8000000000000000', 256, 16),64).digits(16)
  255. ('10000000000000000', 34, 64)
  256. >>> _g.round2(b,64)
  257. mpfr('0.122999999999999999999',64)
  258. '''
  259. __test__['format']=\
  260. r'''
  261. >>> str(a)
  262. '123.456'
  263. >>> repr(a)
  264. "mpfr('123.456')"
  265. >>> a.digits(10,0)
  266. ('12345600000000000', 3, 53)
  267. >>> a.digits(10,1)
  268. Traceback (most recent call last):
  269. ...
  270. ValueError: digits must be 0 or >= 2
  271. >>> a.digits(10,2)
  272. ('12', 3, 53)
  273. >>> a.digits(10,3)
  274. ('123', 3, 53)
  275. >>> a.digits(10,4)
  276. ('1235', 3, 53)
  277. >>> a.digits(10,5)
  278. ('12346', 3, 53)
  279. >>> a.digits(10,6)
  280. ('123456', 3, 53)
  281. >>> a.digits(10,7)
  282. ('1234560', 3, 53)
  283. >>> a.digits(10,8)
  284. ('12345600', 3, 53)
  285. >>> _g.mpfr(3.4)
  286. mpfr('3.3999999999999999')
  287. >>> print _g.mpfr(3.4)
  288. 3.3999999999999999
  289. >>> a.digits(1)
  290. Traceback (most recent call last):
  291. File "<string>", line 1, in ?
  292. ValueError: base must be in the interval 2 ... 62
  293. >>> a.digits(2,-1)
  294. Traceback (most recent call last):
  295. File "<string>", line 1, in ?
  296. ValueError: digits must be 0 or >= 2
  297. >>> a.digits(10,0,0,-1,2)
  298. Traceback (most recent call last):
  299. ...
  300. TypeError: function takes at most 2 arguments (5 given)
  301. >>> saveprec=a.precision
  302. >>> newa = _g.round2(a,33)
  303. >>> newa
  304. mpfr('123.456',33)
  305. >>> newa = _g.round2(newa,saveprec)
  306. >>> newa.precision==saveprec
  307. 1
  308. >>> del(newa)
  309. >>> _g.digits(_g.mpfr(23.45))
  310. ('23449999999999999', 2, 53)
  311. >>>
  312. '''
  313. __test__['binio']=\
  314. r'''
  315. >>> _g.from_binary('pep')
  316. Traceback (most recent call last):
  317. File "<stdin>", line 1, in ?
  318. TypeError: from_binary() argument type not supported
  319. >>> a == _g.from_binary(_g.to_binary(a))
  320. True
  321. >>> -a == _g.from_binary(_g.to_binary(-a))
  322. True
  323. >>> 1/a == _g.from_binary(_g.to_binary(1/a))
  324. True
  325. >>> _g.to_binary(_g.mpfr(0))
  326. '\x04\x00\x00\x005\x00\x00\x00'
  327. >>> 0 == _g.from_binary(_g.to_binary(_g.mpfr(0)))
  328. True
  329. >>> 0.5 == _g.from_binary(_g.to_binary(_g.mpfr(0.5)))
  330. True
  331. >>> -0.5 == _g.from_binary(_g.to_binary(_g.mpfr(-0.5)))
  332. True
  333. >>> -2.0 == _g.from_binary(_g.to_binary(_g.mpfr(-2.0)))
  334. True
  335. >>> 2.0 == _g.from_binary(_g.to_binary(_g.mpfr(2.0)))
  336. True
  337. >>> hash(_g.mpfr(23.0))==hash(23)
  338. True
  339. >>> long(a)
  340. 123L
  341. >>> long(-a)
  342. -123L
  343. >>> int(a)
  344. 123
  345. >>> int(-a)
  346. -123
  347. >>>
  348. '''
  349. def _test(chat=None):
  350. if chat:
  351. print "Unit tests for gmpy2 (mpfr functionality)"
  352. print " on Python %s" % sys.version
  353. print "Testing gmpy2 {0}".format(_g.version())
  354. print " Mutliple-precision library: {0}".format(_g.mp_version())
  355. print " Floating-point library: {0}".format(_g.mpfr_version())
  356. print " Complex library: {0}".format(_g.mpc_version())
  357. print " Caching Values: (Number) {0}".format(_g.get_cache()[0])
  358. print " Caching Values: (Size, limbs) {0}".format(_g.get_cache()[1])
  359. thismod = sys.modules.get(__name__)
  360. doctest.testmod(thismod, report=0)
  361. if chat: print "Repeating tests, with caching disabled"
  362. _g.set_cache(0,128)
  363. sav = sys.stdout
  364. class _Dummy:
  365. def write(self,*whatever):
  366. pass
  367. try:
  368. sys.stdout = _Dummy()
  369. doctest.testmod(thismod, report=0)
  370. finally:
  371. sys.stdout = sav
  372. if chat:
  373. print
  374. print "Overall results for mpfr:"
  375. return doctest.master.summarize(chat)
  376. if __name__=='__main__':
  377. _test(1)