Singleton built-in object Math

Posted by knsito on Sat, 15 Jan 2022 04:02:48 +0100

The calculations provided on Math objects are much faster than those implemented directly in JavaScript, because the calculations on Math objects use more efficient implementations and processor instructions in the JavaScript engine. However, the problem with Math calculation is that the accuracy will vary depending on the browser, operating system, instruction set and hardware.

Math.E is the value of the base e of the natural logarithm
Math. Natural logarithm with ln10 as base
Math. Natural logarithm with LN2 as base
Math.LOG2E logarithm with base e of 2
Math.LOG10E logarithm with base e of 10
Math. Value of PI π
Math. SQRT1_ Square root of 2 1 / 2
Math. Square root of sqrt2

min() and max() methods

The min() and max() methods are used to determine the minimum and maximum values in a set of values. Both methods accept any number of parameters,

  let max = Math.max(3, 54, 32, 16); 
  console.log(max); // 54 
  let min = Math.min(3, 54, 32, 16); 
  console.log(min); // 3 

To know the maximum and minimum values in the array, you can use the extension operator as follows. Using these two methods can avoid using additional loops and if statements to determine the maximum and minimum values of a set of values.

 let val = [1, 2, 3, 4, 5, 6, 7, 8]; 
 let max = Math.max(...val); 

Rounding method

Four methods for rounding small values to integers: math ceil(),Math.floor(),Math.round() and math fround().

 Math. The ceil () method always rounds up to the nearest integer.
 Math. The floor () method always rounds down to the nearest integer.
 Math. The round () method performs rounding.
 Math. The fround () method returns the closest single precision (32-bit) floating-point value representation.

console.log(Math.ceil(25.9));   // 26 
console.log(Math.ceil(25.5));   // 26 
console.log(Math.ceil(25.1));   // 26  
console.log(Math.round(25.9));  // 26 
console.log(Math.round(25.5));  // 26 
console.log(Math.round(25.1));  // 25  
console.log(Math.fround(0.4));  // 0.4000000059604645 
console.log(Math.fround(0.5));  // 0.5 
console.log(Math.fround(25.9)); // 25.899999618530273  
console.log(Math.floor(25.9));  // 25 
console.log(Math.floor(25.5));  // 25 
console.log(Math.floor(25.1));  // 25

random() method

Math. The random () method returns a random number in the range of 0 ~ 1, including 0 but not 1. This method is very convenient for web pages that want to display random quotes or random news.

You can use math based on the following formula Random() randomly selects a number from a set of integers:

number = Math.floor(Math.random() * total_number_of_choices + first_possible_value)

Here we use math Floor () method, because math Random () always returns a decimal, even if multiplied by a number plus a number. Therefore, if you want to randomly select a number from the range of 1 ~ 10, the code is as follows:

let num = Math.floor(Math.random() * 10 + 1);
function selectFrom(lowerValue, upperValue) {    
	let choices = upperValue - lowerValue + 1;  
 	return Math.floor(Math.random() * choices + lowerValue); 
 let num = selectFrom(2,10);
 console.log(num);  // Values in the range of 2 to 10, including 2 and 10

Pay attention to math The random () method is fine here for demonstration purposes. If random numbers need to be generated for encryption (the input to the generator needs high uncertainty), it is recommended to use window crypto. getRandomValues().

Other methods
The following table also summarizes other methods of Math objects.

Math.abs(x)Returns the absolute value of x
Math.exp(x)Return to math x power of e
Math.expm1(x)Equal to math exp(x) - 1
Math.log1p(x)Equal to 1 + Math log(x)
Math.pow(x, power)Returns the power of x


Topics: Javascript Front-end Vue.js