JavaScript Math Object

Source: Internet
Author: User
Tags arithmetic natural logarithm square root

Math (arithmetic objects) performs common arithmetic tasks.

1. Properties of the Math object

// The base of the natural logarithm, which is the value of the constant e // Natural logarithm of 10 // Natural logarithm of 2 // 2 logarithm of base e // 10 logarithm of base e // the value of π // The square root of 1/2 (that is, the reciprocal of the square root of 2) // square root of 2

2, Min (), Max (), and apply () methods

Used to determine the minimum and maximum values in a set of values.

Cases:

var max = Math.max (1, 2, 3, 4, 5); // 5 var min = math.min (1, 2, 3, 4, 5); // 1 var values = [1, 2, 3, 4, 5]; var max = Math.max.apply (Math, values); // 5 var min = Math.min.apply (Math, values); // 1

3. Rounding Method

(1) Math.ceil () performs an upward rounding.

(2) Math.floor () performs a downward rounding.

(3) Math.Round () performs standard rounding, that is, rounding.

Cases:

Console.log (Math.ceil (2.1)); // 3Console.log (Math.floor (3.9)); // 3Console.log (Math.Round (3.3)); // 3

4. Random () method

Returns a random number greater than or equal to 0 less than 1.

Cases:

Console.log (Math.floor (Math.random () * 10 + 1)); // values from 1 to 10

5. Other methods

Math.Abs (num); // returns the absolute value of Num Math.exp (num); // returns the NUM power of MATH.E Math.log (num); // returns the natural logarithm of Num Math.pow (num, power); // returns the power sub power of Num MATH.SQRT (num); // returns the square root of Num Math.acos (x); // returns the inverse cosine value of x ...

JavaScript 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.