Concept

  • Math yes javaScript Built in objects for , It includes some mathematical constant attributes and mathematical function methods .

  • Math It's not a function object , user Number Type , I won't support it BigInt.

  • Math All properties and methods of are static .

  • For example, when we use pi , It's written in Math.PI

  • When using the sine and cosine function, the expression is Math.sin(x),x Is the parameter to pass in .

  • Math The constant used is JavaScript Defined by the full precision floating-point number in .

math Native properties


// Euler constant , It is also the base of natural logarithm , About equal to 2.718.
console.log("Math.E", Math.E); // Math.E 2.718281828459045
// 2 The natural logarithm of , About equal to 0.693.
console.log("Math.LN2", Math.LN2); // Math.LN2 0.6931471805599453
// 10 The natural logarithm of , About equal to 2.303.
console.log("Math.LN10", Math.LN10); // Math.LN10 2.302585092994046
// With 2 At the bottom of the E The logarithmic , About equal to 1.443.
console.log("Math.LOG2E", Math.LOG2E); // Math.LOG2E 1.4426950408889634
// With 10 At the bottom of the E The logarithmic , About equal to 0.434.
console.log("Math.LOG10E", Math.LOG10E); // Math.LOG10E 0.4342944819032518
// PI , The ratio of the circumference of a circle to its diameter , About equal to 3.14159.
console.log("Math.PI", Math.PI); // Math.PI 3.141592653589793
// Calculate the circumference of the circle
function calculateCircumference(radius) {
return 2 * Math.PI * radius;
}
console.log("calculateCircumference(1)", calculateCircumference(1)); // calculateCircumference(1) 6.283185307179586
// A half ½ The square root of , It's also 2 The reciprocal of the square root of 1 2 , About equal to 0.707.
console.log("Math.SQRT1_2", Math.SQRT1_2); // Math.SQRT1_2 0.7071067811865476
// 2 The square root of , About equal to 1.414.
console.log("Math.SQRT2", Math.SQRT2); // Math.SQRT2 1.4142135623730951

math Common methods

Math.abs()  //  Specify the number  “x“  The absolute value of 
Math.abs("-1"); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN

math Digital processing methods in daily development

// Math.round()  Function returns the nearest integer to which a number is rounded .
console.log(Math.round(20.49)); //20
console.log(Math.round(20.5)); //21
console.log(Math.round(-20.5)); //-20
console.log(Math.round(-20.51)); //-21 // Math.ceil() Returns the smallest integer greater than or equal to a given number , Rounding up .
console.log(Math.ceil(0.95));
// 1
console.log(Math.ceil(4));
// 4
console.log(Math.ceil(7.004));
// 8
console.log(Math.ceil(-7.004));
// -7 // Math.floor() Returns the maximum integer less than or equal to a given number , Math.floor() To round down .
Math.floor(45.95);
// 45
Math.floor(45.05);
// 45
Math.floor(4);
// 4
Math.floor(-45.05);
// -46
Math.floor(-45.95);
// -46 // Math.max() Returns the maximum value of a set of numbers
console.log(Math.max(1, 3, 2));
// 3
console.log(Math.max(-1, -3, -2));
// -1
const array1 = [1, -3, 2];
console.log(Math.max(...array1));
// 3 // Math.min() Returns the minimum value of zero or more values .
console.log(Math.min()); // Infinity
console.log(Math.min(1, 2, 3, -4)); // -4 // Use Math.min() Crop value (Clipping a value)
function f(x) {
if (x > 5) {
return (x = 5);
}
return (x = 6);
}
var finalMin = Math.min(f(2), 2, 3, 4, 5, 30);
console.log("finalMin", finalMin); // 2 // Math.sqrt() Returns the square root of a number
function calcHypotenuse(a, b) {
return Math.sqrt(a * a + b * b);
}
console.log(calcHypotenuse(3, 4));
// 5
console.log(calcHypotenuse(5, 12));
// 13
console.log(calcHypotenuse(0, 0));
// 0
Use Math.random() Generate random number
/**
*
* Math.random() Function returns a floating point number
* Pseudo random numbers in the range from 0 To less than 1, in other words , from 0( Include 0) Upward , But not including 1( exclude 1),
* Then you can zoom to the desired range . Implement the initial seed selection to the random number generation algorithm ; It cannot be selected or reset by the user .
*
* */ console.log(Math.random()); function getRandomNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min; // Including the maximum value , Including the minimum value
} console.log(getRandomNumber(2, 100));

Summary

  • The above examples include math Common methods and properties api
  • math In use , Can combine random as well as max and min Such method , Generate the required random number
  • adopt round、floor、ceil, We can take further values for numbers , Get a number format that meets the requirements

Math For more information, please refer to the documentation

Math file


Mathjs plug-in unit

Document address

https://mathjs.org/examples/index.html

  • mathjs The method provided by the plug-in is relatively comprehensive , It covers everything from algebraic calculations to functional calculations , Money calculation and other methods , Matrix serialization, etc , More ways to view official documents .

  • Basic usage :

npm install mathjs

import { sqrt } from 'mathjs'

console.log(sqrt(-4).toString()) // 2i


Source code address

Article personal blog address :javaScript in Math Introduction to basic methods of built-in objects

Welcome to the official account : Program ape boo , Update some front-end introductory articles from time to time

It's not easy to create , Please indicate the source and the author of the reprint .

javaScript in Math More basic methods for getting started with built-in objects

  1. javascript Built in objects in

    2015.12.1 javascript Built in objects in review : 1.js Built in functions in alert prompt write parseInt parseFloat eval isNaN document ...

  2. javascript Built in objects and data structures in

    Catalog brief introduction The base type undefined Boolean and Boolean object Number and BigInt String Symbol null Object Function Date Array K ...

  3. JavaScript Built in objects in -8--3.Math-Math Object method -min()- max()- ceil() - floor()- round()- abs(); Math Object's random() Method ;

    JavaScript Built-in objects -3.Math( The number ) Learning goals 1. master Math Object method : min() max() ceil() floor() round() abs() Math.min() grammar ...

  4. JavaScript Built in objects in -8--2.String- String objects Method ; Interception method ; Comprehensive application ; Other methods ;

    JavaScript Built-in objects -2String( character string ) Learning goals 1. Master string objects Method : charAt() charCodeAt() indexOf() lastIndextOf() charAt ...

  5. JavaScript Built in objects in -8--1.Array( Array )-Array Constructors ; Array stack method ; Array conversion method ; Array operation method ; Delete - Insert - Replace array items ; ECMAScript Two position methods added to an array instance ;

    JavaScript Built-in objects -1Array( Array ) Learning goals 1. Master any creation of arrays 2. Master the reading and writing of numerical elements 3. Master the array of length attribute How to create an array There are two basic ways to create arrays : 1. Use Arra ...

  6. javascript Summary of built-in objects in

    Built-in objects Standard built in objects Object Object.create Object.prototype.toString Object.prototype.hasOwnProperty Boolean S ...

  7. JavaScript( Nine ):JavaScript Built in objects in

    One . The concept of object Object is a special data type (object). Has a set of properties and methods . Divided into built-in objects and custom objects . Two .String object String Object is used to support string processing . 1. attribute lengt ...

  8. JavaScript Built in objects in -8--4.date In the object - obtain , How to set the date time ; obtain , How to set the date, hour, minute, second and week ;

    Learning goals 1. Master the method of creating Date object 2. master date Object to get the date and time 3. master date Object to set the date and time How to create a date object grammar :new Date(); function : Create a date time object ...

  9. understand JavaScript Built in objects in

    All programming languages have internal ( Or built-in ) Object to create The basic function of language . The internal object is The basis of the language you write custom code in , This code implements custom functions based on your imagination .JavaScript There are many Define it as the internal object of the language . Ben ...

  10. JavaScript ( Built in objects and methods )

    JavaScript The objects in are divided into 3 Kind of : Built-in objects . Browser object . Custom object JavaScript Provide multiple built-in objects :Math/Array/Number/String/Boolean... Objects just have properties ...

Random recommendation

  1. rabbitMQ Study ( One )

    General pattern Server side : import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabb ...

  2. 【poj2724】 Purifying Machine

    http://poj.org/problem?id=2724 ( Topic link ) The question Mike There is a machine that can help him clean the cheese , Each cheese consists of a n Bit binary number means , There are... On the machine n Button , Each button has 1,0,*, among ...

  3. 201772020113 Li Qinghua 《 Object oriented programming (java)》 The first 16 Weekly learning summary

    1. The purpose and requirements of the experiment (1) Master the concept of thread : (2) Mastering two techniques of thread creation : (3) Understand and master the priority attribute and scheduling method of thread : (4) Master the concept and implementation technology of thread synchronization : 2. Experiment content and steps experiment 1: Test program ...

  4. < turn > Safety test mind map

    Recently, it has been monitored that some systems of the company appear a large number of HTTP perhaps TCP Connect , Considering the safety , The boss asked me to study security testing ... It's just from the weekly magazine I subscribed to tonight ( Developer headlines ) I saw a post about security testing , Be a porter , branch ...

  5. ClamAV Installation and use of virus software

    ClamAV Antivirus is Linux The most popular anti-virus software on the platform ,ClamAV It is a free and open source product , Support multiple platforms , Such as :Linux/Unix.MAC OS X.Windows.OpenVMS.ClamAV It's based on virus scanning ...

  6. Leetcode Question bank ——23. Merge k A sort list

    @author: ZZQ @software: PyCharm @file: mergeKLists.py @time: 2018/10/12 19:55 explain : Merge k A sort list , Returns the merged sort ...

  7. git Summary of common commands ( One )

    1. Initialize version Library : . Enter the project root directory . Create project directory mkdir Project directory name . Enter the created project cd Project name pwd Show current directory . Project initialization git init // After completion, it will be generated in the project directory ...

  8. Today's headlines advanced back-end development interns three rounds of technical aspects +HR Noodles Face the

    After the end of the two sides, I have blurred my consciousness , I forgot a lot of things I asked , And some basic knowledge is not written here , Most companies ask the same question ... one side (2018/03/27,11:00~11:50) 1: Self introduction. 2: Let's briefly talk about your project ...

  9. springboot Property class automatically loads the value in the configuration file

    springboot Property class automatically loads the value in the configuration file , Such as Person Class loaded in yml Configured in name,age Attribute values such as , The following steps can be used to obtain : On the class to add @ConfigurationProperties annotation ,p ...

  10. python operation Redis cache

    python operation Redis cache https://www.cnblogs.com/guotianbao/p/8683037.html Learning materials : E-book resources Contact email :[email protected] ...