PageRenderTime 25ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/test/test_mpz.txt

http://gmpy.googlecode.com/
Plain Text | 485 lines | 449 code | 36 blank | 0 comment | 0 complexity | 90c4a72abd5d9ef7a9d052bfcc2bfa0b MD5 | raw file
  1MPZ Functionality
  2=================
  3
  4Testing of mpz functionality is split into multiple files.
  5
  6    test_mpz.txt
  7        Test basic functionality and stuff not covered anywhere else.
  8
  9    test_mpz_functions.txt
 10        Test mpz functions, including error messages.
 11
 12    test_mpz_io.txt
 13        Test input/output and formating.
 14
 15    test_mpz_comp.txt
 16        Test comparisons.
 17
 18    >>> import gmpy2 as G
 19    >>> from gmpy2 import mpz, mpq, mpfr, mpc
 20    >>> from decimal import Decimal as D
 21    >>> from fractions import Fraction as F
 22    >>> a = mpz(123)
 23    >>> b = mpz(456)
 24    >>> c = 12345678901234567890
 25
 26Test elementary operations
 27==========================
 28
 29Test addition
 30-------------
 31
 32    >>> a+1
 33    mpz(124)
 34    >>> a+(-1)
 35    mpz(122)
 36    >>> 1+a
 37    mpz(124)
 38    >>> (-1)+a
 39    mpz(122)
 40    >>> a+b
 41    mpz(579)
 42    >>> b+a
 43    mpz(579)
 44    >>> a+'b'
 45    Traceback (most recent call last):
 46      File "<stdin>", line 1, in <module>
 47    TypeError: unsupported operand type(s) for +: 'mpz' and 'str'
 48    >>> 'b'+a
 49    Traceback (most recent call last):
 50      File "<stdin>", line 1, in <module>
 51    TypeError: ** message detail varies **
 52    >>> print(a+c)
 53    12345678901234568013
 54    >>> print(c+a)
 55    12345678901234568013
 56
 57
 58Test subtraction
 59----------------
 60
 61    >>> a-1
 62    mpz(122)
 63    >>> a-(-1)
 64    mpz(124)
 65    >>> 1-a
 66    mpz(-122)
 67    >>> (-1)-a
 68    mpz(-124)
 69    >>> a-b
 70    mpz(-333)
 71    >>> b-a
 72    mpz(333)
 73    >>> a-'b'
 74    Traceback (most recent call last):
 75      File "<stdin>", line 1, in <module>
 76    TypeError: unsupported operand type(s) for -: 'mpz' and 'str'
 77    >>> 'b'-a
 78    Traceback (most recent call last):
 79      File "<stdin>", line 1, in <module>
 80    TypeError: ** message detail varies **
 81    >>> print(a-c)
 82    -12345678901234567767
 83    >>> print(c-a)
 84    12345678901234567767
 85
 86Test multiplication
 87-------------------
 88
 89    >>> a*b
 90    mpz(56088)
 91    >>> b*a
 92    mpz(56088)
 93    >>> a*0
 94    mpz(0)
 95    >>> 0*a
 96    mpz(0)
 97    >>> a*123
 98    mpz(15129)
 99    >>> 123*a
100    mpz(15129)
101    >>> print(a*c)
102    1518518504851851850470
103    >>> print(c*a)
104    1518518504851851850470
105    >>> a*'b'
106    'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
107    >>> 'b'*a
108    'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
109
110Test division
111-------------
112
113    >>> a//b
114    mpz(0)
115    >>> a/b
116    mpfr('0.26973684210526316')
117    >>> b//a
118    mpz(3)
119    >>> b/a
120    mpfr('3.7073170731707319')
121    >>> (a*b)//b
122    mpz(123)
123    >>> (a*b)//a
124    mpz(456)
125    >>> a//0
126    Traceback (most recent call last):
127      File "<stdin>", line 1, in <module>
128    ZeroDivisionError: division or modulo by zero
129    >>> a/0
130    Traceback (most recent call last):
131      File "<stdin>", line 1, in <module>
132    ZeroDivisionError: division or modulo by zero
133    >>> a/0.0
134    mpfr('inf')
135    >>> print(c//a)
136    100371373180768844
137    >>> a**10//c
138    mpz(64)
139
140Test modulo
141-----------
142
143    >>> a%b
144    mpz(123)
145    >>> b%a
146    mpz(87)
147    >>> divmod(a,b)
148    (mpz(0), mpz(123))
149    >>> divmod(b,a)
150    (mpz(3), mpz(87))
151    >>> divmod(a,0)
152    Traceback (most recent call last):
153      ...
154    ZeroDivisionError: division or modulo by zero
155    >>> divmod(a,mpz(0))
156    Traceback (most recent call last):
157      File "<stdin>", line 1, in <module>
158    ZeroDivisionError: division or modulo by zero
159    >>> divmod(123,mpz(0))
160    Traceback (most recent call last):
161      File "<stdin>", line 1, in <module>
162    ZeroDivisionError: division or modulo by zero
163    >>> divmod(b,123)
164    (mpz(3), mpz(87))
165    >>> divmod(a,c)
166    (mpz(0), mpz(123))
167    >>> divmod(a,int(c))
168    (mpz(0), mpz(123))
169    >>> print("%s %s" % divmod(a*(c-1),c))
170    122 12345678901234567767
171    >>> print("%s %s" % divmod(a*(c-1),int(c)))
172    122 12345678901234567767
173    >>> divmod(a*(c-1),-c)
174    (mpz(-123), mpz(-123))
175    >>> divmod(a*(c-1),-int(c))
176    (mpz(-123), mpz(-123))
177    >>> print("%s %s" % divmod(int(a*(c-1)),-int(c)))
178    -123 -123
179
180Test miscellaneous
181------------------
182
183    >>> a+True
184    mpz(124)
185    >>> a+False
186    mpz(123)
187    >>> a*False
188    mpz(0)
189    >>> a//True
190    mpz(123)
191    >>> abs(-a) == a
192    True
193    >>> print(pow(a,10))
194    792594609605189126649
195    >>> pow(a,7,b)
196    mpz(99)
197    >>> G.sign(b-a)
198    1
199    >>> G.sign(b-b)
200    0
201    >>> G.sign(a-b)
202    -1
203    >>> G.sign(a)
204    1
205    >>> G.sign(-a)
206    -1
207    >>> z=b-b; G.sign(z)
208    0
209
210Test pickle
211-----------
212
213    >>> import pickle
214    >>> pickle.loads(pickle.dumps(G.mpz(12346789)))
215    mpz(12346789)
216    >>> pickle.loads(pickle.dumps(G.mpz(-12346789)))
217    mpz(-12346789)
218    >>> pickle.loads(pickle.dumps(G.mpz(0)))
219    mpz(0)
220
221Test mpz.__index__
222------------------
223
224    >>> range(333)[a]
225    123
226    >>> range(333)[b]
227    Traceback (innermost last):
228      ...
229    IndexError: range object index out of range
230
231Test operations involving NaN/Inf
232---------------------------------
233
234    >>> a + float('Inf')
235    mpfr('inf')
236    >>> float('Inf') + a
237    mpfr('inf')
238    >>> a + float('-Inf')
239    mpfr('-inf')
240    >>> float('-Inf') + a
241    mpfr('-inf')
242    >>> a + float('nan')
243    mpfr('nan')
244    >>> float('nan') + a
245    mpfr('nan')
246
247    >>> a - float('Inf')
248    mpfr('-inf')
249    >>> float('Inf') - a
250    mpfr('inf')
251    >>> a - float('-Inf')
252    mpfr('inf')
253    >>> float('-Inf') - a
254    mpfr('-inf')
255    >>> a - float('nan')
256    mpfr('nan')
257    >>> float('nan') - a
258    mpfr('nan')
259
260    >>> a * float('Inf')
261    mpfr('inf')
262    >>> float('Inf') * a
263    mpfr('inf')
264    >>> a * float('-Inf')
265    mpfr('-inf')
266    >>> float('-Inf') * a
267    mpfr('-inf')
268    >>> -a * float('Inf')
269    mpfr('-inf')
270    >>> float('Inf') * -a
271    mpfr('-inf')
272    >>> -a * float('-Inf')
273    mpfr('inf')
274    >>> float('-Inf') * -a
275    mpfr('inf')
276    >>> a * float('nan')
277    mpfr('nan')
278    >>> float('nan') * a
279    mpfr('nan')
280    >>> G.mpz(0) * float('Inf')
281    mpfr('nan')
282    >>> G.mpz(0) * float('-Inf')
283    mpfr('nan')
284    >>> float('Inf') * G.mpz(0)
285    mpfr('nan')
286    >>> float('-Inf') * G.mpz(0)
287    mpfr('nan')
288    >>> a / float('Inf')
289    mpfr('0.0')
290    >>> -a / float('Inf')
291    mpfr('-0.0')
292    >>> float('Inf') / a
293    mpfr('inf')
294    >>> float('Inf') / -a
295    mpfr('-inf')
296    >>> a / float('-Inf')
297    mpfr('-0.0')
298    >>> -a / float('-Inf')
299    mpfr('0.0')
300    >>> float('-Inf') / a
301    mpfr('-inf')
302    >>> float('-Inf') / -a
303    mpfr('inf')
304    >>> a / float('nan')
305    mpfr('nan')
306    >>> float('nan') / a
307    mpfr('nan')
308    >>> float('nan') / G.mpz(0)
309    mpfr('nan')
310    >>> float('nan') / G.mpz(0)
311    mpfr('nan')
312
313    >>> a - mpfr('Inf')
314    mpfr('-inf')
315    >>> mpfr('Inf') - a
316    mpfr('inf')
317    >>> a - mpfr('-Inf')
318    mpfr('inf')
319    >>> mpfr('-Inf') - a
320    mpfr('-inf')
321    >>> a - mpfr('nan')
322    mpfr('nan')
323    >>> mpfr('nan') - a
324    mpfr('nan')
325    >>> a * mpfr('Inf')
326    mpfr('inf')
327    >>> mpfr('Inf') * a
328    mpfr('inf')
329    >>> a * mpfr('-Inf')
330    mpfr('-inf')
331    >>> mpfr('-Inf') * a
332    mpfr('-inf')
333    >>> -a * mpfr('Inf')
334    mpfr('-inf')
335    >>> mpfr('Inf') * -a
336    mpfr('-inf')
337    >>> -a * mpfr('-Inf')
338    mpfr('inf')
339    >>> mpfr('-Inf') * -a
340    mpfr('inf')
341    >>> a * mpfr('nan')
342    mpfr('nan')
343    >>> mpfr('nan') * a
344    mpfr('nan')
345    >>> G.mpz(0) * mpfr('Inf')
346    mpfr('nan')
347    >>> G.mpz(0) * mpfr('-Inf')
348    mpfr('nan')
349    >>> mpfr('Inf') * G.mpz(0)
350    mpfr('nan')
351    >>> mpfr('-Inf') * G.mpz(0)
352    mpfr('nan')
353    >>> a / mpfr('Inf')
354    mpfr('0.0')
355    >>> -a / mpfr('Inf')
356    mpfr('-0.0')
357    >>> mpfr('Inf') / a
358    mpfr('inf')
359    >>> mpfr('Inf') / -a
360    mpfr('-inf')
361    >>> a / mpfr('-Inf')
362    mpfr('-0.0')
363    >>> -a / mpfr('-Inf')
364    mpfr('0.0')
365    >>> mpfr('-Inf') / a
366    mpfr('-inf')
367    >>> mpfr('-Inf') / -a
368    mpfr('inf')
369    >>> a / mpfr('nan')
370    mpfr('nan')
371    >>> mpfr('nan') / a
372    mpfr('nan')
373    >>> mpfr('nan') / G.mpz(0)
374    mpfr('nan')
375    >>> mpfr('nan') / G.mpz(0)
376    mpfr('nan')
377
378    >>> divmod(a, mpfr('Inf'))
379    (mpfr('0.0'), mpfr('123.0'))
380    >>> divmod(a, mpfr('-Inf'))
381    (mpfr('-1.0'), mpfr('-inf'))
382    >>> divmod(-a, mpfr('Inf'))
383    (mpfr('-1.0'), mpfr('inf'))
384    >>> divmod(-a, mpfr('-Inf'))
385    (mpfr('0.0'), mpfr('-123.0'))
386    >>> divmod(a, mpfr('nan'))
387    (mpfr('nan'), mpfr('nan'))
388    >>> divmod(-a, mpfr('nan'))
389    (mpfr('nan'), mpfr('nan'))
390    >>> divmod(G.mpz(0), mpfr('Inf'))
391    (mpfr('0.0'), mpfr('0.0'))
392    >>> divmod(G.mpz(0), mpfr('-Inf'))
393    (mpfr('-0.0'), mpfr('-0.0'))
394    >>> divmod(G.mpz(0), mpfr('nan'))
395    (mpfr('nan'), mpfr('nan'))
396    >>> divmod(mpfr('Inf'), a)
397    (mpfr('nan'), mpfr('nan'))
398    >>> divmod(mpfr('-Inf'), a)
399    (mpfr('nan'), mpfr('nan'))
400    >>> divmod(mpfr('Inf'), -a)
401    (mpfr('nan'), mpfr('nan'))
402    >>> divmod(mpfr('-Inf'), -a)
403    (mpfr('nan'), mpfr('nan'))
404    >>> divmod(mpfr('nan'), a)
405    (mpfr('nan'), mpfr('nan'))
406    >>> divmod(mpfr('nan'), -a)
407    (mpfr('nan'), mpfr('nan'))
408    >>> divmod(mpfr('Inf'), G.mpz(0))
409    (mpfr('nan'), mpfr('nan'))
410    >>> divmod(mpfr('-Inf'), G.mpz(0))
411    (mpfr('nan'), mpfr('nan'))
412    >>> divmod(mpfr('nan'), G.mpz(0))
413    (mpfr('nan'), mpfr('nan'))
414
415    >>> divmod(a, mpfr('Inf'))
416    (mpfr('0.0'), mpfr('123.0'))
417    >>> divmod(a, mpfr('-Inf'))
418    (mpfr('-1.0'), mpfr('-inf'))
419    >>> divmod(-a, mpfr('Inf'))
420    (mpfr('-1.0'), mpfr('inf'))
421    >>> divmod(-a, mpfr('-Inf'))
422    (mpfr('0.0'), mpfr('-123.0'))
423    >>> divmod(a, mpfr('nan'))
424    (mpfr('nan'), mpfr('nan'))
425    >>> divmod(-a, mpfr('nan'))
426    (mpfr('nan'), mpfr('nan'))
427    >>> divmod(G.mpz(0), mpfr('Inf'))
428    (mpfr('0.0'), mpfr('0.0'))
429    >>> divmod(G.mpz(0), mpfr('-Inf'))
430    (mpfr('-0.0'), mpfr('-0.0'))
431    >>> divmod(G.mpz(0), mpfr('nan'))
432    (mpfr('nan'), mpfr('nan'))
433    >>> divmod(mpfr('Inf'), a)
434    (mpfr('nan'), mpfr('nan'))
435    >>> divmod(mpfr('-Inf'), a)
436    (mpfr('nan'), mpfr('nan'))
437    >>> divmod(mpfr('Inf'), -a)
438    (mpfr('nan'), mpfr('nan'))
439    >>> divmod(mpfr('-Inf'), -a)
440    (mpfr('nan'), mpfr('nan'))
441    >>> divmod(mpfr('nan'), a)
442    (mpfr('nan'), mpfr('nan'))
443    >>> divmod(mpfr('nan'), -a)
444    (mpfr('nan'), mpfr('nan'))
445    >>> divmod(mpfr('Inf'), G.mpz(0))
446    (mpfr('nan'), mpfr('nan'))
447    >>> divmod(mpfr('-Inf'), G.mpz(0))
448    (mpfr('nan'), mpfr('nan'))
449    >>> divmod(mpfr('nan'), G.mpz(0))
450    (mpfr('nan'), mpfr('nan'))
451
452Test bit operations
453-------------------
454
455    >>> ~a
456    mpz(-124)
457    >>> a&b
458    mpz(72)
459    >>> a|b
460    mpz(507)
461    >>> a^b
462    mpz(435)
463    >>> a<<1
464    mpz(246)
465    >>> a>>1
466    mpz(61)
467    >>> a<<-1
468    Traceback (innermost last):
469      ...
470    ValueError: negative shift count
471    >>> a>>-2
472    Traceback (innermost last):
473      ...
474    ValueError: negative shift count
475    >>> a<<0
476    mpz(123)
477    >>> a>>0
478    mpz(123)
479
480Test conversions
481----------------
482
483    >>> int(G.mpz(-3))
484    -3
485