Languages
[Edit]
EN

JavaScript - Math.exp() method example

12 points
Created by:
Dollie-Rutledge
806

Math.exp() is a static method that takes only one parameter and return exponential function value in the range 0 (exclusive) to +Infinity.

// ONLINE-RUNNER:browser;
console.log( Math.exp(      -100 ) ); // 3.720075976020836e-44
console.log( Math.exp(        -1 ) ); // 0.36787944117144233
console.log( Math.exp(         0 ) ); // 1
console.log( Math.exp(         1 ) ); // 2.718281828459045
console.log( Math.exp(       100 ) ); // 2.6881171418161356e+43

console.log( Math.exp( -Infinity ) ); // 0
console.log( Math.exp( +Infinity ) ); // Infinity
console.log( Math.exp(       NaN ) ); // NaN

1. Documentation

SyntaxMath.exp(number)
Parametersnumber - integer or float number value (primitive value).
Result

Exponential function value of a number in the range 0 (exclusive) to +Infinity (primitive value).

If the value can not be calculated NaN is returned.

Description

Math.exp() is a static method that takes only one parameter and returns exponential function value.

2. Reversed console plot example

// ONLINE-RUNNER:browser;

var x1 = -4; // beginning of sine chart
var x2 = +2.8; // end of sine chart

var y1 = -1.0;
var y2 = +10.0;

var xSteps = 25;
var ySteps = 60;

var dx = (x2 - x1) / xSteps; // x axis step
var dy = (y2 - y1) / ySteps; // y axis step

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

for (var rad = x1; rad < x2; rad += dx) {
  	var y = Math.exp(rad);
  
  	if (y <= y1 || y >= y2) {
        console.log(' ');
    } else {
    	printLine(y1, y, '+');
    }
}

3. Canvas plot example

// ONLINE-RUNNER:browser;

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

    // exponential function chart range
    var x1 =  -0.1;
    var x2 =  +5.0;
    var y1 =  -0.1;
    var y2 = +10.0;

    var dx = 0.1;

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

    function calculatePoint(x) {
      var y = Math.exp(x) * yScale;

      // 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 + '>');
    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. Exponential function - Wikipedia

Alternative titles

  1. JavaScript - Math.exp() documentation with examples
  2. js - Math.exp() method documentation with examples
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.

JavaScript - Math documentation (EN)

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