PageRenderTime 44ms CodeModel.GetById 39ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/test/test_dec.txt

http://gmpy.googlecode.com/
Plain Text | 122 lines | 112 code | 10 blank | 0 comment | 0 complexity | 1da208e01fdfc4461ac49391552d4ecc MD5 | raw file
  1Interoperability with Decimal
  2=============================
  3
  4    >>> import gmpy2 as G
  5    >>> from gmpy2 import mpz, mpq, mpfr
  6    >>> from decimal import Decimal
  7
  8Test comparisons
  9----------------
 10
 11Non-trivial comparions between mpfr and Decimal instances are only equal
 12when the fractional can be written at 1/2**n.
 13
 14    >>> mpfr('123.456') == Decimal('123.456')
 15    False
 16    >>> mpfr('123.5') == Decimal('123.5')
 17    True
 18    >>> mpq(123456,1000) == Decimal('123.456')
 19    True
 20    >>> mpz(123) == Decimal(123)
 21    True
 22    >>> mpfr('123.456') > Decimal('123.456')
 23    True
 24    >>> mpfr('123.456') < Decimal('123.456')
 25    False
 26    >>> Decimal('123.456') > mpfr('123.456')
 27    False
 28    >>> Decimal('123.456') < mpfr('123.456')
 29    True
 30
 31Test elementary operations
 32--------------------------
 33
 34    >>> mpz(23) == Decimal(23)
 35    True
 36    >>> mpz(Decimal('123.456'))
 37    mpz(123)
 38    >>> mpq(Decimal('123.456'))
 39    mpq(15432,125)
 40    >>> mpfr(Decimal('123.456'))
 41    mpfr('123.456')
 42
 43    >>> f = mpfr('123.456')
 44    >>> q = mpq('789123/1000')
 45    >>> z = mpz('234')
 46    >>> dd = Decimal('12.34')
 47    >>> fd = Decimal('123.456')
 48    >>> qd = Decimal('789.123')
 49    >>> zd = Decimal('234')
 50
 51    >>> f+dd
 52    mpfr('135.79599999999999')
 53    >>> dd+f
 54    mpfr('135.79599999999999')
 55    >>> q+dd
 56    mpfr('801.46300000000008')
 57    >>> dd+q
 58    mpfr('801.46300000000008')
 59    >>> z+dd
 60    mpfr('246.34')
 61    >>> dd+z
 62    mpfr('246.34')
 63    >>> f-dd
 64    mpfr('111.116')
 65    >>> dd-f
 66    mpfr('-111.116')
 67    >>> q-dd
 68    mpfr('776.78300000000002')
 69    >>> dd-q
 70    mpfr('-776.78300000000002')
 71    >>> z-dd
 72    mpfr('221.66')
 73    >>> dd-z
 74    mpfr('-221.66')
 75    >>> f*dd
 76    mpfr('1523.44704')
 77    >>> dd*f
 78    mpfr('1523.44704')
 79    >>> q*dd
 80    mpfr('9737.7778200000012')
 81    >>> dd*q
 82    mpfr('9737.7778200000012')
 83    >>> z*dd
 84    mpfr('2887.5599999999999')
 85    >>> dd*z
 86    mpfr('2887.5599999999999')
 87    >>> f/dd
 88    mpfr('10.00453808752026')
 89    >>> dd/f
 90    mpfr('0.099954639709694135')
 91    >>> q/dd
 92    mpfr('63.948379254457052')
 93    >>> dd/q
 94    mpfr('0.015637612894314319')
 95    >>> z/dd
 96    mpfr('18.962722852512155')
 97    >>> dd/z
 98    mpfr('0.052735042735042738')
 99
100    >>> G.ceil(dd)
101    mpfr('13.0')
102    >>> G.floor(dd)
103    mpfr('12.0')
104    >>> G.trunc(dd)
105    mpfr('12.0')
106    >>> mpfr(dd).precision
107    53
108
109    >>> G.sin(Decimal("123.456"))
110    mpfr('-0.80393736857282394')
111    >>> G.sqrt(Decimal("123.456"))
112    mpfr('11.111075555498667')
113    >>> G.is_nan(Decimal("123.456"))
114    False
115    >>> G.is_nan(Decimal("NaN"))
116    True
117    >>> G.is_nan(Decimal("Inf"))
118    False
119    >>> G.is_inf(Decimal("Inf"))
120    True
121    >>> G.is_inf(Decimal("-Inf"))
122    True