Radian conversion and trigonometric functions in Python



[python] view plain copy 
1.<strong> 37 import math  
2. 38 a = int(raw_input("a = "))  
3. 39 b = int(raw_input("b = "))  
4. 40 c = int(raw_input("c = "))  
5. 41 print math.acos((a**2 + b**2 - c**2)/(2 * a * b)) * 180 /math.pi</strong>  


This is to achieve the angle before finding the two angles of a triangle; 

OK Now let's look at some basic knowledge

Knowledge point 1:

The math module in Python implements many mathematical operation functions for floating-point numbers. These functions are generally simple encapsulations of the functions of the same name in the platform C library, so in general, the results of calculations under different platforms may be slightly different, sometimes even a big discrepancy   

 Function (Method) Description Example 
 acos(x) finds the arc cosine of x (result in radians) acos(2.0) equals 0.0 
 asin(x) finds the arc sine of x (result in radians asin(0.0) equals 0.0 
 atan(x) finds the arctangent of x (result is in radians) atan(0.0) equals 0.0 
 ceil(x) rounds x, the result is the smallest integer not less than x ceil(9.2) is equal to 10.0
  ceil(-9.8) equals -9.0 
 cos(x) finds the cosine of x (x is in radians) cos(0.0) equals 1.0 
 exp(x) exponentiation function e` exp(1.0) is equal to 2.71828 
  exp(2.0) equals 7.38906 
 fabs(x) Find the absolute value of x fabs(5.1) is equal to 5.1 
  fabs(-5.1) equals 5.1 
 floor(x) rounds x, the result is the largest integer not greater than x floor(9.2) is equal to 9.0 
  floor(-9.8) equals -10.0 
 fmod(x,y) finds the remainder of x/y, the result is the floating point number fmod(9.8,4.0) is equal to 1.8 
 hypot(x,y) Find the length of the hypotenuse of a right triangle, the length of the straight side is x and y: Sqrt(x2-y2) hypot(3.0,4.0) equals 5.0 
 log10(x) Find the logarithm of x (base 10) log10(10.0) equals 1.0 
  log10(100.0) equals 2.0 
 pow(x,y) find the y power of x (xy) pow(2.7,7.0) is equal to 128.0 
   pow(9.0,0.5) equals 3.0 
 sin(x) finds the sine of x (x is in radians) sin(0.0) equals 0.0 
 sqrt(x) find the square root of x sqrt(900.0) equals 30.0 
 tan(x) finds the tangent of x (x is in radians) tan(0.0) equals 0.0 


 

  The cmath module contains some functions for complex number operations. The functions of the cmath module are basically the same as the functions of the math module, the difference is that the cmath module operates on complex numbers, and the math module operates on mathematical operations.

>>> cmath.sqrt(-1)
 1j
 >>> cmath.sqrt(9)
 (3+0y)
 >>> cmath.sin(1)
 (0.8414709848078965+0y)
 >>> cmath.log10(100)
 (2+0y) 
 
 

Knowledge point 2

The relationship between radians and angles

1. Two units of angle 
"Radians" and "degrees" are two different units for measuring the magnitude of an angle. Just like "meter" and "city ruler" are two different units for measuring length. 
    It is stipulated in flash: the angle in the rotation angle (rotation) is in "degrees"; and the angle in the trigonometric function is in "radians". This rule is the first thing we have to remember! ! ! For example: rotation2-- is a rotation of "2 degrees"; sin(π/2)-- is the sine of an angle whose size is "π/2 radians". 
   
      Second, the definition of radian 
      The so-called "definition of radian" means, how is the angle size of 1 radian defined? 
      We know that the definition of "degree" is, "Two rays shoot from the center of the circle to the circumference, forming an arc with an included angle and the included angle directly opposite. When the length of this arc is exactly equal to 1/360 of the circumference of the circumference, the two rays The size of the included angle is 1 degree.(Figure 1) 
    
      Then, how is the radian defined? The definition of radian is: two rays are shot from the center of the circle to the circumference, forming an included angle and an arc that is exactly opposite to the included angle. When the arc length is exactly equal to the radius of the circle, the angle between the two rays is 1 radian. (Figure 2) 
        For comparison, these two definitions of degrees and radians are very similar. The only difference between them is the length of the arc subtended by the angle. A degree is equal to 1/360 of the circumference of a circle, while a radian is equal to the radius. 
        Simply put, the definition of radian is that when the length of the arc subtended by the angle is equal to the radius, the magnitude of the angle is 1 radian. 


Pictures related to this topic are as follows: 

  


      The length of the arc subtended by an angle is several times the radius, so the size of the angle is a few radians. 
Their relationship can be expressed and calculated by the following formula: 
      Angle (radians) = arc length/radius 
The circumference of a circle is 2π times the radius, so a circumference angle (360 degrees) is 2π radians. 
The length of a semicircle is π times the radius, so a straight angle (180 degrees) is π radians. 

     Third, the conversion between degrees and radians 
     As stated above, a straight angle is π radians. 
i.e. 180 degrees = π radians 
From this it can be seen that: 
      1 degree = π/180 radians (≈0.017453 radians) 
Hence, the formula for converting degrees to radians: 
      Radians = degrees × π/180 
E.g: 
       90°=90×π/180 = π/2 radians 
       60°=60×π/180 = π/3 radians 
       45°=45×π/180 = π/4 radians 
       30°=30×π/180 =π/6 radians 
       120°=120×π/180 = 2π/3 radians 

Conversely, how to calculate radians into degrees? 
Because π radians = 180° 
So 1 radian = 180°/π (≈57.3°) 
Therefore, the formula for converting radians to degrees is available: 
       Degree = radian × 180°/π 
E.g: 
       4π/3 radians = 4π/3 × 180°/π 
      = 240° 

Maybe some friends will say that it is easy to make a mistake whether to multiply by "π/180" or "180°/π". In fact, you just need to remember: π is π radians, and 180 is 180 degrees. What unit I want to turn into, I have to put the unit that has this unit on the molecule. That is to say, if I want to convert it into radians, I have to put π radians on the numerator -- multiply by π/180. Also, 1 degree is much smaller than 1 radian, about only 0.017453 radians (π/180 ≈ 0.017453). Therefore, after converting degrees into radians, the number must be smaller, so when converting to radians, it must be multiplied by π/180. If you can think about it like this, you can't go wrong. 

Write "π" as "PI" in AS code. And because "π" and "sin" are both "mathematical functions", it is required to add "Math." (Math is the abbreviation of "Mathematics" in English), and write "Math.PI", "Math.sin". 
So sin30° has to be written as Math.sin (30*Math.PI/180). The part in parentheses is to convert 30° into radians, that is, 30×π/180.

OK After understanding the above two knowledge points, I believe you have understood; 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324524029&siteId=291194637