Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60080

Re: Setting longer default decimal precision

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.059
X-Spam-Evidence '*H*': 0.88; '*S*': 0.00; 'float': 0.07; 'arguments': 0.09; 'used.': 0.09; 'cc:addr:python-list': 0.11; '(unlike': 0.16; 'cc:name:python list': 0.16; 'limiting': 0.16; 'range.': 0.16; 'subject:default': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'url:decimal': 0.16; 'wrote:': 0.18; 'library': 0.18; 'module': 0.19; 'examples': 0.20; 'cc:addr:python.org': 0.22; 'alternate': 0.24; 'module,': 0.24; 'math': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'first,': 0.26; 'possibly': 0.26; 'suggested': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; 'gives': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'steven': 0.31; 'probably': 0.32; 'regular': 0.32; 'url:python': 0.33; 'could': 0.34; 'basic': 0.35; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'functions.': 0.36; 'url:org': 0.36; 'easiest': 0.38; 'url:library': 0.38; 'enough': 0.39; 'how': 0.40; 'ensure': 0.60; 'even': 0.60; 'skip:u 10': 0.60; 'range': 0.61; 'back': 0.62; 'high': 0.63; 'term': 0.63; 'map': 0.64; 'sum': 0.64; 'series': 0.66; 'close': 0.67; 'study': 0.69; 'calculations': 0.84; 'oscar': 0.84; 'skip:| 10': 0.84; 'trig': 0.84; 'ratio': 0.91; 'subject:Setting': 0.96; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=n57mBurU/yKOHle+3UMItUX4jKpz4Z7QbuyAmb5QTQE=; b=G0+jr6tgKJoD9H3QuiY2CIUwoKySP5CTl+ZcSCB6lnmhfshwJDL4mtOTPb/N2nYUhy 5WB1+Z1VL9PyQwoSJ/CELUASSs06RP53wkZ34RUOoOhjgriirvlhnl5a86CyTJT0SD4W C/IA7Yi0aPUG2p5xw1Rpj4tsNJ70VvWcN6M4h4u/ko412hSjy7lf/RYb69P7Sy/TKxiQ XXWCTlDmFa7vQkz0akKsYeICIHrhbXJiaBvIX2vBz0gkqwK8DF6hvzS8aVh6n4xzsJKk b3P4oBiiHQSWZ9++VZHAINpfd8xjDWfghCCl+N4UmwmE5jngVLlSrBwCEa3dmaXQiivx CWeA==
X-Received by 10.58.39.97 with SMTP id o1mr654227vek.15.1384958456778; Wed, 20 Nov 2013 06:40:56 -0800 (PST)
MIME-Version 1.0
In-Reply-To <528cc0eb$0$29992$c3e8da3$5496439d@news.astraweb.com>
References <l6d7c7$402$1@speranza.aioe.org> <528cc0eb$0$29992$c3e8da3$5496439d@news.astraweb.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Wed, 20 Nov 2013 14:40:36 +0000
Subject Re: Setting longer default decimal precision
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2962.1384958461.18130.python-list@python.org> (permalink)
Lines 38
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384958461 news.xs4all.nl 15931 [2001:888:2000:d::a6]:47742
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60080

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web