Javascript Math object

Source: Internet
Author: User
Tags acos asin natural logarithm rounds
The Math object is different from the above object. It can be said to be a common mathematics class, which has many mathematical methods used for various mathematical operations, but Math objects do not need to be constructed, you can directly use the methods here. 1. Constant (I .e. attribute) E returns the arithmetic constant e, that is, the base number of the natural logarithm (approx... syntaxHighlighter. all ();

Math object is different from the above object. It can be said to be a common mathematics class, which has many mathematical methods for various mathematical operations.
However, the Math object does not need to be constructed and can be directly used for the methods.
1. constants (I .e. attributes)
E returns the arithmetic constant e, that is, the base number of the natural logarithm (approximately 2.718)
LN2 returns the natural logarithm of 2 (approximately 0.693)
LN10 returns the natural logarithm of 10 (approximately 2.302)
LOG2E returns the base-2 logarithm of e (approximately 1.414)
LOG10E returns the base-10 logarithm of e (approximately 0.434)
PI returns the circumference rate (approximately 3.14159)
SQRT1_2 returns the reciprocal of the square root of 2 (approximately 0.707)
SQRT2 returns the square root of 2 (approximately 1.414)
Their values are as follows:
Document. write ("Math. E =" + Math. E +"
");
Document. write ("Math. LN2 =" + Math. LN2 +"
");
Document. write ("Math. LN10 =" + Math. LN10 +"
");
Document. write ("Math. LOG2E =" + Math. LOG2E +"
");
Document. write ("Math. LOG10E =" + Math. LOG10E +"
");
Document. write ("Math. PI =" + Math. PI +"
");
Document. write ("Math. SQRT1_2 =" + Math. SQRT1_2 +"
");
Document. write ("Math. SQRT2 =" + Math. SQRT2 +"
");
Output result:
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 returns the absolute value of the number.
Math. abs (x); x must be a numerical value, which can be an integer or a decimal number.
Document. write (Math. abs (-2.77); // output 2.77
3. acos (x) returns the arc cosine of the number.
Math. acos (x); x must be-1.0 ~ Number between 1.0
If x is not in the preceding range, NaN is returned.
4. The asin () method returns the arc sine of a number.
Math. asin (x); x must be a value ranging from-1.0 ~ In the range of 1.0.
If the value of x exceeds-1.0 ~ In the range of 1.0, the browser returns NaN.
5. The atan () method returns the arc tangent of a number.
Math. atan (x); x is required. It must be a numerical value.
The returned value is the radian value between-PI/2 and PI/2.
6. The atan2 () method returns an angle between the x axis and the point (x, y.
Math. atan2 (y, x)
-The value between PI and PI is the angle that passes through when the X axis is clockwise (x, y.
7. The ceil () method rounds up a number.
What is top rounding? That is, an integer that is equal to or greater than x and closest to x.
Math. ceil (x); x is required. It must be a numerical value.
Document. write (Math. ceil (0.60) +"
")
Document. write (Math. ceil (0.40) +"
")
Document. write (Math. ceil (5) +"
")
Document. write (Math. ceil (5.1) +"
")
Document. write (Math. ceil (-5.1) +"
")
Document. write (Math. ceil (-5.9 ))
Output:
1
1
5
6
-5
-5
For negative numbers, you know
8. The cos () method returns the cosine of a number.
Math. cos (x); x is required. It must be a numerical value. The returned value is the number between-1.0 and 1.0. ,
X requires entering a radian value, for example, --->
π represents 180 °, π is Math. PI
Document. write (Math. cos (Math. PI ));
The output is-1.
But suppose:
Document. write (Math. cos (Math. PI/2 ));
Output: 6.123233995745066e-17
And: document. write (Math. cos (Math. PI/3 ));
0.5000000000000001
Why are these weird numbers?
As we all know, document. write (Math. cos (Math. PI/2); 0 should be output, and 0 may not be evaluated in Javascript, so a very, very small number should be used instead.
Similar to document. write (Math. cos (Math. PI/3); it should be 0.5, but there is one more
These are minor issues. Nothing can be said, and the Register itself cannot represent all the numbers. Therefore, errors in the calculation process are normal.
9. The exp () method returns the x power of e.
Math. exp (x); x is required. Any number or expression. Used as an index.
Returns the x power of e. E Represents the base number of the natural logarithm, and its value is approximately 2.71828.
Document. write (Math. exp (1) +"
"); // Output 2.718281828459045
10. The floor () method can deploy a number.
Corresponds to the ceil () method. The floor () method is a number that is equal to or less than x and the nearest integer to x.
Math. floor (x );
Document. write (Math. floor (0.60) +"
")
Document. write (Math. floor (0.40) +"
")
Document. write (Math. floor (5) +"
")
Document. write (Math. floor (5.1) +"
")
Document. write (Math. floor (-5.1) +"
")
Document. write (Math. floor (-5.9 ))
Output:
0
0
5
5
-6
-6
For negative numbers, you know
11. The log () method returns the natural logarithm of a number.
Math. log (x); // parameter x must be greater than 0. If it is greater than 0, the result is NaN. If it is equal to 0, the result is-Infinity.
Document. write (Math. log (2.7183) +"
")
Document. write (Math. log (2) +"
")
Document. write (Math. log (1) +"
")
Document. write (Math. log (0) +"
")
Document. write (Math. log (-1 ))
Output:
1.0000066849139877
0.6931471805599453
0
-Infinity
NaN
We can see from the above
12. The max () method returns the number with a large value in two specified numbers.
Math. max (x...), // x is 0 or multiple values. Before ECMASCript v3, this method has only two parameters.
Return Value:
The maximum value in the parameter.
If no parameter exists,-Infinity is returned.
If a parameter is NaN or cannot be converted to a non-numeric value, NaN is returned.
For example:
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 multiple values. Before ECMASCript v3, this method has only two parameters.
Return Value:
The smallest value in the parameter.
If no parameter exists, Infinity is returned.
If a parameter is NaN or cannot be converted to a non-numeric value, NaN is returned.
Similar to the max () method
14. The pow () method returns the y power value of x.
Math. pow (x, y );//
X is required. Base number. Must be a number.
Y is required. Power. Must be a number.
Return Value:
If the result is a virtual number or a negative number, the method returns NaN. If the floating point overflow is caused by an excessively large index, this method returns Infinity.
For example:
Document. write (Math. pow () +'
');
Document. write (Math. pow (2) +'
');
Document. write (Math. pow (2, 2) +'
');
Document. write (Math. pow (2, 2) +'
');
Document. write (Math. pow ('M', 2) +'
');
Output:
NaN
NaN
4
4
NaN
15. The random () method returns a value ranging from 0 ~ A random number between 1.
Math. random (); // No Parameter
Return Value:
0.0 ~ A pseudo-random number between 1.0.
What is a pseudo-random number?
The real random number is the result of a random event. After countless times, it shows a probability theory, which is unpredictable.
The pseudo-random number is implemented based on the pseudo-random algorithm. It uses a simulated random algorithm. Therefore, it is called a pseudo-random number.
Document. write (Math. random ())
0.12645312909485157
16. The round () method rounds a number to the nearest integer.
Math. round (x), x required. Must be a number.
For 0.5, this method is rounded up.
For example, 3.5 is rounded to 4, and-3.5 is rounded to-3.
In fact, this method is implemented by combining the ceil () and floor () methods.
Document. write (Math. round (0.60) +"
")
Document. write (Math. round (0.50) +"
")
Document. write (Math. round (0.49) +"
")
Document. write (Math. round (-4.40) +"
")
Document. write (Math. round (-4.60 ))
Output:
1
1
0
-4
-5
17. sin () returns the sine of a number.
Math. sin (x), x required. An angle in radians. Multiply the angle by 0.017453293 (2PI/360) to a radian.
Return Value:
Returns the sine of x. The returned value ranges from-1.0 to 1.0.
Document. write (Math. sin (3) +"
")
Document. write (Math. sin (-3) +"
")
Document. write (Math. sin (0) +"
")
Document. write (Math. sin (Math. PI) +"
")
Document. write (Math. sin (Math. PI/2)
Output:
0.1411200080598672
-0.1411200080598672
0
1.2246063538223772e-16
1
18. The sqrt () method returns the square root of a number.
Math. sqrt (x); // x is required. It must be a number greater than or equal to 0.
Return Value:
The square root of x. If x is less than 0, NaN is returned.
It's equivalent to Math. pow (x, 0.5 );
19. The tan () method returns a number representing the tangent of an angle.
Math. tan (x), // x required. An angle in radians. Multiply the angle by 0.017453293 (2PI/360) to a radian.

 

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.