Java Math sin() method with Examples

The java.lang.Math.sin() returns the trignometric sine of an angle in between 0.0 and pi. If the argument is NaN or infinity, then the result is NaN. If the argument is zero, then the result is a zero with the same sign as the argument. The value returned will be between -1 and 1.

Syntax :

public static double sin(double a)
Parameter :
a : the value whose sine is to be returned.

Return :
This method returns the sine value of the argument.



Example 1 : To show working of java.lang.Math.sin() method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.sin() method
import java.lang.Math;
  
class Gfg {
  
    // driver code
    public static void main(String args[])
    {
        double a = 30;
          
        // converting values to radians
        double b = Math.toRadians(a);
  
        System.out.println(Math.sin(b));
  
        a = 45;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.sin(b));
  
        a = 60;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.sin(b));
  
        a = 90;
          
        // converting values to radians
        b = Math.toRadians(a);
  
        System.out.println(Math.sin(b));
    }
}

chevron_right


Output:

0.49999999999999994
0.7071067811865475
0.8660254037844386
1.0

Example 2 : To show working of java.lang.Math.sin() method when argument is NaN or infinity.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate working
// of java.lang.Math.cos() method infinity case
import java.lang.Math;
  
public class GFG {
      
    // Driver code
    public static void main(String[] args)
    {
        double positiveInfinity = 
               Double.POSITIVE_INFINITY;
        double negativeInfinity = 
               Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;
  
        // Here argument is negative infinity,
        // output will be NaN
        result = Math.sin(negativeInfinity);
        System.out.println(result);
  
        // Here argument is positive infinity,
        // output will also be NaN
        result = Math.sin(positiveInfinity);
        System.out.println(result);
  
        // Here argument is NaN, output will be NaN
        result = Math.sin(nan);
        System.out.println(result);
    }
}

chevron_right


Output:

NaN
NaN
NaN


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.