JavaScript monomer built-in object: Math Object

Source: Internet
Author: User
Tags natural logarithm rounds square root value of pi

JavaScript provides a built-in math object, which has a powerful and convenient auxiliary computing function, this article summarizes its properties and methods, equivalent to a solid foundation of the following ~

1. Properties of the Math object

MATH.E (value of the constant E); Math.ln10 (natural logarithm of 10); MATH.LN2 (natural logarithm of 2), math.log2e (logarithm of base e of 2), math.log10e (logarithm of base e of 10), Math.PI (Value of pi), math.sort1_2 (the square root of 1/2, also the reciprocal of the Radical Two) ; Math.sort2 (square root of 2).

2, Min () and Max () methods

Used to determine the maximum and minimum values in a set of numeric values. These two methods are often used to avoid redundant loops and to determine the maximum value of a set of numbers in an if statement.

It is important to note that to determine the maximum value in an array, we can use the Apply () method, as in the following example:

var values = [1,2,3,4,5,6,7,8,9]; var max = Math.max.apply (math,values);

3. Rounding Method

There are a total of three rounding methods:

    • Math.ceil () rounds the value up to the nearest integer;
    • Math.floor () rounds the value down to the nearest integer;
    • Math.Round () Standard rounding, which is the rounding method in mathematics.

In fact, very good understanding, Ceil has the meaning of the ceiling, so upward, floor has the meaning of flooring, so down ~ ~ respectively For example:

document.write (Math.ceil (10.5));     // document.write (Math.floor (10.5));    // Tendocument.write (Math.Round (10.5));  //  One

4. Random () method

Like the nature of many languages, JavaScript can also use the Math.random () method to return a random number between 0 and 1, its usage will not be so rigid, and with your flexibility to use to show the unusual magic,

For example, this formula:Math.floor (Math.random () * may be worth the total + first possible number)

We can give an example: var num = Math.floor (math.random () * 10 + 1), the formula is to randomly take an integer between 1-10, the possible value is also the same as the value behind multiplication sign, the equation can have 10 values, The first possible value is determined by the value that follows the plus sign, and the other requirements can be class-lifted.

So amazing!

Thus we can use it to randomly fetch a value in the array:

var arr = [2,8, "China", 22, "613 quarters", "I Love javascript!" ]function  selectnum (min,max) {    var num = max-min + 1;       return Math.floor (math.random () * num + min);} var myneed = arr[selectnum (0, Arr.length-1)];d ocument.write (myneed);   

5. Other methods

There are some ways to complete a variety of calculations, here is not introduced, choose a few typical write it ~

Math.Abs () returns the absolute value of a number;

Math.sin () returns the sinusoidal value of the value;

Math.asin () returns the inverse chord value of the value

Finally give yourself to encourage, JavaScript is a very interesting language, easy to get started, and do not have to be very boring to remember them, so the effect will not be very good, and so you use the time, you go through the query, and then the memory will be very deep, this is my blog record these learning footprints reason ~ ~ ~ (Most of these examples are from the "JavaScript Advanced Programming (Third Edition)" book, equivalent to reading notes ~)

JavaScript monomer built-in object: Math Object

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.