Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #67226 > unrolled thread
| Started by | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| First post | 2014-02-28 14:41 +0000 |
| Last post | 2014-02-28 10:17 -0800 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
Re: extend methods of decimal module Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2014-02-28 14:41 +0000
Re: extend methods of decimal module Wolfgang <xpysol@gmail.com> - 2014-02-28 06:52 -0800
Re: extend methods of decimal module "Mark H. Harris" <harrismh777@gmail.com> - 2014-02-28 10:17 -0800
| From | Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> |
|---|---|
| Date | 2014-02-28 14:41 +0000 |
| Subject | Re: extend methods of decimal module |
| Message-ID | <mailman.7472.1393598536.18130.python-list@python.org> |
Mark H. Harris <harrismh777 <at> gmail.com> writes:
>
> If you get a chance, take a look at the dmath.py code on:
>
> https://code.google.com/p/pythondecimallibrary/
>
Hi Mark,
here is an enhancement for your epx function.
Your current version comes with the disadvantage of potentially storing
extremely large values in n and d because of the multiplications in the
while loop:
q = D(x)
n = q
c = D(1)
d = D(1)
ex = 1 + q
prev_ex = D(0)
while (ex != prev_ex):
prev_ex = ex
c += 1
d *= c # can become a huge number
n *= q # this as well
ex += n/d
in general, large numbers are handled well by the Decimal class, but there
is a certain burden on these calculations and with VERY large numbers you
can also get a decimal.Overflow:
>>> exp(200000)
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
epx(190000)
File "C:\Python34\dmath_rev.py", line 27, in epx
n *= q
decimal.Overflow: [<class 'decimal.Overflow'>]
My re-write of the part above is:
q = D(x)
c = 1
new_element = q
ex = 1 + new_element
prev_ex = D(0)
while (ex != prev_ex):
prev_ex = ex
c += 1
# every new element in the series is a known fraction of the
# previous element,
# so there is no need to store large numbers
new_element *= q/c
ex += new_element
in my hands, this simple change increases performance (exact timing left to
you) and boost the range of possible calculations:
>>> epx2(1000000)
Decimal('3.033215396802087545086402141E+434294')
Cheers,
Wolfgang
[toc] | [next] | [standalone]
| From | Wolfgang <xpysol@gmail.com> |
|---|---|
| Date | 2014-02-28 06:52 -0800 |
| Message-ID | <8b1ae93d-6e92-4927-bbaf-2d8fd5e76114@googlegroups.com> |
| In reply to | #67226 |
Uhh, the curse of not copy-pasting everything: > >>> exp(200000) should, of course, read >>> epx(190000) > Traceback (most recent call last): > File "<pyshell#46>", line 1, in <module> > epx(190000) > File "C:\Python34\dmath_rev.py", line 27, in epx > n *= q > decimal.Overflow: [<class 'decimal.Overflow'>] >
[toc] | [prev] | [next] | [standalone]
| From | "Mark H. Harris" <harrismh777@gmail.com> |
|---|---|
| Date | 2014-02-28 10:17 -0800 |
| Message-ID | <8fc1043a-2ea2-4c8e-8ac4-c6829614c8cf@googlegroups.com> |
| In reply to | #67226 |
On Friday, February 28, 2014 8:41:49 AM UTC-6, Wolfgang Maier wrote: > Hi Mark, > > here is an enhancement for your epx function. > > Wolfgang hi Wolfgang, thanks much! As a matter of point in fact, I ran into this little snag and didn't understand it, because I was thinking that outside of memory there should not be any overflow for decimal in python / which means, I suppose, that the C accelerated decimal does have some built-in limitations. So, yes, I got some overflows too... Also, thanks for your comment about my D(str()) function... from Oscar to you, and everyone else... well that has to go. Well, now I gotta get busy and clean this thing up... Thanks again. marcus
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web