sin_value = math.sin(angle) print(f"Sin(30 degrees) = {sin_value}") 1.2 余弦函数(cos) 余弦函数可以用来计算一个角度的余弦值。使用方法与正弦函数类似: import math angle = math.pi / 3 # 60度角 cos_value = math.cos(angle) print(f"Cos(60 degrees) = {cos_value}") 1.3 正切函数(ta...
angle_in_degrees = 45 angle_in_radians = math.radians(angle_in_degrees) 计算45度(即π/4弧度)的余弦值 cos_value = math.cos(angle_in_radians) print("cos(45°) =", cos_value) 在这个例子中,我们将45度转换为弧度,然后计算其余弦值,结果约为0.7071。 四、应用实例与示例代码 为了更好地理解如...
# 存储计算结果 cosine_value = math.cos(angle_in_radians) tangent_value = math.tan(angle_in_radians) # 输出结果 print(f"The cosine of {angle_in_degrees} degrees is: {cosine_value}") print(f"The tangent of {angle_in_degrees} degrees is: {tangent_value}") 通过以上步骤,你可以在Pyth...
使用numpy计算余切的示例代码如下: importnumpyasnpdefcot_numpy(angle_in_degrees):# 将角度转换为弧度angle_in_radians=np.radians(angle_in_degrees)# 计算余切return1/np.tan(angle_in_radians)# 测试计算angle=45cot_value=cot_numpy(angle)print(f"cot({angle}) ={cot_value}") 1. 2. 3. 4. 5....
importmath# 正确的用法:将角度转换为弧度angle_in_degrees=45angle_in_radians=math.radians(angle_in_degrees)result=math.cos(angle_in_radians)print("Cosine of 45 degrees:",result) 1. 2. 3. 4. 5. 6. 7. 代码示例 让我们通过一个简单的例子来演示如何使用math.cos函数计算角度的余弦值。
importmathdefcosine(x,n):cosx =1sign =-1foriinrange(2, n,2): pi=22/7y=x*(pi/180) cosx = cosx + (sign*(y**i))/math.factorial(i) sign = -signreturncosx x=int(input("Enter the value of x in degrees:")) n=int(input("Enter the number of terms:")) ...
import math # Take the cosine angle in degrees x = 60 # Convert it into radians using math.radians() function rad = math.radians(x) # Find the cosine value using cos() method cosine = math.cos(rad) # Display the cosine ratio print("The cosine value of x is:", cosine) The...
from math import cos, radians # Create a string with spaces proportional to a cosine of x in degrees def make_dot_string(x): rad = radians(x) # cos works with radians numspaces = int(20 * cos(rad) + 20) # Scale to 0-40 spaces st = ' ' * numspaces + 'o...
In [146]: import math In [147]: help(math) Help on built-in module math: NAME math DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(...) acos(x) Return the arc cosine (measured in radians) of x. ...
cos_value = math.cos(angle_in_radians) print(f"The cosine of {angle_in_degrees} degrees is {cos_value}") 这里同样先将角度从度数转换为弧度,再使用math.cos()函数计算其余弦值。 3、计算正切值 正切函数tan(x)返回角度x的正切值。x的单位是弧度。