Python:具有三角函数的小数 [英] Python: Decimals with trigonometric functions

查看:82
本文介绍了Python:具有三角函数的小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题,看看:

I'm having a little problem, take a look:

>>> import math
>>> math.sin(math.pi)
1.2246467991473532e-16

这不是我在微积分课上学到的(实际上是0)

This is not what I learnt in my Calculus class (It was 0, actually)

所以,现在,我的问题:

So, now, my question:

我需要使用Python执行一些繁琐的三角演算.我可以使用哪个库来获取正确的值?

I need to perform some heavy trigonometric calculus with Python. What library can I use to get correct values?

我可以使用十进制吗?

对不起,我的意思是其他事情.

Sorry, What I mean is other thing.

我想要的是某种方法:

>>> awesome_lib.sin(180)
0

或者这个:

>>> awesome_lib.sin(Decimal("180"))
0

我需要一个表现出色的三角积分的天秤座.大家都知道180°的罪孽为0,我需要一个可以做到这一点的图书馆.

I need a libraray that performs good trigonometric calculus. Everybody knows that sin 180° is 0, I need a library that can do that too.

推荐答案

1.2246467991473532e-16 接近于 0 -- 小数点和第一位有效数字之间有 16 个零 -- 很多因为 3.1415926535897931 ( math.pi 的值)接近pi.答案是正确的,精确到小数点后十六位!

1.2246467991473532e-16 is close to 0 -- there are 16 zeroes between the decimal point and the first significant digit -- much as 3.1415926535897931 (the value of math.pi) is close to pi. The answer is correct to sixteen decimal places!

因此,如果您希望 sin(pi)等于0,只需将其舍入到合理的小数位数即可.15对我来说看起来不错,并且对于任何应用程序都应该足够:

So if you want sin(pi) to equal 0, simply round it to a reasonable number of decimal places. 15 looks good to me and should be plenty for any application:

print round(math.sin(math.pi), 15)

这篇关于Python:具有三角函数的小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆