EN
JavaScript - Math.cos() method example
15 points
The Math.cos()
function returns the cosine of the specified angle in radians in the range -1
to +1
.
xxxxxxxxxx
1
console.log( Math.cos( 0 ) ); // 1 <- 0 degrees
2
console.log( Math.cos( 1.5707963267948966 ) ); // ~0 <- ~90 degrees == PI / 2
3
console.log( Math.cos( 3.1415926535897932 ) ); // ~-1 <- ~180 degrees == PI
4
console.log( Math.cos( 4.71238898038469 ) ); // ~0 <- ~270 degrees == -PI * (3 / 2)
5
console.log( Math.cos( 6.2831853071795850 ) ); // ~1 <- ~360 degrees == PI * 2
6
7
console.log( Math.cos(-1.5707963267948966 ) ); // ~0 <- ~-90 degrees == -PI / 2
Note:
6.123233995736766e-17
,-1.8369701987210297e-16
and6.123233995736766e-17
should be equal to0
but they are not because of computation precision error.
Syntax | Math.cos(number) |
Parameters | number - integer or float number value in radians (primitive value). |
Result |
returns a numeric value between |
Description | cos is static method that takes only one parameter and returns approximation of cos(x) mathematical function result. |
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.cos(rad);
8
9
console.log('cos(' + rad + ' rad) = ' + y);
10
}
xxxxxxxxxx
1
function calculateCos(deg) {
2
var rad = (Math.PI / 180) * deg;
3
return Math.cos(rad);
4
}
5
6
7
// Usage 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 = calculateCos(deg );
16
17
console.log('cos(' + deg + ' deg) = ' + y);
18
}
xxxxxxxxxx
1
var x1 = 0.0; // beginning of cosine chart
2
var x2 = 6 * 3.14 // end of cosine 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.cos(rad) + 1;
10
11
var line = '';
12
for(var y = y1; y < y2; y += dy) {
13
line += ' ';
14
}
15
16
console.log(line + '+');
17
}
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
function Chart(canvas, x1, x2, y1, y2, dx, func) {
11
var self = this;
12
13
var context = canvas.getContext('2d');
14
15
var width = canvas.width;
16
var height = canvas.height;
17
18
var xRange = x2 - x1;
19
var yRange = y2 - y1;
20
21
var calculatePoint = function(x) {
22
var y = func(x);
23
// chart will be reversed horizontaly because of reversed canvas pixels
24
var nx = (x - x1) / xRange; // normalized x
25
var ny = 1.0 - (y - y1) / yRange; // normalized y
26
return {
27
x: nx * width,
28
y: ny * height
29
};
30
};
31
32
self.drawChart = function() {
33
var point = calculatePoint(x1);
34
context.beginPath();
35
context.moveTo(point.x, point.y);
36
for (var x = x1 + dx; x < x2; x += dx) {
37
var point = calculatePoint(x);
38
context.lineTo(point.x, point.y);
39
}
40
var point = calculatePoint(x2);
41
context.lineTo(point.x, point.y);
42
context.stroke();
43
};
44
};
45
46
47
// Usage example:
48
49
var canvas = document.querySelector('#canvas');
50
51
// cosine chart range
52
var x1 = 0; // 0 degress
53
var x2 = +2 * Math.PI; // +360 degress
54
var y1 = -1.0;
55
var y2 = +1.0;
56
57
var dx = 0.1;
58
59
var chart = new Chart(canvas, x1, x2, y1, y2, dx, Math.cos);
60
61
chart.drawChart();
62
63
console.log('x range: <' + x1 + '; ' + x2 + '> // angles in radians');
64
console.log('y range: <' + y1 + '; ' + y2 + '>');
65
66
</script>
67
</body>
68
</html>