MPFR Functionality ================== Testing of mpfr functionality is split into multiple files. test_mpfr.txt Test basic functionality and stuff not covered anywhere else. test_mpfr_exp_log.txt Test exponentiation and logarithmic functions. test_mpfr_special.txt Test special functions test_mpfr_io.txt Test input/output and formating. test_mpfr_comp.txt Test comparisons. >>> import gmpy2 as G >>> from gmpy2 import mpz, mpq, mpfr >>> a = mpfr("12.34") >>> b = mpfr("45.67") >>> c = 12345678901234567890 Test elementary operations ========================== Test addition ------------- >>> a+1 mpfr('13.34') >>> a+1.0 mpfr('13.34') >>> a+mpz(1) mpfr('13.34') >>> a+mpq(1,1) mpfr('13.34') >>> 1+a mpfr('13.34') >>> 1.0+a mpfr('13.34') >>> mpz(1)+a mpfr('13.34') >>> mpq(1,1)+a mpfr('13.34') >>> a+b mpfr('58.010000000000005') >>> a+0 mpfr('12.34') >>> 0+a mpfr('12.34') >>> b+a mpfr('58.010000000000005') >>> a+'b' Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'mpfr' and 'str' >>> 'b'+a Traceback (most recent call last): File "", line 1, in TypeError: Can't convert 'mpfr' object to str implicitly >>> a == 12.34 True >>> b == 45.67 True >>> a+b == 12.34+45.67 True Test subtraction ---------------- >>> a-1 mpfr('11.34') >>> a-1.0 mpfr('11.34') >>> a-(-1) mpfr('13.34') >>> a-(-1.0) mpfr('13.34') >>> a-b mpfr('-33.329999999999998') >>> b-a mpfr('33.329999999999998') >>> a-'b' Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for -: 'mpfr' and 'str' >>> 'b'-a Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for -: 'str' and 'mpfr' >>> a-b==12.34-45.67 True Test multiplication ------------------- >>> a*b mpfr('563.56780000000003') >>> a*0 mpfr('0.0') >>> a*0.0 mpfr('0.0') >>> a*1 mpfr('12.34') >>> a*1.0 mpfr('12.34') >>> a*(-1.0) mpfr('-12.34') >>> 0*a mpfr('0.0') >>> 0.0*a mpfr('0.0') >>> 1*a mpfr('12.34') >>> 1.0*a mpfr('12.34') >>> a*b mpfr('563.56780000000003') >>> a*b==12.34*45.67 True Test division ------------- >>> a//b mpfr('0.0') >>> a/b mpfr('0.27019925552879348') >>> b//a mpfr('3.0') >>> b/a mpfr('3.7009724473257699') >>> a/b==12.34/45.67 True >>> a/0 mpfr('inf') >>> a/(-0.0) mpfr('-inf') >>> a/b==12.34/45.67 True Test modulo ----------- >>> a%1 mpfr('0.33999999999999986') >>> 12.34%1 0.33999999999999986 Test divmod ----------- >>> divmod(12.34, 45.67) (0.0, 12.34) >>> divmod(a,b) (mpfr('0.0'), mpfr('12.34')) >>> divmod(b,a) (mpfr('3.0'), mpfr('8.6500000000000021')) >>> divmod(45.67,12.34)==divmod(b,a) True Test pickle ----------- >>> import pickle >>> pickle.loads(pickle.dumps(a)) mpfr('12.34') >>> pickle.loads(pickle.dumps(mpfr("inf"))) mpfr('inf') >>> pickle.loads(pickle.dumps(mpfr("-inf"))) mpfr('-inf') >>> pickle.loads(pickle.dumps(mpfr("nan"))) mpfr('nan') >>> pickle.loads(pickle.dumps(mpfr(0))) mpfr('0.0') Test operations involving NaN/Inf --------------------------------- >>> a + float('Inf') mpfr('inf') >>> float('Inf') + a mpfr('inf') >>> a + float('-Inf') mpfr('-inf') >>> float('-Inf') + a mpfr('-inf') >>> a + float('nan') mpfr('nan') >>> float('nan') + a mpfr('nan') >>> a - float('Inf') mpfr('-inf') >>> float('Inf') - a mpfr('inf') >>> a - float('-Inf') mpfr('inf') >>> float('-Inf') - a mpfr('-inf') >>> a - float('nan') mpfr('nan') >>> float('nan') - a mpfr('nan') >>> a * float('Inf') mpfr('inf') >>> float('Inf') * a mpfr('inf') >>> a * float('-Inf') mpfr('-inf') >>> float('-Inf') * a mpfr('-inf') >>> -a * float('Inf') mpfr('-inf') >>> float('Inf') * -a mpfr('-inf') >>> -a * float('-Inf') mpfr('inf') >>> float('-Inf') * -a mpfr('inf') >>> a * float('nan') mpfr('nan') >>> float('nan') * a mpfr('nan') >>> mpfr(0) * float('Inf') mpfr('nan') >>> mpfr(0) * float('-Inf') mpfr('nan') >>> float('Inf') * mpfr(0) mpfr('nan') >>> float('-Inf') * mpfr(0) mpfr('nan') >>> a / float('Inf') mpfr('0.0') >>> -a / float('Inf') mpfr('-0.0') >>> float('Inf') / a mpfr('inf') >>> float('Inf') / -a mpfr('-inf') >>> a / float('-Inf') mpfr('-0.0') >>> -a / float('-Inf') mpfr('0.0') >>> float('-Inf') / a mpfr('-inf') >>> float('-Inf') / -a mpfr('inf') >>> a / float('nan') mpfr('nan') >>> float('nan') / a mpfr('nan') >>> float('nan') / mpfr(0) mpfr('nan') >>> float('nan') / mpfr(0) mpfr('nan') >>> a + mpfr('Inf') mpfr('inf') >>> mpfr('Inf') + a mpfr('inf') >>> a + mpfr('-Inf') mpfr('-inf') >>> mpfr('-Inf') + a mpfr('-inf') >>> a + mpfr('nan') mpfr('nan') >>> mpfr('nan') + a mpfr('nan') >>> a - mpfr('Inf') mpfr('-inf') >>> mpfr('Inf') - a mpfr('inf') >>> a - mpfr('-Inf') mpfr('inf') >>> mpfr('-Inf') - a mpfr('-inf') >>> a - mpfr('nan') mpfr('nan') >>> mpfr('nan') - a mpfr('nan') >>> a * mpfr('Inf') mpfr('inf') >>> mpfr('Inf') * a mpfr('inf') >>> a * mpfr('-Inf') mpfr('-inf') >>> mpfr('-Inf') * a mpfr('-inf') >>> -a * mpfr('Inf') mpfr('-inf') >>> mpfr('Inf') * -a mpfr('-inf') >>> -a * mpfr('-Inf') mpfr('inf') >>> mpfr('-Inf') * -a mpfr('inf') >>> a * mpfr('nan') mpfr('nan') >>> mpfr('nan') * a mpfr('nan') >>> G.mpz(0) * mpfr('Inf') mpfr('nan') >>> G.mpz(0) * mpfr('-Inf') mpfr('nan') >>> mpfr('Inf') * mpfr(0) mpfr('nan') >>> mpfr('-Inf') * mpfr(0) mpfr('nan') >>> a / mpfr('Inf') mpfr('0.0') >>> -a / mpfr('Inf') mpfr('-0.0') >>> mpfr('Inf') / a mpfr('inf') >>> mpfr('Inf') / -a mpfr('-inf') >>> a / mpfr('-Inf') mpfr('-0.0') >>> -a / mpfr('-Inf') mpfr('0.0') >>> mpfr('-Inf') / a mpfr('-inf') >>> mpfr('-Inf') / -a mpfr('inf') >>> a / mpfr('nan') mpfr('nan') >>> mpfr('nan') / a mpfr('nan') >>> mpfr('nan') / mpfr(0) mpfr('nan') >>> mpfr('nan') / mpfr(0) mpfr('nan') >>> divmod(a, float('Inf')) (mpfr('0.0'), mpfr('12.34')) >>> divmod(a, float('-Inf')) (mpfr('-1.0'), mpfr('-inf')) >>> divmod(-a, float('Inf')) (mpfr('-1.0'), mpfr('inf')) >>> divmod(-a, float('-Inf')) (mpfr('0.0'), mpfr('-12.34')) >>> divmod(a, float('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(-a, float('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr(0), float('Inf')) (mpfr('0.0'), mpfr('0.0')) >>> divmod(mpfr(0), float('-Inf')) (mpfr('-0.0'), mpfr('-0.0')) >>> divmod(mpfr(0), float('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(float('Inf'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('-Inf'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('Inf'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('-Inf'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('nan'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('nan'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(float('Inf'), mpfr(0)) (mpfr('nan'), mpfr('nan')) >>> divmod(float('-Inf'), mpfr(0)) (mpfr('nan'), mpfr('nan')) >>> divmod(float('nan'), mpfr(0)) (mpfr('nan'), mpfr('nan')) >>> divmod(a, mpfr('Inf')) (mpfr('0.0'), mpfr('12.34')) >>> divmod(a, mpfr('-Inf')) (mpfr('-1.0'), mpfr('-inf')) >>> divmod(-a, mpfr('Inf')) (mpfr('-1.0'), mpfr('inf')) >>> divmod(-a, mpfr('-Inf')) (mpfr('0.0'), mpfr('-12.34')) >>> divmod(a, mpfr('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(-a, mpfr('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr(0), mpfr('Inf')) (mpfr('0.0'), mpfr('0.0')) >>> divmod(mpfr(0), mpfr('-Inf')) (mpfr('-0.0'), mpfr('-0.0')) >>> divmod(mpfr(0), mpfr('nan')) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('Inf'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('-Inf'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('Inf'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('-Inf'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('nan'), a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('nan'), -a) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('Inf'), mpfr(0)) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('-Inf'), mpfr(0)) (mpfr('nan'), mpfr('nan')) >>> divmod(mpfr('nan'), mpfr(0)) (mpfr('nan'), mpfr('nan'))