Js-20170804-math Object

Source: Internet
Author: User

Math methodThe Math.Abs method returns the absolute value of the parameter values. Math.Abs (1)//1math.abs (-1)//1both the Math.max method and the Math.min method can accept multiple arguments, MATH.MAX returns the largest of them, Math.min returns the smallest parameter. Math.max (2,-1, 5)//5math.min (2,-1, 5)//-1The Math.floor method takes a parameter that returns the largest integer less than the parameter. Math.floor (3.2)//3math.floor (-3.2)//-4The Math.ceil method takes a parameter that returns the smallest integer greater than the parameter. Math.ceil (3.2)//4math.ceil (-3.2)//3 If you need a function that always returns a numeric integer part, you can implement it yourself. function Tointeger (x) {x = number (x); return x < 0? Math.ceil (x): Math.floor (x);} Tointeger (3.2)//3ToInteger (3.5)//3ToInteger (3.8)//3ToInteger ( -3.2)// -3tointeger ( -3.5)// -3tointeger (-3.8)//-3The Math.Round method is used for rounding. Math.Round (0.1)//0math.round (0.5)//1math.round (0.6)//1//equivalent to Math.floor (x + 0.5) Note that it handles negative numbers (mainly 0.5 processing). Math.Round ( -1.1)// -1math.round ( -1.5)// -1math.round (-1.6)//-2The Math.pow method returns an exponential value with the first argument as a base and the second argument to a power. Math.pow (2, 2)//4math.pow (2, 3)//8 The following is a method for calculating the area of a circle. var radius = 20;var Area = Math.PI * MATH.POW (RADIUS, 2);The Math.sqrt method returns the square root of the parameter value. If the argument is a negative value, Nan is returned. MATH.SQRT (4)//2MATH.SQRT ( -4)//NaNThe Math.log method returns the natural pair of values with E as the base. Math.log (MATH.E)//1Math.log (10)//2.302585092994046The Math.exp method returns the parameter of the constant E. Math.exp (1)//2.718281828459045MATH.EXP (3)//20.085536923187668 Math.random ()Math.random () returns a pseudo-random number from 0 to 1, which may be equal to 0, but must be less than 1. Math.random ()//0.7151307314634323random number generation functions of any range are as follows. function getrandomarbitrary (min, max) {return math.random () * (max-min) + min;} Getrandomarbitrary (1.5, 6.5)//2.4942810038223864random integer generation functions of any range are as follows. function Getrandomint (min, max) {return Math.floor (Math.random () * (Max-min + 1)) + min;} Getrandomint (1, 6)//5examples of returning random characters are as followsfunction Random_str (length) {var ALPHABET = ' abcdefghijklmnopqrstuvwxyz '; ALPHABET + = ' abcdefghijklmnopqrstuvwxyz '; ALPHABET + = ' 0123456789-_ '; var str = '; for (var i=0; i < length; ++i) {var rand = Math.floor (Math.random () * ALPHABET. length); str + = Alphabet.substring (rand, Rand + 1);} return str;} RANDOM_STR (6)//"Ndqkor" in the above code, the RANDOM_STR function takes an integer as an argument, returning a string of the specified length consisting of random characters within the variable alphabet. Trigonometric methods

Js-20170804-math Object

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.