Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Setting longer default decimal precision

80 views
Skip to first unread message

Kay Y. Jheallee

unread,
Nov 18, 2013, 9:14:33 AM11/18/13
to
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.

Steven D'Aprano

unread,
Nov 18, 2013, 9:43:34 AM11/18/13
to
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
Message has been deleted

Steven D'Aprano

unread,
Nov 20, 2013, 9:02:20 AM11/20/13
to
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

Oscar Benjamin

unread,
Nov 20, 2013, 9:40:36 AM11/20/13
to Steven D'Aprano, Python List
On 20 November 2013 14:02, Steven D'Aprano
<steve+comp....@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

Kay Y. Jheallee

unread,
Nov 20, 2013, 2:34:55 PM11/20/13
to
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!

Mark Lawrence

unread,
Nov 20, 2013, 2:59:11 PM11/20/13
to pytho...@python.org
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

Tim Golden

unread,
Nov 20, 2013, 3:04:30 PM11/20/13
to pytho...@python.org
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

Tim Golden

unread,
Nov 20, 2013, 3:24:37 PM11/20/13
to pytho...@python.org
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

Larry Hudson

unread,
Nov 21, 2013, 2:04:40 AM11/21/13
to
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 -=-

0 new messages