Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #59869 > unrolled thread
| Started by | "Kay Y. Jheallee" <nospam@use.net> |
|---|---|
| First post | 2013-11-18 14:14 +0000 |
| Last post | 2013-11-20 23:04 -0800 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.python
Setting longer default decimal precision "Kay Y. Jheallee" <nospam@use.net> - 2013-11-18 14:14 +0000
Re: Setting longer default decimal precision Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-18 14:43 +0000
Re: Setting longer default decimal precision Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-18 20:29 -0500
Re: Setting longer default decimal precision Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-20 14:02 +0000
Re: Setting longer default decimal precision Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-11-20 14:40 +0000
Re: Setting longer default decimal precision "Kay Y. Jheallee" <nospam@use.net> - 2013-11-20 19:34 +0000
Re: Setting longer default decimal precision Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-20 19:59 +0000
Re: Setting longer default decimal precision Tim Golden <mail@timgolden.me.uk> - 2013-11-20 20:04 +0000
Re: Setting longer default decimal precision Tim Golden <mail@timgolden.me.uk> - 2013-11-20 20:24 +0000
Re: Setting longer default decimal precision Larry Hudson <orgnut@yahoo.com> - 2013-11-20 23:04 -0800
| From | "Kay Y. Jheallee" <nospam@use.net> |
|---|---|
| Date | 2013-11-18 14:14 +0000 |
| Subject | Setting longer default decimal precision |
| Message-ID | <l6d7c7$402$1@speranza.aioe.org> |
Using 1/3 as an example, >>> 1./3 0.3333333333333333 >>> print "%.50f" % (1./3) 0.33333333333333331482961625624739099293947219848633 >>> print "%.50f" % (10./3) 3.33333333333333348136306995002087205648422241210938 >>> print "%.50f" % (100./3) 33.33333333333333570180911920033395290374755859375000 which seems to mean real (at least default) decimal precision is limited to "double", 16 digit precision (with rounding error). Is there a way to increase the real precision, preferably as the default? For instance, UBasic uses a "Words for fractionals", f, "Point(f)" system, where Point(f) sets the decimal display precision, .1^int(ln(65536^73)/ln(10)), with the last few digits usually garbage. Using "90*(pi/180)*180/pi" as an example to highlight the rounding error (4 = UBasic's f default value): Point(2)=.1^09: 89.999999306 Point(3)=.1^14: 89.9999999999944 Point(4)=.1^19: 89.9999999999999998772 Point(5)=.1^24: 89.999999999999999999999217 Point(7)=.1^33: 89.999999999999999999999999999999823 Point(10)=.1^48: 89.999999999999999999999999999999999999999999997686 Point(11)=.1^52: 89.9999999999999999999999999999999999999999999999999632 If not in the core program, is there a higher decimal precision module that can be added? -- Kill Hector dead, because Desi sent Milli.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-18 14:43 +0000 |
| Message-ID | <528a2796$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #59869 |
On Mon, 18 Nov 2013 14:14:33 +0000, Kay Y. Jheallee wrote: > Using 1/3 as an example, [snip examples] > which seems to mean real (at least default) decimal precision is limited > to "double", 16 digit precision (with rounding error). That's because Python floats actually are implemented as C doubles. And no, they're not configurable. However, Python also has a Decimal class, which (unlike floats) are actually decimal rather than binary, and include configurable precision. There is a performance hit -- prior to Python version 3.3, Decimal was quite slow, but in 3.3 they got a major speed increase and are now nearly as fast as floats. An example: py> import decimal py> x = decimal.Decimal(1)/3 py> decimal.getcontext().prec = 50 py> y = decimal.Decimal(1)/3 py> print(x, y) 0.3333333333333333333333333333 0.33333333333333333333333333333333333333333333333333 -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-11-18 20:29 -0500 |
| Message-ID | <mailman.2874.1384824591.18130.python-list@python.org> |
| In reply to | #59869 |
On Mon, 18 Nov 2013 14:14:33 +0000, "Kay Y. Jheallee" <nospam@use.net>
declaimed the following:
>Using 1/3 as an example,
>
> >>> 1./3
>0.3333333333333333
> >>> print "%.50f" % (1./3)
>0.33333333333333331482961625624739099293947219848633
> >>> print "%.50f" % (10./3)
>3.33333333333333348136306995002087205648422241210938
> >>> print "%.50f" % (100./3)
>33.33333333333333570180911920033395290374755859375000
>
>which seems to mean real (at least default) decimal precision
The default double precision FLOATING POINT is limited to ~15
significant digits. That's a hardware limit for double on most machines
(64-bits). Most floating point processors may use 80 bits internally, but
truncate back down on output.
Python has supported a DECIMAL package for some years, but math with
that package will be slower as it can't go through the FPU.
Check the help file for your installation, look for decimal
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-11-20 14:02 +0000 |
| Message-ID | <528cc0eb$0$29992$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #59869 |
Hi Kay, You emailed me off-list, but your email address is bouncing or invalid, so I have no way to email you back. Unless you have something private or personal to say, you should keep replies on the list here so that others can either answer your questions or learn from the responses. If you do have something private to say, you should use a real email address that accepts replies :-) I'm taking the liberty of republishing your comments to me here: [you wrote] Okay,but after I import "math" and "decimal", py> decimal.getcontext().prec=75 py> print decimal.Decimal(math.atan(1)) 0.78539816339744827899949086713604629039764404296875 though I set precision to 75, it only did the trig function to 50 places AND it is only right to 16 places, 0.785398163397448309615660845819875721049292349843776...(actual). [end quote] Here, you calculate the atan of 1 using floating point maths, that is, to the precision of C doubles (about 17 decimal places). After the calculation is performed using float, you then convert it to a Decimal, but it is too late, you can't retroactively regain precision. In a perfect world, this would work: math.atan(Decimal(1)) but alas, all the functions in the math module convert their arguments to float first, so even though your Decimal(1) could perform calculations to 75 decimal places, the math.atan function downgrades it to a regular float. Unfortunately, Decimals don't support high-precision trig functions. If you study the decimal.py module, you could possibly work out how to add support for trig functions, but they have no current support for them. You could try this third-party module: http://code.google.com/p/mpmath/ which claims to be arbitrary-precision maths for Python, but I've never used it. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-11-20 14:40 +0000 |
| Message-ID | <mailman.2962.1384958461.18130.python-list@python.org> |
| In reply to | #60077 |
On 20 November 2013 14:02, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > > but alas, all the functions in the math module convert their arguments to > float first, so even though your Decimal(1) could perform calculations to > 75 decimal places, the math.atan function downgrades it to a regular > float. > > Unfortunately, Decimals don't support high-precision trig functions. If > you study the decimal.py module, you could possibly work out how to add > support for trig functions, but they have no current support for them. The documentation has examples for how to make high precision sin() and cos() functions that work with Decimals. http://docs.python.org/2/library/decimal.html#recipes The basic idea is to sum terms of the Maclaurin series until it converges. For atan(x) the Maclaurin series is atan(x) = x - (1/3)x**3 + (1/5)x**5 - (1/7)x**7 + (1/9)x**9 + ... The nth term is given by f(n) = ((-1)**n)*(1/(2n+1))*x**(2n+1). The ratio test gives that that |f(n+1)/f(n)| = (2(n+1)+1)/(2n + 1) * x**2 which has a limiting value of x**2 so the series converges for |x| < 1 (unlike sin() and cos() that converge for all x). For values outside this range you can use the identity arctan(1/x) == sign(x)*pi/2 - arctan(x) to map the values back into the convergent range. This may still be problematic when x is close to 1 in which case an alternate Taylor series around x=1 could be used. You'd need to ensure that you were using enough digits for pi as well if you wanted to make this work. It's probably easiest just to use the mpmath library as Steven suggested (or gmpy2, or sympy which includes mpmath). Oscar
[toc] | [prev] | [next] | [standalone]
| From | "Kay Y. Jheallee" <nospam@use.net> |
|---|---|
| Date | 2013-11-20 19:34 +0000 |
| Message-ID | <l6j2st$na7$1@speranza.aioe.org> |
| In reply to | #60077 |
On 13.Nov.20.Wed 14:02, Steven D'Aprano wrote:> Hi Kay,
>
> You emailed me off-list, but your email address is bouncing or
invalid,
> so I have no way to email you back.
So THAT's where it went! Sorry about that...yes, it WAS meant
for the group :/!
> [you wrote]
> Okay,but after I import "math" and "decimal",
>
> py> decimal.getcontext().prec=75
> py> print decimal.Decimal(math.atan(1))
> 0.78539816339744827899949086713604629039764404296875
>
> though I set precision to 75, it only did the trig function to
> 50 places AND it is only right to 16 places,
>
> 0.785398163397448309615660845819875721049292349843776...
> (actual).
> [end quote]
>
>
> Here, you calculate the atan of 1 using floating point maths,
> that is, to the precision of C doubles (about 17 decimal
> places). After the calculation is performed using float, you
> then convert it to a Decimal,
> but it is too late, you can't retroactively regain precision.
>
> In a perfect world, this would work:
>
> math.atan(Decimal(1))
>
> but alas, all the functions in the math module convert their
> arguments to float first, so even though your Decimal(1)
> could perform calculations to 75 decimal places, the
> math.atan function downgrades it to a regular float.
Then that is useless! :(
> You could try this third-party module:
>
> http://code.google.com/p/mpmath/
Ah, that looks like just the puppy I'm looking for. :)
Okay then, I just installed the PortableApps version of Python,
but when I downloaded "mpmath-0.17.win32" the installer aborted
with "No Python installation found in the registry".
So I'm trying to install "setuptools 1.4" (and do it that way) at
https://pypi.python.org/pypi/setuptools/1.4
but where is the "Download" link ("Downloads" just shifts down to
the page section)? I'm just looking for the ".zip" file version
(if one exists), not ".tar.gz".
Thx!
--
Kill Hector dead, because Desi sent Milli.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-20 19:59 +0000 |
| Message-ID | <mailman.2985.1384977569.18130.python-list@python.org> |
| In reply to | #60128 |
On 20/11/2013 19:34, Kay Y. Jheallee wrote:
>
> Ah, that looks like just the puppy I'm looking for. :)
> Okay then, I just installed the PortableApps version of Python,
> but when I downloaded "mpmath-0.17.win32" the installer aborted with "No
> Python installation found in the registry".
> So I'm trying to install "setuptools 1.4" (and do it that way) at
>
> https://pypi.python.org/pypi/setuptools/1.4
>
> but where is the "Download" link ("Downloads" just shifts down to the
> page section)? I'm just looking for the ".zip" file version (if one
> exists), not ".tar.gz".
> Thx!
>
Advice from a long time Python Windows user that's aimed at everybody,
not just the OP. Always try and find a binary installer, it's far
easier than messing about with ".zip" or ".tar.gz" files. In particular
it avoids the infamous "error: Unable to find vcvarsall.bat", which in
plain English means you've not got Visual C++ installed or you've got
the wrong version.
A very good site to find binaries is this
http://www.lfd.uci.edu/~gohlke/pythonlibs/. You can safely ignore the
"Unofficial" at the top of the page, I've been using stuff from there
for years and never, ever had a problem.
--
Python is the second best programming language in the world.
But the best has yet to be invented. Christian Tismer
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2013-11-20 20:04 +0000 |
| Message-ID | <mailman.2986.1384977851.18130.python-list@python.org> |
| In reply to | #60128 |
On 20/11/2013 19:59, Mark Lawrence wrote:
> On 20/11/2013 19:34, Kay Y. Jheallee wrote:
>>
>> Ah, that looks like just the puppy I'm looking for. :)
>> Okay then, I just installed the PortableApps version of Python,
>> but when I downloaded "mpmath-0.17.win32" the installer aborted with "No
>> Python installation found in the registry".
>> So I'm trying to install "setuptools 1.4" (and do it that way) at
>>
>> https://pypi.python.org/pypi/setuptools/1.4
>>
>> but where is the "Download" link ("Downloads" just shifts down to the
>> page section)? I'm just looking for the ".zip" file version (if one
>> exists), not ".tar.gz".
>> Thx!
>>
>
> Advice from a long time Python Windows user that's aimed at everybody,
> not just the OP. Always try and find a binary installer, it's far
> easier than messing about with ".zip" or ".tar.gz" files. In particular
> it avoids the infamous "error: Unable to find vcvarsall.bat", which in
> plain English means you've not got Visual C++ installed or you've got
> the wrong version.
>
> A very good site to find binaries is this
> http://www.lfd.uci.edu/~gohlke/pythonlibs/. You can safely ignore the
> "Unofficial" at the top of the page, I've been using stuff from there
> for years and never, ever had a problem.
>
While Mark's advice here is fairly sound, the OP pointed out that they
are using PortableApps Python (which I've never tried myself). The key
point is that the binary installers expect to find a Python entry in the
registry to show them where to go.
For now I'm lazily going to point to the effbot's 10-year-old page on
the subject:
http://effbot.org/zone/python-register.htm
with the proviso that, if that doesn't work, the OP should come back and
we'll try to help some more.
If it's any consolation, this business (bootstrapping installation on
Windows) is improving, piece by piece. It's just not all there yet.
TJG
[toc] | [prev] | [next] | [standalone]
| From | Tim Golden <mail@timgolden.me.uk> |
|---|---|
| Date | 2013-11-20 20:24 +0000 |
| Message-ID | <mailman.2988.1384979058.18130.python-list@python.org> |
| In reply to | #60128 |
On 20/11/2013 19:34, Kay Y. Jheallee wrote:
> Ah, that looks like just the puppy I'm looking for. :)
> Okay then, I just installed the PortableApps version of Python,
> but when I downloaded "mpmath-0.17.win32" the installer aborted with "No
> Python installation found in the registry".
> So I'm trying to install "setuptools 1.4" (and do it that way) at
>
> https://pypi.python.org/pypi/setuptools/1.4
>
> but where is the "Download" link ("Downloads" just shifts down to the
> page section)? I'm just looking for the ".zip" file version (if one
> exists), not ".tar.gz".
Yes, unfortunately you seem to have hit the "sour spot" of installation:
Windows with a Portable Python.
I note that the setuptools page has a .whl, which is the brand new
Python binary installer. Although designed to be installed with tool
support, it is in fact a .zip file which you should be able to unpack
into c:\python<xy>\lib\site-packages. It might be worth a try.
Please feel to come back for more help if needs be: you've hit an
unfortunately bumpy patch in the Python experience and I'd prefer to
smooth things out for you than have to turn away in disgust :)
TJG
[toc] | [prev] | [next] | [standalone]
| From | Larry Hudson <orgnut@yahoo.com> |
|---|---|
| Date | 2013-11-20 23:04 -0800 |
| Message-ID | <-tidnWVDIb4ULRDPnZ2dnUVZ_vWdnZ2d@giganews.com> |
| In reply to | #60128 |
On 11/20/2013 11:34 AM, Kay Y. Jheallee wrote:
> On 13.Nov.20.Wed 14:02, Steven D'Aprano wrote:> Hi Kay,
> >
> > You emailed me off-list, but your email address is bouncing or invalid,
> > so I have no way to email you back.
>
> So THAT's where it went! Sorry about that...yes, it WAS meant for the group :/!
>
I don't know if this applies to you or not but...
I'm using Thunderbird with the News Group (not the News List) and it has two buttons, Reply and
Followup. Reply sends e-mail, Followup sends to the group. Very easy to use the wrong one (and
I have). :-(
-=- Larry -=-
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web