The Python math.sin() method is used to calculate the sine value of an angle in radians. Mathematically, the sine function is defined as the ratio of the opposite side of the given angle to the hypotenuse (the longest side of the triangle) in a right-angled triangle.
The most commonly used sine values are of angles 0, 30, 45, 60 and 90 degrees. The sine function retrieves the values between -1 to 1. This method raises a TypeError whenever we pass anything but a floating-point number as an argument to it.
Note − This function is not accessible directly, so we need to import math module and then we need to call this function using math static object.
SyntaxFollowing is the syntax of Python math.sin() method −
math.sin(x)Parameters
x − This must be a numeric value.
This method returns a numeric value between -1 and 1, which represents the sine of the angle.
ExampleThe following example shows the usage of the Python math.sin() method. Here, we are trying to pass the standard sine angles and find their trigonometric sine ratios using this method.
import math print ("sin(3) : ", math.sin(3)) print ("sin(-3) : ", math.sin(-3)) print ("sin(0) : ", math.sin(0)) print ("sin(math.pi) : ", math.sin(math.pi)) print ("sin(math.pi/2) : ", math.sin(math.pi/2))
When we run above program, it produces following result −
sin(3) : 0.14112000806 sin(-3) : -0.14112000806 sin(0) : 0.0 sin(math.pi) : 1.22464679915e-16 sin(math.pi/2) : 1.0Example
Not just the standard angles, this method can also be used to find the sine ratios for non-standard angles.
In this example, we are creating multiple number objects that hold non-standard angles in radians. These values are passed as arguments to this method in order to find their resultant sine ratios.
import math # If the sine angle is pi x = 5.48 sine = math.sin(x) print("The sine value of x is:", sine) # If the sine angle is pi/2 x = 1.34 sine = math.sin(x) print("The sine value of x is:", sine) # If the sine angle is 0 x = 0.78 sine = math.sin(x) print("The sine value of x is:", sine)
While executing the above code we get the following output −
The sine value of x is: -0.7195716728205075 The sine value of x is: 0.9734845416953194 The sine value of x is: 0.7032794192004103Example
Even though complex numbers are still considered as numbers, this method only accepts real numbers as arguments.
Let us look at scenario where we pass complex numbers as arguments to the sin() method. The method raises a TypeError.
import math # If the sine angle is a complex number x = 12-11j sine = math.sin(x) print("The sine value of x is:", sine)
Following is an output of the above code −
Traceback (most recent call last): File "C:\Users\Lenovo\Desktop\untitled.py", line 4, in <module> sine = math.sin(x) TypeError: must be real number, not complexExample
We can use the math.radians() method to convert an angle in degrees and pass it as an argument to the sin() method.
In the following example, we are creating a number object that holds a sine angle in degrees. Since, the sin() method takes arguments in radians, we can call the radians() method on this object to convert it into the corresponding radian value. We will then pass this radian value as an argument to this method and find its sine ratio.
import math # Take the sine angle in degrees x = 60 # Convert it into radians using math.radians() function rad = math.radians(x) # Find the sine value using sin() method sine = math.sin(rad) # Display the sine ratio print("The sine value of x is:", sine)
Output of the above code is as follows −
The sine value of x is: 0.8660254037844386
python_maths.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4