JavaScript Math Object Detailed _ Basics

Source: Internet
Author: User
Tags abs acos arithmetic asin natural logarithm numeric value sin square root

The Math object is different from the above object, it can be said to be a public mathematics class, there are many mathematical methods, for various mathematical operations
However, the math object does not need to be constructed, and you can use the method directly

1, constants (that is, attributes)

e returns the arithmetic constant E, that is, the base of the natural logarithm (approximately equal to 2.718)

E Returns the arithmetic constant E, the base of the natural logarithm (approximately equal to 2.718)
LN2 Returns the natural logarithm of 2 (approximately equal to 0.693)
LN10 Returns the natural logarithm of 10 (approximately equal to 2.302)
Log2e Returns the logarithm of a 2-based e (approximately 1.414)
log10e Returns the logarithm of a 10-based E (approximately 0.434)
Pi return pi (approximately equal to 3.14159)
Sqrt1_2 Returns the reciprocal of the square root that returns 2 (about 0.707)
SQRT2 Returns the square root of 2 (approximately equal to 1.414)

The following are their values:

Copy Code code as follows:
document.write ("MATH.E =" +math.e+ "<br>");
document.write ("MATH.LN2 =" +math.ln2+ "<br>");
document.write ("math.ln10 =" +math.ln10+ "<br>");
document.write ("math.log2e =" +math.log2e+ "<br>");
document.write ("math.log10e =" +math.log10e+ "<br>");
document.write ("Math.PI =" +math.pi+ "<br>");
document.write ("math.sqrt1_2 =" +math.sqrt1_2+ "<br>");
document.write ("Math.sqrt2 =" +math.sqrt2+ "<br>");

Output results:

MATH.E = 2.718281828459045
MATH.LN2 = 0.6931471805599453
Math.ln10 = 2.302585092994046
MATH.LOG2E = 1.4426950408889634
math.log10e = 0.4342944819032518
Math.PI = 3.141592653589793
Math.sqrt1_2 = 0.7071067811865476
Math.sqrt2 = 1.4142135623730951

2, ABS () method can return the absolute value of the number

Math.Abs (x); x must be a numeric value, this number can be an integer, and a decimal can
document.write (Math.Abs (-2.77));//Output 2.77

3, ACOs (x) returns the inverse cosine of the number.

Math.acos (x); x must be a number between 1.0 ~ 1.0
Returns Nan if X is not in the above range

4, ASIN () method can return a number of the inverse chord value.

Math.asin (x); x must be a numeric value between 1.0 ~ 1.0.
If the parameter x exceeds the range of 1.0 ~ 1.0, then the browser returns NaN.

5. The Atan () method can return the tangent value of a number.

Math.atan (x); x required. Must be a numeric value.
The value returned is the Radian value between-PI/2 and PI/2.

6. The atan2 () method returns the angle from the X axis to point (X,y).

Math.atan2 (y,x)
The value between-pi and PI is the angle that is passed when the X axis is rotated counterclockwise from the point (x,y).

7, the Ceil () method can be rounded on a number.

What is rounding up? That is greater than or equal to X, and is the nearest integer to it.
Math.ceil (x); x required. Must be a numeric value.

Copy Code code as follows:
document.write (Math.ceil (0.60) + "<br/>")
document.write (Math.ceil (0.40) + "<br/>")
document.write (Math.ceil (5) + "<br/>")
document.write (Math.ceil (5.1) + "<br/>")
document.write (Math.ceil ( -5.1) + "<br/>")
document.write (Math.ceil (-5.9))

The output is:

1
1
5
6
-5
-5

For negative numbers, you know.

8, cos () method can return the cosine of a number.

Math.Cos (x); x required. Must be a numeric value. Returns the number between 1.0 and 1.0. 、
X is actually required to enter a Radian value, such as--->
Pi means 180°, and pi is Math.PI.
document.write (Math.Cos (Math.PI));
Output is-1

But if:

Copy Code code as follows:
document.write (Math.Cos (MATH.PI/2));

Output as: 6.123233995736766E-17

and

Copy Code code as follows:
document.write (Math.Cos (MATH.PI/3));

Output as: 0.5000000000000001

Why are these weird numbers coming up?

In fact, we all know document.write (Math.Cos (MATH.PI/2)), should output 0, and in JavaScript may not ask for 0, so we used a very very small number instead
Similar document.write (Math.Cos (MATH.PI/3)); it's supposed to be 0.5, but at the back, one more.
These are small problems, there is nothing to say, the register is not possible to represent all the numbers, so in the calculation of errors in the process is also very normal

9. The exp () method returns the value of the X power of E.

Math.exp (x); x required. An arbitrary value or expression. is used as an index.
Returns the x power of E. E represents the base of the natural logarithm, whose value is approximately 2.71828.
document.write (Math.exp (1) + "<br/>");//Output 2.718281828459045

10. The floor () method can be rounded down for a number.

corresponding to the Ceil () method, the floor () approach is to round down a number that is less than or equal to x and the nearest integer to X.
Math.floor (x);

Copy Code code as follows:
document.write (Math.floor (0.60) + "<br/>")
document.write (Math.floor (0.40) + "<br/>")
document.write (Math.floor (5) + "<br/>")
document.write (Math.floor (5.1) + "<br/>")
document.write (Math.floor ( -5.1) + "<br/>")
document.write (Math.floor (-5.9))

The output is:

0
0
5
5
-6
-6

For negative numbers, you know.

11, log () method can return a number of the natural logarithm.

Math.log (x);//parameter x must be greater than 0, greater than 0, the result is Nan, or 0 is-infinity

Copy Code code as follows:
document.write (Math.log (2.7183) + "<br/>")
document.write (Math.log (2) + "<br/>")
document.write (Math.log (1) + "<br/>")
document.write (Math.log (0) + "<br/>")
document.write (Math.log (-1))

The output is:

1.0000066849139877
0.6931471805599453
0
-infinity
NaN
From the above we can see

12, the Max () method returns the number of two specified numbers with a larger value.

Math.max (x ...),//x is 0 or more values. The method has only two parameters before ECMASCript v3.
return value:
The largest value in the parameter.
If there are no arguments, return-infinity.
Returns a Nan if a parameter is Nan, or a non-numeric value that cannot be converted to a number.
The following example:

Copy Code code as follows:
document.write (Math.max (5,3,8,1));//8
document.write (Math.max (5,3,8, ' M '));//nan
document.write (Math.max (5));//5
document.write (Math.max ());//-infinity

13. The min () method returns the number with the lowest value in the specified number.

Math.min (x,y); x is 0 or more values. The method has only two parameters before ECMASCript v3.
return value:
The smallest value in the parameter.
If there are no arguments, return Infinity.
Returns a Nan if a parameter is Nan, or a non-numeric value that cannot be converted to a number.
and the Max () method uses a similar

14. The POW () method returns the value of the Y-power of X.

Math.pow (X,y);
X required. Base Must be a number.
Y required. Power number. Must be a number.
return value:
If the result is an imaginary number or a negative number, the method returns NaN. If the index is too large to cause a floating-point overflow, the method returns Infinity.
The following example:

Copy Code code as follows:
document.write (Math.pow () + ' <br> ');
document.write (Math.pow (2) + ' <br> ');
document.write (Math.pow (2,2) + ' <br> ');
document.write (Math.pow (2,2,2) + ' <br> ');
document.write (Math.pow (' M ', 2) + ' <br> ');

Output:

NaN
NaN
4
4
NaN

15. The random () method can return a random number between 0 ~ 1.

Math.random ()//No Ginseng
Return:
A pseudo random number between 0.0 ~ 1.0.
What is pseudo random number?
The true meaning of random numbers is the result of a random event, which, after countless times, appears to present some probability theory, which is unpredictable.
Pseudo-random number is based on pseudo random algorithm, it is a simulation of random algorithm, so called pseudo-random number

Copy Code code as follows:
document.write (Math.random ())

0.12645312909485157

16. The round () method can round a number to the nearest integer.

Math.Round (x), x required. Must be a number.
For 0.5, the method is rounded up.
For example, 3.5 will be rounded to 4, and-3.5 will be rounded to-3.
It actually feels like this method is implemented in combination with the Ceil () and the floor () method.

Copy Code code as follows:
document.write (Math.Round (0.60) + "<br/>")
document.write (Math.Round (0.50) + "<br/>")
document.write (Math.Round (0.49) + "<br/>")
document.write (Math.Round ( -4.40) + "<br/>")
document.write (Math.Round (-4.60))

The output is:

1
1
0
-4
-5

17. The sin () method returns the sine of a number.

Math.sin (x), x required. An angle in radians. The angle is multiplied by 0.017453293 (2pi/360) to be converted to radians.
return value:
The sine value of the parameter x. The return value is between 1.0 and 1.0.

Copy Code code as follows:
document.write (Math.sin (3) + "<br/>")
document.write (Math.sin ( -3) + "<br/>")
document.write (Math.sin (0) + "<br/>")
document.write (Math.sin (Math.PI) + "<br/>")
document.write (Math.sin (MATH.PI/2)

The output is:

0.1411200080598672
-0.1411200080598672
0
1.2246063538223772e-16
1

18, sqrt () method can return the square root of a number.

MATH.SQRT (x);//x required, must be a number greater than or equal to 0.
return value:
The square root of the parameter x. If x is less than 0, it returns NaN.
It is equivalent to Math.pow (x,0.5);

19. The tan () method returns a number that represents the tangent of a corner.

Math.tan (x),//x required. An angle in radians. The angle is multiplied by 0.017453293 (2pi/360) to be converted to radians.

See more JavaScript syntax, you can focus on: JavaScript reference tutorial, JavaScript Code style guide, and also hope that many people support the cloud-Habitat community.

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.