EN
JavaScript - Math.sin() method example
17 points
Math.sin()
is a static method that takes only one parameter and returns an approximation of sine mathematical function.
xxxxxxxxxx
1
console.log( Math.sin( 0 ) ); // 0 <- 0 degrees
2
console.log( Math.sin( 1.5707963267948966 ) ); // ~1 <- ~90 degrees == PI / 2
3
console.log( Math.sin( 3.1415926535897932 ) ); // ~0 <- ~180 degrees == PI
4
console.log( Math.sin( 4.71238898038469 ) ); // ~-1 <- ~270 degrees == -PI * (3/2)
5
console.log( Math.sin( 6.2831853071795850 ) ); // ~0 <- ~360 degrees == PI * 2
6
7
console.log( Math.sin( -1.5707963267948966 ) ); // ~-1 <- ~-90 degrees == -PI / 2
Note:
1.2246467991473532e-16
and-1.133107779529596e-15
should be equal to0
but they are not because of compuptation precision error.
Syntax | Math.sin(number) |
Parameters | number - integer or float number value that represents an angle in radians (primitive value). |
Result |
If the function can not be calculated it returns |
Description | sin is a static method that takes only one parameter and returns an approximation of sin(x) mathematical function. |
xxxxxxxxxx
1
var x1 = 0.0; // beginning of calculation in radians
2
var x2 = Math.PI / 2; // ending of calculation radians
3
4
var dx = Math.PI / 36; // calculation step in degrees
5
6
for (var rad = x1; rad <= x2; rad += dx) {
7
var y = Math.sin(rad);
8
console.log('sin(' + rad + ' rad) = ' + y);
9
}
xxxxxxxxxx
1
function calculateSin(degrees) {
2
var radians = (Math.PI / 180) * degrees;
3
return Math.sin(radians);
4
}
5
6
7
// Example:
8
9
var x1 = 0.0; // beginning of calculation in degrees
10
var x2 = 90.0; // ending of calculation degrees
11
12
var dx = 5.0; // calculation step in degrees
13
14
for (var deg = x1; deg <= x2; deg += dx) {
15
var y = calculateSin(deg);
16
console.log('sin(' + deg + ' deg) = ' + y);
17
}
xxxxxxxxxx
1
var x1 = 0.0; // beginning of sine chart
2
var x2 = 6 * 3.14; // end of sine chart
3
4
var dx = 3.14 / 4.0; // x axis step
5
var dy = 1.0 / 5.0; // y axis step
6
7
for (var rad = x1; rad < x2; rad += dx) {
8
var y1 = 0.0;
9
var y2 = Math.sin(rad) + 1;
10
var line = '';
11
for(var y = y1; y < y2; y += dy) {
12
line += ' ';
13
}
14
console.log(line + '+');
15
}
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style> #canvas { border: 1px solid black; } </style>
5
</head>
6
<body>
7
<canvas id="canvas" width="400" height="130"></canvas>
8
<script>
9
10
var canvas = document.querySelector('#canvas');
11
var context = canvas.getContext('2d');
12
13
// sine chart range
14
var x1 = 0; // 0 degress
15
var x2 = +2 * Math.PI; // +360 degress
16
var y1 = -1.0;
17
var y2 = +1.0;
18
19
var dx = 0.1;
20
21
var xRange = x2 - x1;
22
var yRange = y2 - y1;
23
24
function calculatePoint(x) {
25
var y = Math.sin(x);
26
27
// chart will be reversed horizontaly because of reversed canvas pixels
28
29
var nx = (x - x1) / xRange; // normalized x
30
var ny = 1.0 - (y - y1) / yRange; // normalized y
31
32
var point = {
33
x: nx * canvas.width,
34
y: ny * canvas.height
35
};
36
37
return point;
38
}
39
40
console.log('x range: <' + x1 + '; ' + x2 + '> // angles in radians');
41
console.log('y range: <' + y1 + '; ' + y2 + '>');
42
43
var point = calculatePoint(x1);
44
45
context.beginPath();
46
context.moveTo(point.x, point.y);
47
48
for (var x = x1 + dx; x < x2; x += dx) {
49
point = calculatePoint(x);
50
context.lineTo(point.x, point.y);
51
}
52
53
point = calculatePoint(x2);
54
context.lineTo(point.x, point.y);
55
context.stroke();
56
57
</script>
58
</body>
59
</html>