Object Math in JavaScript. Mathematical operations. Code exa... open

Object Math in JavaScript. Mathematical operations. Code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: ES6

The Math object provides a number of mathematical functions that can be used in calculations. Consider the basic mathematical functions:

abs()

The abs() function returns the absolute value of a number:

    var x = -25;
    document.write(Math.abs(x)); // 25
    var y = 34;
    document.write(Math.abs(y)); // 34

min() and max()

The min() and max() functions return the minimum and maximum value from a set of numbers, respectively:

    var max = Math.max(19, 45); // 45
    var min = Math.min(33, 24); // 24

ceil()

The ceil() function rounds a number up to the next largest integer:

    var x = Math.ceil(9.2); // 10
    var y = Math.ceil(-5.9); // -5

floor()

The floor() function rounds a number up to the next smallest integer:

 var x = Math.floor(9.2); // 9
 var y = Math.floor(-5.9); // -6

round()

The round() function rounds a number to the next smallest integer if its decimal part is less than 0.5.
If the decimal part is equal to or greater than 0.5, then rounding goes to the nearest largest integer:

    var x = Math.round(5.5); // 6
    var y = Math.round(5.4); // 5
    var z = Math.round(-5.4); // -5
    var n = Math.round(-5.5); // -5
    var m = Math.round(-5.6); // -

random()

The random() function returns a random floating point number in their range from 0 to 1:

var x = Math.random();//0.2

pow()

The pow() function returns a number to a certain extent. For example, let’s raise the number 2 to the power of 3:

$base = 2;
$exponent = 3;
$result = pow($base, $exponent);
echo $result; // Return 8

sqrt()

The sqrt() function returns the square root of a number:

    var x = Math.sqrt(121); // 11
    var y = Math.sqrt(9); // 3
    var z = Math.sqrt(20); // 4.47213595499958

log()

The log() function returns the natural logarithm of a number:

    var x = Math.log(1); // 0
    var z = Math.log(10); // 2.302585092994046

Trigonometric functions

  • Math.sin() – calculates the sine of a number
  • Math.cos() – calculates the cosine of a number
  • Math.tan() – calculates the tangent of a number
  • Math.asin() – calculates the arcsine of a number
  • Math.acos() – calculates the arccosine of a number
  • Math.atan() – calculates the arc tangent of a number

Math method constants

In addition to methods, the Math object also defines a set of built-in constants that can be used in various calculations:

  • Math.PI – (PI): 3.141592653589793
  • Math.SQRT2 – (square root of 2): 1.4142135623730951
  • Math.SQRT1_2 – (half of the square root of two): 0.7071067811865476
  • Math.E – (number e or Euler number): 2.718281828459045
  • Math.LN2 – (natural logarithm of the number 2): 0.6931471805599453
  • Math.LN10 – (natural logarithm of 10): 2.302585092994046
  • Math.LOG2E – (binary logarithm of e): 1.4426950408889634
  • Math.LOG10E – (decimal logarithm of e): 0.4342944819032518
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation