[英]Calculation of trigonometric functions in python
Python如何計算三角函數? 我嘗試計算使用
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
我越來越
x = 0.1
這是為什么? 在一個普通的計算器(弧度)中,我得到0.001
在Python 2中, /
是整數除法,您需要導入__future__ .division
以進行浮動除法:
>>> from __future__ import division
>>> import math
>>> x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
>>> x
0.001
在python2.x中 , python進行整數除法 。 因此,您需要從程序頂部的__future__
庫中導入division
。
from __future__ import division
x = ((0.1-0.001)/2)*math.sin(((1/20)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2)+0.001
print x
只是讓你的整數,如2
浮動2.0
,其他的Python 2.x使用整數除法,也被稱為地板師 (朝負無窮如四舍五入-9/8
給出了-2
, 9/8
給出1
),除以整數其他時候整數(無論是純數還是長整數):
x = ((0.1-0.001)/2.0)*math.sin(((1/20.0)*math.pi*20)+(0.5*math.pi*1))+((0.1-0.001)/2.0)+0.001
和:
print x
0.001
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.