Home > Web Front-end > JS Tutorial > body text

What is the Math object in js? how to use?

青灯夜游
Release: 2018-11-13 11:28:02
forward
2685 people have browsed it

What is the Math object in js? how to use? What this article brings to you is to introduce the properties and methods of the Math object, so that everyone can understand how to use the Math object. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. [Recommended related video tutorials: JavaScript Tutorial]

1. Properties of the Math object

 (1) E: Returns the arithmetic constant e, that is The base of the natural logarithm (approximately equal to 2.718).
 (2) LN2: Returns the natural logarithm of 2 (approximately equal to 0.693).
 (3) LN10: Returns the natural logarithm of 10 (approximately equal to 2.302).
 (4) LOG2E: Returns the logarithm of e with base 2 (approximately equal to 1.443).
 (5) LOG10E: Returns the logarithm of e with base 10 (approximately equal to 0.434).
 (6) PI: Returns pi (approximately equal to 3.14159).
 (7) SQRT1_2: Returns the reciprocal of the square root of 2 (approximately equal to 0.707).
 (8) SQRT2: Returns the square root of 2 (approximately equal to 1.414).

  <script type="text/javascript">
        document.write('属性E<br/>');
        document.write(Math.E);

        document.write('<br/><br/>属性LN2<br/>');
        document.write(Math.LN2);

        document.write('<br/><br/>属性LN10<br/>');
        document.write(Math.LN10);

        document.write('<br/><br/>属性LOG2E<br/>');
        document.write(Math.LOG2E);

        document.write('<br/><br/>属性LOG10E<br/>');
        document.write(Math.LOG10E);

        document.write('<br/><br/>属性PI<br/>');
        document.write(Math.PI);

        document.write('<br/><br/>属性SQRT1_2<br/>');
        document.write(Math.SQRT1_2);

        document.write('<br/><br/>属性SQRT2<br/>');
        document.write(Math.SQRT2);    
  </script>
Copy after login

The value of each attribute:

2. Math object method

(1) abs( x): Returns the absolute value of the number.

  <script type="text/javascript">
        var x=-3;
        document.write('abs(x)方法:<br/>');
        document.write(Math.abs(x));  
  </script>
Copy after login

(2) acos(x): Returns the arc cosine of the number.

      <script type="text/javascript">
            var x=0.5;
            document.write('acos(x)方法:<br/>');
            document.write(Math.acos(x)); 
      </script>
Copy after login

(3) asin(x): Returns the arcsine of the number.

<script type="text/javascript">
        var x=1;
        document.write('asin(x)方法:<br/>');
        document.write(Math.asin(x));   
</script>
Copy after login

(4) atan(x): Returns the arctangent of x as a number between -PI/2 and PI/2 radians.

<script type="text/javascript">
        var x=1;
        document.write('atan(x)方法:<br/>');
        document.write(Math.atan(x));   
</script>
Copy after login

(5) atan2(y,x): Returns the angle from the x-axis to the point (x,y) (between -PI/2 and PI/2 between radians).

<script type="text/javascript">
        var x=0.5;        var y=1;
        document.write('atan2(y,x)方法:<br/>');
        document.write(Math.atan2(y,x));  
</script>
Copy after login

(6) ceil(x): Round the number up.

<script type="text/javascript">
        var x=0.5;
        document.write('ceil(x)方法:<br/>');
        document.write(Math.ceil(x));    
</script>
Copy after login

(7) cos(x): Returns the cosine of the number.

<script type="text/javascript">
        var x=0;
        document.write('cos(x)方法:<br/>');
        document.write(Math.cos(x));    
</script>
Copy after login

(8) exp(x): Returns the exponent of e.

<script type="text/javascript">
        var x=1;
        document.write('exp(x)方法:<br/>');
        document.write(Math.exp(x));    
</script>
Copy after login

(9) floor(x): Round the number down.

<script type="text/javascript">
        var x=1;
        document.write('floor(x)方法:<br/>');
        document.write(Math.floor(x));    
</script>
Copy after login

(10) log(x): Returns the natural logarithm of the number (base is e).

<script type="text/javascript">
        var x=10;
        document.write('log(x)方法:<br/>');
        document.write(Math.log(x));    
</script>
Copy after login

(11) max(x,y): Returns the highest value between x and y.

<script type="text/javascript">
        var x=10;        var y=32;
        document.write('max(x,y)方法:<br/>');
        document.write(Math.max(x,y));    
</script>
Copy after login

(12) min(x,y): Returns the lowest value between x and y.

<script type="text/javascript">
        var x=10;        var y=32;
        document.write('min(x,y)方法:<br/>');
        document.write(Math.min(x,y));    
</script>
Copy after login

(13) pow(x,y): Returns the y power of x.

<script type="text/javascript">
        var x=2;        var y=3;
        document.write('pow(x,y)方法:<br/>');
        document.write(Math.pow(x,y));    
</script>
Copy after login

(14) random(): Returns a random number between 0 ~ 1.

<script type="text/javascript">
        document.write('random()方法:<br/>');
        document.write(Math.random());   
</script>
Copy after login

(15) round(x): Round the number to the nearest integer.

<script type="text/javascript">
        var x=23.4353;
        document.write('round(x)方法:<br/>');
        document.write(Math.round(x));    
</script>
Copy after login

(16) sin(x): Returns the sine of the number.

<script type="text/javascript">
        var x=Math.PI/2;        
        document.write('sin(x)方法:<br/>');
        document.write(Math.sin(x));    
</script>
Copy after login

(17) sqrt(x): Returns the square root of the number.

<script type="text/javascript">
        var x=2;
        document.write('sqrt(x)方法:<br/>');
        document.write(Math.sqrt(x));    
</script>
Copy after login

(18) tan(x): Returns the tangent of the angle.

<script type="text/javascript">
        var x=1;
        document.write('tan(x)方法:<br/>');
        document.write(Math.tan(x));    
</script>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of What is the Math object in js? how to use?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
js
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template