The "lying gun" and "standing gun" of trigonometric functions in Python | Representation of trigonometric functions in python


Hello everyone, welcome to Eraser's blog, today I'm going to talk about trigonometry in Python.
Don't worry, I'm not going to blow you away with dull math jargon, but I'll walk you through how these functions are represented in a humorous way.
let me start!
Let's Coding

1. Foreplay: basic concepts of trigonometry

Trigonometric functions are common concepts in mathematics, including sine (sin), cosine (cos), and tangent (tan), among others. They appear frequently in fields such as geometry, physics, and computer graphics. Don't worry, this isn't a math class, I'll try to explain the functions in a way that's easy to understand.

2. Sine function: sin

The sine function, as the name suggests, is to find a way to make the function seem to be "lying a gun". Take a look at the following Python code example:

import math

angle = 45  # 角度,单位:度
radians = math.radians(angle)  # 将角度转换为弧度

sine_value = math.sin(radians)  # 计算正弦值
print(f"当角度为{
      
      angle}度时,正弦值为{
      
      sine_value:.2f}")

In this code, I first convert the angle to radians and then use math.sin()a function to calculate the sine. When the angle is 45 degrees, the sine is 0.71. Therefore, the sine function is the representative of the "lying gun", which always makes you feel a little bit of its relaxation on the beach.

3. Cosine function: cos

The cosine function, it sounds like you're trying to find a way to make the function look like it's "standing guns". Here is a deduction for the cosine function:

angle = 60  # 角度,单位:度
radians = math.radians(angle)  # 将角度转换为弧度

cosine_value = math.cos(radians)  # 计算余弦值
print(f"当角度为{
      
      angle}度时,余弦值为{
      
      cosine_value:.2f}")

In this code, I also convert the angles to radians, then use math.cos()the function to calculate the cosine. When the angle is 60 degrees, the cosine value is 0.50. It's like the cosine function is always doing all kinds of tricks on the balance beam, and you can't help but imagine it thinking: "I can not only stand firm, but I can stand handsome!"

4. Tangent function: tan

The tangent function seems to be the "rebel" in the trigonometric function, and it always makes people feel that it is "rebelling". Let me take a look at the code below to unravel this mystery:

angle = 30  # 角度,单位:度
radians = math.radians(angle)  # 将角度转换为弧度

tangent_value = math.tan(radians)  # 计算正切值
print(f"当角度为{
      
      angle}度时,正切值为{
      
      tangent_value:.2f}")

In this code snippet, I do the same conversion from angles to radians, and then use math.tan()a function to calculate the tangent. When the angle is 30 degrees, the tangent is about 0.58. The tangent function is like a challenge to you, trying to tell you: "I may be a bit rebellious, but I also have my own value!"

5. Mischievous angle change

Sometimes, I may encounter situations where I need to convert angles between different units, such as converting between radians and degrees. Fortunately, there is such a small tool in Python that allows you to change the angle unit at will:

degree_angle = 90
radian_angle = math.radians(degree_angle)
print(f"{
      
      degree_angle}度 = {
      
      radian_angle:.2f}弧度")

new_degree_angle = math.degrees(radian_angle)
print(f"{
      
      radian_angle:.2f}弧度 = {
      
      new_degree_angle}度")

This code shows how to use the math.degrees()and math.radians()function to freely convert between degrees and radians. So, don't be afraid to play with angles, because I have the tools to play with them anytime.

6. Summary

Through this blog, I hope you have a better understanding of the trigonometric functions in Python: sine, cosine, and tangent. They are like a contest of "laying guns", "standing guns" and "muting", affecting the world of mathematics and programming in a light-hearted way.

Well, this time the blog is here! Hope you learned something about trigonometry while laughing.

Guess you like

Origin blog.csdn.net/hihell/article/details/132109886