Languages
[Edit]
EN

Java - Math.sin() method example

0 points
Created by:
Peter-Mortensen
558

Math sin is a static method that takes only one parameter and returns an approximation of sine mathematical function.

public class MathExample {

    public static void main(String[] args) {
        System.out.println( Math.sin( 0                  ) ); //   0 <-     0 degrees
        System.out.println( Math.sin( 1.5707963267948966 ) ); //  ~1 <-  ~90 degrees ==  PI / 2
        System.out.println( Math.sin( 3.1415926535897932 ) ); //  ~0 <- ~180 degrees ==  PI
        System.out.println( Math.sin( 4.71238898038469   ) ); // ~-1 <- ~270 degrees == -PI * (3/2)
        System.out.println( Math.sin( 6.2831853071795850 ) ); //  ~0 <- ~360 degrees ==  PI * 2

        System.out.println( Math.sin(-1.5707963267948966 ) ); // ~-1 <- ~-90 degrees == -PI / 2
    }
}

Note: 1.2246467991473532e-16 and -1.133107779529596e-15 should be equal to 0 but they are not because of compuptation precision error.


1. Documentation

Syntax
package java.lang;

public final class Math {

    public static double sin(double number) { ... }

}

Note: Classes in the java.lang package are imported automatically, so it is not necessary to do it manually - we use just Math.sin() call.

Parametersnumber - double value that represents an angle in radians (primitive value).
Result

number value calculated as sin(x) mathematical function in a range -1 to +1 (primitive value).

If the function can not calculate it returns NaN.

Descriptionsin is a static method that takes only one parameter and returns an approximation of sin(x) mathematical function.

2. Working with radians

public class MathExample {

    public static void main(String[] args) {
        double x1 = 0.0;          // beginning of calculation in radians
        double x2 = Math.PI / 2;  // ending of calculation radians

        double dx = Math.PI / 9;  // calculation step in degrees

        for (double rad = x1; rad <= x2; rad += dx) {
            double y = Math.sin(rad);
            System.out.println("sin(" + rad + " rad) = " + y);
        }
    }
}

Output: 

sin(0.0 rad) = 0.0
sin(0.3490658503988659 rad) = 0.3420201433256687
sin(0.6981317007977318 rad) = 0.6427876096865393
sin(1.0471975511965976 rad) = 0.8660254037844386
sin(1.3962634015954636 rad) = 0.984807753012208

3. Working with degrees

public class MathExample {

    static double calculateSin(double deg) {
        double radians = (Math.PI / 180) * deg;
        return Math.sin(radians);
    }

    public static void main(String[] args) {
        // Example:

        double x1 = 0.0;   // beginning of calculation in degrees
        double x2 = 90.0;  // ending of calculation degrees

        double dx = 15.0;  // calculation step in degrees

        for (double deg = x1; deg  <= x2; deg  += dx) {
            double y = calculateSin(deg );
            System.out.println("sin(" + deg + " deg) = " + y);
        }
    }
}

Output: 

sin(0.0 deg) = 0.0
sin(15.0 deg) = 0.25881904510252074
sin(30.0 deg) = 0.49999999999999994
sin(45.0 deg) = 0.7071067811865475
sin(60.0 deg) = 0.8660254037844386
sin(75.0 deg) = 0.9659258262890683
sin(90.0 deg) = 1.0

4. Reversed console plot example

public class MathExample {

    public static void main(String[] args) {
        double x1 = 0.0;      // beginning of sine chart
        double x2 = 2 * 3.14; // end of sine chart

        double dx = 3.14 / 4.0; // x axis step
        double dy = 1.0  / 5.0; // y axis step

        for (double rad = x1; rad < x2; rad += dx) {
            double y1 = 0.0;
            double y2 = Math.sin(rad) + 1;
            StringBuilder line = new StringBuilder();
            for(double y = y1; y < y2; y += dy) {
                line.append(" ");
            }
            System.out.println(line + "+");
        }
    }
}

Output: 

     +
         +
          +
         +
      +
  +
 +
  +

References

  1. Sine - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - Math object

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join