3

In Python 2.6, I found that the Decimal equivalent of sqrt(pi) is

Decimal(pi).sqrt()

Is there anything like that for sin, cos or other (inverse) trigonometric functions?

The docs only mention how to calculate cos and sin computationally. Decimal(pi).cos() doesn't exist nor does from decimal import cos

Update: the problem with using the default trigonometric functions is that it defeats the point of using Decimal if I convert them from float and back every time I want to calculate something. (Also typing Decimal around every calculation is annoying, but I guess I could make a wrapper function)

Mark
  • 18,730
  • 7
  • 107
  • 130

4 Answers4

6

Please refer to this.

http://docs.python.org/library/decimal.html#decimal-recipes

dan-boa
  • 590
  • 4
  • 10
5

The Decimal library was intended to support properly rounded, base-10 arithmetic primarily for financial calculations where precise control of rounding is required. It was not intended to be a general-purpose arbitrary precision library.

If you need a general purpose arbitrary precision library, look at mpmath or gmpy2.

casevh
  • 11,093
  • 1
  • 24
  • 35
4

The trig functions are in the math module

So:

>>> decimal.Decimal(math.cos(0.1))
Decimal('0.99500416527802570954008842818439006805419921875')

If you are looking for multi precision math library in Python that supports trig functions, Decimal isn't it. Take a look at mpmath. Mpmath supports trig function at arbitrary precision; decimal does not.

Thruston
  • 1,467
  • 17
  • 23
the wolf
  • 34,510
  • 13
  • 53
  • 71
  • Seems rather pointless to use Decimal for extra precision, then convert it to float for every calculation... – Mark Jun 08 '12 at 15:49
  • 1
    @Mark: Decimal does not have trig functions.You either need to write them all in Python in Decimal class or settle for what Python's math module has. Or, use a library, like mpmath, that supports decimal like precision and has trig functions. – the wolf Jun 08 '12 at 15:58
  • Because 0.1 is converted to a float first, it only has 53 bits of precision. So your result isn't accurate beyond 53 bits (around 17 digits). ` >>> gmpy2.cos(gmpy2.mpfr("0.1")) mpfr('0.9950041652780257660955619878038702948385762254150840359593528',200)` – casevh Jun 08 '12 at 18:35
  • @casevh: Agreed, but the OP seemed to be looking (at first) for trig functions with Decimal. He did not mention (at first) arbitrary precision. – the wolf Jun 09 '12 at 05:43
3

Values of sin(a) and cos(a) are rational numbers only for particular angles (see Link ) so you can't store values of sin(a) or cos(a) as Decimals without losing precision. This defeats the purpose of converting sin and cos values to Decimal and having built-in functions that return sin and cos values as Decimals.

Moreover, you can't store all rational numbers as Decimal without losing precision because python's decimals are (-1)**_sign * _int * 10**_exp so the divisor of a rational number must be 10 for Decimal to store it with full precision.

I guess @carrot-top's mpmath advice or some numeric algorithm (like Taylor series approximation) is the best you can get.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mikhail Korobov
  • 21,908
  • 8
  • 73
  • 65
  • 3
    The square root of a number isn't always rational either. The point is arbitrary precision, not infinite precision. Default functions don't even give you that. – nog642 Jan 18 '18 at 02:48