JavaScript Advanced Programming Reading notes (12) JS built-in object Math_javascript tips

Source: Internet
Author: User
Tags abs natural logarithm rounds square root
Properties of the Math object

E: Value E, bottom of the natural logarithm
The natural logarithm of the ln10:10
The natural logarithm of the Ln2:2
LOG2E: logarithm of 2 as the base E
LOG10E: logarithm of 10 as the base E
PI: Value Pie
The square root of the SQRT1_2:1/2
The square root of the Sqrt2:2
Methods for the Math object: maximum and minimum values
Min () &&max () is used to take the minimum and maximum values from a set of numbers.

Example:

Copy Code code as follows:

var Imax=math.max (1,2,3);
alert (IMax);//outputs 3
var imin=math.min (1,2,3);
alert (imin);//outputs 1


Approximately pair value

ABS () is used to return the absolute value of a number.
Example:
Copy Code code as follows:

var inegone=math.abs (-1);
alert (inegone);//oupputs 1
var iposone=math.abs (1);
alert (iposone);//outputs 1


Round decimal to Integer

Ceil () is a rounded up function that always rounds the number up to the nearest value
Floor () is a downward rounding function that always rounds the number down to the nearest value
Round () rounding the rounded method
Example:
Copy Code code as follows:

Alert (Math.ceil (25.5));//oputpus 26
Alert (Math.floor (25.5));//oputpus 25
Alert (Math.Round (25.5));//oputpus 26


Exponential calculation

EXP () used to raise the MATH.E to a specified power
Log () is used to return the natural logarithm of a particular number
POW () is used to raise the specified number to a specified power
sqrt () is used to return the square root of the specified number

Trigonometric methods

ACOs (x) is used to return the inverse cosine value of x
ASIN (x) is used to return the inverse chord value of x
Atan (x) is used to return the tangent value of X
ATAN2 (y,x) is used to return the y/x's inverse cosine value
COS (x) is used to return the cosine value of X
Sin (x) is used to return the sine value of x
Tan (x) is used to return the tangent value of X

Random number function

Random () is used to return a random number between 0 and 1, excluding 0 and 1.
Select a random number within a range:

Copy Code code as follows:

function Selectfrom (ifirstvalue,ilastvalue) {
var ichoices=ilastvalue-ifirstvalue+1;
Return Math.floor (Math.random () *ichoices+ifirstvalue);
}
Demo
var inum=selectfrom (2,10);


Author: artwl
Source: http://artwl.cnblogs.com
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.