Fedora Linux Support Community & Resources Center
  #1  
Old 27th August 2009, 09:21 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,687
linuxfirefox
Python high-precision for trig functions

Suppose I want to calculate the cosine of 2 radians to 50 decimal places. I tried using the decimal class like this:
Code:
>>> import math
>>> from decimal import *
>>> getcontext().prec = 50
>>> a = Decimal(2)
>>> s = Decimal(str(math.cos(a)))
>>> s
Decimal("-0.416146836547")
So I'm not getting 50 decimal places. If I leave out the str() string conversion then I get "TypeError: Cannot convert float to Decimal. First convert the float to a string". I'm sure this is easy to do and I'm missing something obvious (I'm still a Python n00b). What am I missing?
Reply With Quote
  #2  
Old 31st August 2009, 08:57 AM
PantheraLeo Offline
Registered User
 
Join Date: Mar 2008
Posts: 27
linuxfedorafirefox
Not being a big python programmer, I'm going to step out on a limb and say the 'decimal' data type isn't fully defined. If it's like C/C++ that Python is suppose to be interoperable with, it looks like when the compiler is told what a 'Decimal' data type is it just not told how to convert it to or from a 'float' data type.
Reply With Quote
  #3  
Old 31st August 2009, 10:34 AM
aleph Offline
Banned (for/from) behaving just like everybody else!
 
Join Date: Jul 2007
Location: Nanjing, China
Posts: 1,332
linuxfedorafirefox
Conversion from a Decimal to a float is well supported. Just apply the built-in float() callable.

On the other hand, converting a float instance to a Decimal cannot be well-defined without extra information, I think. A Decimal instance is supposed to be "as precise as needed", just like when a human being performs arithmetic with pencils and paper. However, a float is already a lossy representation of a real number, so we don't gain much by converting one to a Decimal. Converting a text string to Decimal makes better sense though, for a string literal (which can be arbitrarily long within certain practical limits) is compatible with our "natural", "human" and variable-precision notation of real numbers.

OK so much for my own buzzing noise Please refer to the PEP for Decimal type for a sketch of what the Python czars thought when the decimal module was conceived.

Python's support for decimal number is still in development, so there is an absence of some methods such as trig and other transcendental functions. However, I'm not sure how practical they are -- Is the precision gained worth the CPU time and memory consumption? For some applications, maybe yes; but I'm afraid it's "no" most of the time.
__________________
Code:
from rlyeh import cthulhu
cthulhu.fhtagn()
Reply With Quote
  #4  
Old 2nd September 2009, 05:04 PM
Gödel Offline
Registered User
 
Join Date: Jul 2009
Location: London,England
Posts: 1,102
linuxfedorafirefox
Also can use mpmath library from sympy package ('yum install sympy'), which can set the precision in decimal places (dps) or binary bits (prec). Note that the dps is applied correctly when converting to a string

Code:
>>> from sympy.mpmath import *
>>> mp.dps=50
>>> cos(2)
mpf('-0.41614683654714238699756822950076218976600077107554512')
>>> s=str(cos(2))
>>> s
'-0.41614683654714238699756822950076218976600077107555'
For precise bounds on calculations you can try the interval arithmetic python package (mpmath also has some support for intervals, but not transcendental functions) (you need the crlibm package, and use './configure CFLAGS=-fPIC')

Code:
$ sudo yum install python-setuptools-devel
$ sudo easy_install pyinterval

$ python
Python 2.6 (r26:66714, Jun  8 2009, 16:07:29) 
[GCC 4.4.0 20090506 (Red Hat 4.4.0-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from interval import imath
>>> imath.cos(2)
interval([-0.41614683654714241, -0.41614683654714235])
which means, cos(2) definitely lies in that interval.
Reply With Quote
Reply

Tags
functions, highprecision, python, trig

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python, IDLE, Python-tools Gnome. Deonis Using Fedora 2 12th March 2009 03:44 AM
python and python-irclib problem LostSon Programming & Packaging 3 6th March 2008 11:12 PM
Processing Conflict: python-devel conflicts python< 2.3.4-13.1 guarriman Using Fedora 0 15th September 2005 05:53 PM


Current GMT-time: 00:27 (Saturday, 01-11-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Pancevo Travel Photos - Palera Photos on Instagram - Altamura Photos on Instagram