PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test2/gmpy_test_mpf.py

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