EN
Java - Math.sin() method example
0 points
Math
sin
is a static method that takes only one parameter and returns an approximation of sine mathematical function.
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
System.out.println( Math.sin( 0 ) ); // 0 <- 0 degrees
5
System.out.println( Math.sin( 1.5707963267948966 ) ); // ~1 <- ~90 degrees == PI / 2
6
System.out.println( Math.sin( 3.1415926535897932 ) ); // ~0 <- ~180 degrees == PI
7
System.out.println( Math.sin( 4.71238898038469 ) ); // ~-1 <- ~270 degrees == -PI * (3/2)
8
System.out.println( Math.sin( 6.2831853071795850 ) ); // ~0 <- ~360 degrees == PI * 2
9
10
System.out.println( Math.sin(-1.5707963267948966 ) ); // ~-1 <- ~-90 degrees == -PI / 2
11
}
12
}
Note:
1.2246467991473532e-16
and-1.133107779529596e-15
should be equal to0
but they are not because of compuptation precision error.
Syntax |
xxxxxxxxxx 1 package java.lang; 2 3 public final class Math { 4 5 public static double sin(double number) { ... } 6 7 }
|
Parameters | number - double value that represents an angle in radians (primitive value). |
Result |
If the function can not calculate it returns |
Description | sin is a static method that takes only one parameter and returns an approximation of sin(x) mathematical function. |
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
double x1 = 0.0; // beginning of calculation in radians
5
double x2 = Math.PI / 2; // ending of calculation radians
6
7
double dx = Math.PI / 9; // calculation step in degrees
8
9
for (double rad = x1; rad <= x2; rad += dx) {
10
double y = Math.sin(rad);
11
System.out.println("sin(" + rad + " rad) = " + y);
12
}
13
}
14
}
Output:
xxxxxxxxxx
1
sin(0.0 rad) = 0.0
2
sin(0.3490658503988659 rad) = 0.3420201433256687
3
sin(0.6981317007977318 rad) = 0.6427876096865393
4
sin(1.0471975511965976 rad) = 0.8660254037844386
5
sin(1.3962634015954636 rad) = 0.984807753012208
xxxxxxxxxx
1
public class MathExample {
2
3
static double calculateSin(double deg) {
4
double radians = (Math.PI / 180) * deg;
5
return Math.sin(radians);
6
}
7
8
public static void main(String[] args) {
9
// Example:
10
11
double x1 = 0.0; // beginning of calculation in degrees
12
double x2 = 90.0; // ending of calculation degrees
13
14
double dx = 15.0; // calculation step in degrees
15
16
for (double deg = x1; deg <= x2; deg += dx) {
17
double y = calculateSin(deg );
18
System.out.println("sin(" + deg + " deg) = " + y);
19
}
20
}
21
}
Output:
xxxxxxxxxx
1
sin(0.0 deg) = 0.0
2
sin(15.0 deg) = 0.25881904510252074
3
sin(30.0 deg) = 0.49999999999999994
4
sin(45.0 deg) = 0.7071067811865475
5
sin(60.0 deg) = 0.8660254037844386
6
sin(75.0 deg) = 0.9659258262890683
7
sin(90.0 deg) = 1.0
xxxxxxxxxx
1
public class MathExample {
2
3
public static void main(String[] args) {
4
double x1 = 0.0; // beginning of sine chart
5
double x2 = 2 * 3.14; // end of sine chart
6
7
double dx = 3.14 / 4.0; // x axis step
8
double dy = 1.0 / 5.0; // y axis step
9
10
for (double rad = x1; rad < x2; rad += dx) {
11
double y1 = 0.0;
12
double y2 = Math.sin(rad) + 1;
13
StringBuilder line = new StringBuilder();
14
for(double y = y1; y < y2; y += dy) {
15
line.append(" ");
16
}
17
System.out.println(line + "+");
18
}
19
}
20
}
Output:
xxxxxxxxxx
1
+
2
+
3
+
4
+
5
+
6
+
7
+
8
+