Languages
[Edit]
EN

JavaScript - Math.cos() method example

15 points
Created by:
Kate_C
2845

The Math.cos() function returns the cosine of the specified angle in radians in the range -1 to +1.

// ONLINE-RUNNER:browser;

console.log( Math.cos( 0                  ) ); //   1 <-     0 degrees
console.log( Math.cos( 1.5707963267948966 ) ); //  ~0 <-  ~90 degrees ==  PI / 2
console.log( Math.cos( 3.1415926535897932 ) ); // ~-1 <- ~180 degrees ==  PI
console.log( Math.cos( 4.71238898038469   ) ); //  ~0 <- ~270 degrees == -PI * (3 / 2)
console.log( Math.cos( 6.2831853071795850 ) ); //  ~1 <- ~360 degrees ==  PI * 2

console.log( Math.cos(-1.5707963267948966 ) ); //  ~0 <- ~-90 degrees == -PI / 2

Note: 6.123233995736766e-17, -1.8369701987210297e-16 and 6.123233995736766e-17 should be equal to 0 but they are not because of computation precision error.


1. Documentation

SyntaxMath.cos(number)
Parametersnumber - integer or float number value in radians (primitive value).
Result

returns a numeric value between -1 and +1, which represents the cosine of the angle (primitive value).

Descriptioncos is static method that takes only one parameter and returns approximation of cos(x) mathematical function result.

2. Working with radians

// ONLINE-RUNNER:browser;

var x1 = 0.0;          // begining of calculation in radians
var x2 = Math.PI / 2;  // ending of calculation radians

var dx = Math.PI / 36; // calculation step in degrees

for (var rad = x1; rad <= x2; rad += dx) {
  	var y = Math.cos(rad);
  
	console.log('cos(' + rad + ' rad) = ' + y);
}

3. Working with degrees

// ONLINE-RUNNER:browser;

function calculateCos(deg) {
	var rad = (Math.PI / 180) * deg;
  
  	return Math.cos(rad);
}

// Example:

var x1 = 0.0;  // begining of calculation in degrees
var x2 = 90.0; // ending of calculation degrees

var dx = 5.0;  // calculation step in degrees

for (var deg = x1; deg  <= x2; deg  += dx) {
	var y = calculateCos(deg );
  
  	console.log('cos(' + deg + ' deg) = ' + y);
}

4. Reversed console plot example

// ONLINE-RUNNER:browser;

var x1 = 0.0;     // begining of cosine chart
var x2 = 6 * 3.14 // end of cosine chart

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

for (var rad = x1; rad < x2; rad += dx) {
  	var y1 = 0.0;
  	var y2 = Math.cos(rad) + 1;

  	var line = '';
  
  	for(var y = y1; y < y2; y += dy) {
    	line += ' ';
    }
  
  	console.log(line + '+');
}

5. Canvas plot example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style> #canvas { border: 1px solid black; } </style>
</head>
<body>
  <canvas id="canvas" width="400" height="130"></canvas>
  <script>
    
    var canvas = document.querySelector('#canvas');
    var context = canvas.getContext('2d');

    // cosine chart range
    var x1 =  0;           // 0 degress
    var x2 = +2 * Math.PI; // +360 degress
    var y1 = -1.0;
    var y2 = +1.0;

    var dx = 0.1;

    var xRange = x2 - x1;
    var yRange = y2 - y1;

    function calculatePoint(x) {
      var y = Math.cos(x);

      // chart will be reversed horizontaly because of reversed canvas pixels

      var nx = (x - x1) / xRange;       // normalized x
      var ny = 1.0 - (y - y1) / yRange; // normalized y
      
      var point = {
        x: nx * canvas.width,
        y: ny * canvas.height
      };

      return point;
    }

    console.log('x range: <' + x1 + '; ' + x2 + '> // angles in radians');
    console.log('y range: <' + y1 + '; ' + y2 + '>');

    var point = calculatePoint(x1);
    
    context.beginPath();
    context.moveTo(point.x, point.y);

    for (var x = x1 + dx; x < x2; x += dx) {
      point = calculatePoint(x);
      context.lineTo(point.x, point.y);
    }

    point = calculatePoint(x2);
    context.lineTo(point.x, point.y);
    context.stroke();

  </script>
</body>
</html>

References

  1. Cosine - Wikipedia

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

JavaScript - Math (EN)

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍