Python 2 Decimal的Sin,cos等?

时间:2021-05-28 19:57:57

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

在Python 2.6中,我发现sqrt(pi)的Decimal等价物是

Decimal(pi).sqrt()

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

是否有类似sin,cos或其他(逆)三角函数的东西?

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

文档只提到如何计算cos和sin。十进制(pi).cos()不存在,也不存在十进制导入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)

更新:使用默认三角函数的问题是,如果我每次想要计算某些东西时将它们从float和back转换它就会失败使用Decimal的点。 (在每次计算周围输入Decimal也很烦人,但我想我可以做一个包装函数)

4 个解决方案

#1


4  

Values of sin(a) and cos(a) are rational numbers only for particular angles (see http://planetmath.org/encyclopedia/RationalSineAndCosine.html ) 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.

sin(a)和cos(a)的值仅对于特定角度是有理数(参见http://planetmath.org/encyclopedia/RationalSineAndCosine.html),因此您不能存储sin(a)或cos(a的值) )作为小数而不会丢失精度。这违背了将sin和cos值转换为Decimal并具有将sin和cos值作为Decimal返回的内置函数的目的。

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.

此外,你不能将所有有理数存储为Decimal而不会丢失精度,因为python的小数是(-1)** _ sign * _int * 10 ** _ exp所以有理数的除数必须为10才能使Decimal存储为满精确。

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

我猜@ carrot-top的mpmath建议或一些数值算法(如Taylor系列近似)是你能得到的最好的。

#2


6  

Please refer to this.

请参考这个。

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

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

#3


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.

Decimal库旨在支持适当舍入的base-10算法,主要用于需要精确控制舍入的财务计算。它不是一个通用的任意精度库。

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

如果您需要通用的任意精度库,请查看mpmath或gmpy2。

#4


4  

The trig functions are in the math module

trig函数位于数学模块中

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.

如果您正在寻找支持trig函数的Python中的多精度数学库,那么Decimal就不是了。看看mpmath。 Mpmath支持任意精度的trig函数;小数没有。

#1


4  

Values of sin(a) and cos(a) are rational numbers only for particular angles (see http://planetmath.org/encyclopedia/RationalSineAndCosine.html ) 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.

sin(a)和cos(a)的值仅对于特定角度是有理数(参见http://planetmath.org/encyclopedia/RationalSineAndCosine.html),因此您不能存储sin(a)或cos(a的值) )作为小数而不会丢失精度。这违背了将sin和cos值转换为Decimal并具有将sin和cos值作为Decimal返回的内置函数的目的。

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.

此外,你不能将所有有理数存储为Decimal而不会丢失精度,因为python的小数是(-1)** _ sign * _int * 10 ** _ exp所以有理数的除数必须为10才能使Decimal存储为满精确。

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

我猜@ carrot-top的mpmath建议或一些数值算法(如Taylor系列近似)是你能得到的最好的。

#2


6  

Please refer to this.

请参考这个。

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

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

#3


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.

Decimal库旨在支持适当舍入的base-10算法,主要用于需要精确控制舍入的财务计算。它不是一个通用的任意精度库。

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

如果您需要通用的任意精度库,请查看mpmath或gmpy2。

#4


4  

The trig functions are in the math module

trig函数位于数学模块中

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.

如果您正在寻找支持trig函数的Python中的多精度数学库,那么Decimal就不是了。看看mpmath。 Mpmath支持任意精度的trig函数;小数没有。