| by Arround The Web | No comments

How to Calculate Sine Values with JavaScript Math sin() Method?

Math is required in every aspect of programming ranging from frontend to backend, for aligning the animation and transitions to building the complex logic for the application. Math makes the programmer’s lives a lot easier by providing an easier way to perform several tasks and also helps in data validation or pattern-matching scenarios. Luckily! JavaScript offers the “Math” object which has various properties and methods and for performing specifically the “sin” trigonometry function, its “sin()” method is used.

This blog explains the process to find the sine values using the Math sin() method of JavaScript.

How to Calculate Sine Values with JavaScript Math sin() Method?

The “sin()” method accepts the numeric value in the radian type and performs the trigonometry sin function over the provided value. The returned value always lies between the “1” and “-1” and can be “NAN” if the provided value is not in the numeric type.

Syntax

The syntax for JavaScript Math.sin() method is stated below:

Math.sin(val)

The “val” is the numeric value in the radian format. If the developer has a value in degree then it must be first converted into radians, then the obtained radian will be passed to “sin()” method. The formula which is used for conversion is stated below:

Visit the below table where the mostly used degrees are converted into radians

Degree 0 30 45 60 90 180 270 360
Radians 0 0.523 0.79 1.047 1.57 3.14 4.712 6.28

Let’s have a couple of examples for the understanding of the “sin()” method.

Example 1: Applying sin() method to Positive Values

In this example, the behavior of the “sin()” method with the “positive” degrees is going to be described by  passing the converted value of corresponding degree values into the radian number. Then, insert these values into the “sin()” method parenthesis:

<script>
  console.log("Sine for 60 Degree: " + Math.sin(1.047))
  console.log("Sine for 90 Degree: " + Math.sin(1.57))
  console.log("Sine for 180 Degree: " + Math.sin(3.14))
  console.log("Sine for 270 Degree: " + Math.sin(4.712))
  console.log("Sine for 360 Degree: " + Math.sin(6.28))
</script>

In the above lines of code, the radian values for corresponding degree values are inserted inside the “Math.sin()” function. The positive radian values have been created via the formula described above. The “+” is a sign of concatenation, combining the text and method results to display them next to each other over the console.

After the compilation of above-stated code, the console window looks like this:

The output shows that the sin() method successfully returns the values for each provided radian type value of degrees.

Example 2: Where sin() Method Gives Zero and NaN

In this case, the behavior of a “sin()” method with the values of “0”, “Empty Parenthesis”, “undefined”, and “String” is going to be found, as shown below:

<script>
  console.log("Sine of 0 : " + Math.sin(0))
  console.log("Sine When Parenthesis are Empty: " + Math.sin())
  console.log("Sine of undefined Value: " + Math.sin(undefined))
  console.log("Sine of String Value: " + Math.sin('Linux'))
</script>

As stated above, different false has been provided to the “sin()” method to retrieve its behavior and find where this method returns “NaN” and “Zero”.

The output generated after the compilation is shown below:

The output shows the undefined, empty, or string value is not acceptable by the “sin()” method and it returns “NaN” (Not a Number) in response to them. Moreover, in the case of “0” sin() method returns the “0” value.

Example 3: Applying sin() Method on Negative Values

The “sin()” method can also be applied to negative degrees by converting them into radians, the same as in the case of positive degrees. The table of conversion for some negative degrees into radians is stated below:

Degree 0 -30 -45 -60 -90 -180 -270 -360
Radians 0 -0.523 -0.79 -1.047 -1.57 -3.14 -4.712 -6.28

Let’s insert some of these negative radians into the “sin()” method:

<script>
  console.log("Sine for -60 Degree: " + Math.sin(-1.047))

  console.log("Sine for -90 Degree: " + Math.sin(-1.57))
  console.log("Sine for -180 Degree: " + Math.sin(-3.14))
  console.log("Sine for -270 Degree: " + Math.sin(-4.712))
  console.log("Sine for -360 Degree: " + Math.sin(-6.28))
</script>

After the compilation of above code:

The output shows that the “sin()” method has been successfully applied to negative degrees.

Conclusion

The sine values are calculated by converting the provided degree values into radian format and then passing the generated values directly in the “sin()” method. This method returns “0” only when the value of “0” is passed to the “sin()” method and prints “NaN” in the case of empty, undefined, or String value. This blog has explained the process to calculate sine values using the “Math.sin()” method.

Share Button

Source: linuxhint.com

Leave a Reply